We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,089 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

whats wrong with this prepared statement?

I get an error when i try to execute this PS:

$ADJUST         = "UPDATE modul_galleri SET pos = pos - 1 WHERE pos = ?";
$ADJUST_PREP    = mysqli_stmt_prepare($connection, $ADJUST);
$ADJUST_PREP    = mysqli_stmt_bind_param($ADJUST_PREP, "i", $pos_op);

$pos_op = $_GET['pos'];
$pos_op = $pos_op + 1;

mysqli_stmt_execute($ADJUST_PREP) or die(mysqli_stmt_error($ADJUST_PREP));
mysqli_stmt_free_result($ADJUST_PREP);
mysqli_stmt_close($ADJUST_PREP);

The error: Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in C:\wamp\www\elvir\backend\ajaxPHP\moduler\galleri_img_pos.php on line 17

Line 17 is: mysqli_stmt_execute($ADJUST_PREP) or die(mysqli_stmt_error($ADJUST_PREP));

3
Contributors
12
Replies
17 Hours
Discussion Span
6 Months Ago
Last Updated
13
Views
Question
Answered
klemme
Posting Whiz
311 posts since Mar 2011
Reputation Points: 18
Solved Threads: 8
Skill Endorsements: 0

@klemme

whats wrong with this prepared statement?

This wrong:

$ADJUST_PREP = mysqli_stmt_prepare($connection, $ADJUST);
$ADJUST_PREP = mysqli_stmt_bind_param($ADJUST_PREP, "i", $pos_op);
$pos_op = $_GET['pos'];
$pos_op = $pos_op + 1;

Why are you using $ADJUST_PREP & $pos_op twice but different variable?

You only used it one time! The reason the error come up because mysqli_stmt_execute statment is confused of which statement to used because it's using both at the same time:

$ADJUST_PREP = mysqli_stmt_prepare($connection, $ADJUST); 

-and

$ADJUST_PREP = mysqli_stmt_bind_param($ADJUST_PREP, "i", $pos_op);

So another words your mysqli_connect probably didn't work.

I assume that $connection is your mysqli_connect

You need to rename one of those $ADJUST_PREP & $pos_op to a different name so it won't get confused.

LastMitch
Industrious Poster
4,165 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45
$ADJUST             = "UPDATE modul_galleri SET pos = pos - 1 WHERE pos = ?";
$ADJUST_PREP        = mysqli_stmt_prepare($connection, $ADJUST);
$ADJUST_STMT_PREP   = mysqli_stmt_bind_param($ADJUST_PREP, "i", $pos_op);

$pos = $_GET['pos'];
$pos_op = $pos + 1;

mysqli_stmt_execute($ADJUST_STMT_PREP);
mysqli_stmt_free_result($ADJUST_STMT_PREP);
mysqli_stmt_close($ADJUST_STMT_PREP);

If I set to to fixed values, it updates just fine, but with the variables the error comes back again?

Starting to see double after a long day here, bur I hope to fix it before stopping for the day..

Shouldnt the above work?

klemme
Posting Whiz
311 posts since Mar 2011
Reputation Points: 18
Solved Threads: 8
Skill Endorsements: 0

@klemme

 $ADJUST = "UPDATE modul_galleri SET pos = pos - 1 WHERE pos = ?";
 $ADJUST_PREP = mysqli_stmt_prepare($connection, $ADJUST);
 $ADJUST_STMT_PREP = mysqli_stmt_bind_param($ADJUST_PREP, "i", $pos_op);

 $pos = $_GET['pos'];
 $pos_op = $pos + 1;

 mysqli_stmt_execute($ADJUST_STMT_PREP);
 mysqli_stmt_free_result($ADJUST_STMT_PREP);
 mysqli_stmt_close($ADJUST_STMT_PREP);

If I set to to fixed values, it updates just fine, but with the variables the error comes back again?

Yes it looks right now have you test it out Yet?

Starting to see double after a long day here, bur I hope to fix it before stopping for the day..

I get it and I understand. It should work now. You did this very quickly. You won't get this error anymore:

mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean

LastMitch
Industrious Poster
4,165 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

Warning: mysqli_stmt_prepare() expects parameter 1 to be mysqli_stmt, object given in C:\wamp\www\elvir\backend\ajaxPHP\moduler\galleri_img_pos.php on line 11

Line 11 = $ADJUST_PREP = mysqli_stmt_prepare($connection, $ADJUST);

Hmm, the $connection should be fine though..

It shouldnt be this hard, but somehow it is..

klemme
Posting Whiz
311 posts since Mar 2011
Reputation Points: 18
Solved Threads: 8
Skill Endorsements: 0

@klemme

Hmm, the $connection should be fine though..

It shouldnt be this hard, but somehow it is..

I agree with you.

Can you post your query for your connection.

Do you have this @ in front of your mysqli connection?

Try to put these `` quotes on your query

LastMitch
Industrious Poster
4,165 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45
$host   = 'localhost';
$user   = 'root';
$pass   = '';
$db     = 'elvir';
$connection = mysqli_connect("$host", "$user", "$pass", "$db");
mysqli_set_charset($connection, 'utf8');

