-1

I am moving to mysqli - and I know that some will say that I should go for PDO - but that's not where I am at now. My first prepared statement is not working, and I am at a loss on why. I looked at PHP documentation and it seems as I am alright. I am not getting errors - and I did provoke an error situation and got messages. It may just be on the fetch_assoc part. Please see below:

I appreciate any assistance.

$link = mysqli_connect("localhost","user","pass","db") or trigger_error($link->error);
mysqli_set_charset($link,"utf8");

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
$ct=count($arr3_modid);
if(!($stmt = $link->prepare("SELECT vbrands.* FROM vbrands WHERE model_id=? ORDER BY  price"))){
echo "Prepare failed: (" . $link->errno . ") " . $link->error;  }

$i=0;
for($i=0;$i <= $ct-1; $i++){
$arr3_int=(int)$arr3_modid[$i];

if(!($stmt->bind_param("i", $arr3_int))){
echo "Bind failed: (" . $stmt->errno . ") " . $stmt->error; }

if(!($stmt->execute())){
echo "execute failed: (" . $stmt->errno . ") " . $stmt->error;  }

if(!($res = $stmt->get_result())){
echo "res  failed: (" . $stmt->errno . ") " . $stmt->error; }

while($r3=$res->fetch_assoc()){
echo $r3['version_id];
echo.......
}
}

The variable $arr3_modid is an array of strings.

BernardA
  • 1,391
  • 19
  • 48
  • Add single quotes, `echo $r3['version_id'];` instead of `echo $r3['version_id];` – Krish R Dec 16 '13 at 19:40
  • what is `$ct-1` ? if that's 0, then your for loop doesn't do anything.. – Nanne Dec 16 '13 at 19:41
  • @Nanne $ct is defined elsewhere and is just the count. It is working fine. Remember I am converting from mysql extension where all is working well. – BernardA Dec 16 '13 at 19:44
  • @user876345. Thanks for the edit. This was just my typo here. This is not the reason it is not working. – BernardA Dec 16 '13 at 19:45
  • `while($r3=$res->fetch_assoc()){` may be the one giving error (not sure at all). `while($r3=$res->fetch()){` ? – tattvamasi Dec 16 '13 at 19:46

1 Answers1

0
  1. Consider using error_reporting(E_ALL);

  2. $i=0; above for($i=0 is not necessary.

  3. echo....... gives an error, I'm considering you suppressed part of the code, if not, this is your error and the call to error_reporting is not working properly.

  4. Check if you have the MySQLi extension installed/compiled with your PHP.

psxls
  • 6,807
  • 6
  • 30
  • 50
  • thanks! Did change error to E_ALL with no different result than before. The echo..... is just a way to say that it goes on and on with the echo'ing. Mysqli is installed and working. Before trying the prepared statement I did all the conversion to 'regular' mysqli. Then I moved to prepared stmt's and hit the wall. – BernardA Dec 16 '13 at 20:50
  • 1
    At least part of the problem seems to be that get_result requires mysqlnd, which MAMP does not have, to my surprise. There is a work around( http://stackoverflow.com/questions/7016169/php-mysqli-return-row-and-set-to-var-help/7016350#7016350), but I am not interested in creating custom functions to do the job that the extension is supposed to do. Will look at PDO, after all. – BernardA Dec 17 '13 at 09:00