Any ideas? I use this connection variable throughout the CMS, and never ran into any issues?

klemme
Posting Whiz
311 posts since Mar 2011
Reputation Points: 18
Solved Threads: 8
Skill Endorsements: 0

I get the same error when quoted..

I tried clearing the cache, just to see if t somehow had saved the queries from before, but to no luck either.

Thanks for looking at it!!

klemme
Posting Whiz
311 posts since Mar 2011
Reputation Points: 18
Solved Threads: 8
Skill Endorsements: 0

It works, when i do thhis:

$adjust         = "UPDATE modul_galleri SET pos = pos - 1 WHERE pos = 2";
$ADJUST_PREP    = mysqli_prepare($connection, $adjust) or die(mysqli_error($connection));

$pos = $_GET['pos'];
$pos_op = $pos + 1;

mysqli_stmt_execute($ADJUST_PREP)or die(mysqli_stmt_error($connection));
mysqli_stmt_free_result($ADJUST_PREP)or die(mysqli_error($connection));
mysqli_stmt_close($ADJUST_PREP)or die(mysqli_error($connection));

So leaving out the mysqli_stmt_bind_param, and setting the value in the query to a fixed nnumber, makes it work. That is just not what i need...

Any idea why mysqli_stmt_bind_param is creating this error?

klemme
Posting Whiz
311 posts since Mar 2011
Reputation Points: 18
Solved Threads: 8
Skill Endorsements: 0

@klemme

Any idea why mysqli_stmt_bind_param is creating this error?

For the "i", change to this 'i'

From this

$ADJUST_STMT_PREP = mysqli_stmt_bind_param($ADJUST_PREP, "i", $pos_op);

to this

$ADJUST_STMT_PREP = mysqli_stmt_bind_param($ADJUST_PREP, 'i', $pos_op);

Instead of this using mysqli_stmt_free_result():

mysqli_stmt_free_result($ADJUST_STMT_PREP);

Try used mysqli_stmt_get_result():

mysqli_stmt_get_result($ADJUST_STMT_PREP);

Try it now.

So everything you change

$ADJUST = "UPDATE modul_galleri SET pos = pos - 1 WHERE pos = ?";
$ADJUST_PREP = mysqli_stmt_prepare($connection, $ADJUST);
$ADJUST_STMT_PREP = mysqli_stmt_bind_param($ADJUST_PREP, 'i', $pos_op);

$pos = $_GET['pos'];
$pos_op = $pos + 1;

mysqli_stmt_execute($ADJUST_STMT_PREP);
mysqli_stmt_get_result($ADJUST_STMT_PREP);
mysqli_stmt_close($ADJUST_STMT_PREP);
LastMitch
Industrious Poster
4,165 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

It gets me the exact same error msg as before.

Very weird, because further down the same page, ia am runnning another prepared statement with the same connection variable, and having no problems. I just get an error in every attampt to use mysqli_stmt_etc_etc.. in this script:

$adjust             = "UPDATE modul_galleri SET pos = pos - 1 WHERE pos = ?";
$ADJUST_PREP        = mysqli_stmt_prepare($connection, $adjust);
$ADJUST_STMT_PREP   = mysqli_stmt_bind_param($ADJUST_PREP, "i", $pos_op);

$pos = $_GET['pos'];
$pos_op = $pos + 1;

mysqli_stmt_execute($ADJUST_STMT_PREP);

Is there someway to debug better, than mysqli_error,mysqli_stmt_error? As I dont see whats wrong with the statements..Is there something in my installation etc? All though there shuldnt be, as I am using PS throughout the entire system with no problems. Its only this one thats f...s up..

klemme
Posting Whiz
311 posts since Mar 2011
Reputation Points: 18
Solved Threads: 8
Skill Endorsements: 0

I think $pos_op should go before the statement, because it still doesn't exists and so this will give a boolean FALSE value when you try to send it to mysqli, so try to change the order:

$pos = $_GET['pos'];
$pos_op = $pos + 1;

$adjust             = "UPDATE modul_galleri SET pos = pos - 1 WHERE pos = ?";
$ADJUST_PREP        = mysqli_stmt_prepare($connection, $adjust);
$ADJUST_STMT_PREP   = mysqli_stmt_bind_param($ADJUST_PREP, "i", $pos_op);

bye!

cereal
Veteran Poster
1,145 posts since Aug 2007
Reputation Points: 344
Solved Threads: 222
Skill Endorsements: 22

I have found out what i did wrong..

I had saved the mysqli_stmt_bind_param in a variable, and then used that variable in the execute statement, instead of the actual stmt object that was created using mysqli_stmt_prepare. So basically I wasnt using the stmt object at all :-)

Thanks, for your time LastMitch!

/Klemme

klemme
Posting Whiz
311 posts since Mar 2011
Reputation Points: 18
Solved Threads: 8
Skill Endorsements: 0
Question Answered as of 6 Months Ago by LastMitch and cereal

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1052 seconds using 2.74MB