2

I'm trying to read from DB using mysqli but the script is crashing on the mysqli_stmt_bind_result statement. I cannot find any reason - perhaps any of you with fresh eyes??

If I comment out that line, $rows is populated, so it's clearly connecting to the database OK.

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$id="a";
$name="b";
$dbc=mysqli_connect("1.2.3.4", "username", "password", "database") OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );

echo "Hello\n";
#### Read park names from DB ####
$q="SELECT id,name FROM park WHERE status=? ORDER BY id";
$stmt=mysqli_prepare($dbc, $q);
$status='show';
mysqli_stmt_bind_param($stmt,'s',$status);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt,$id,$name);
mysqli_stmt_store_result($stmt);
$rows=mysqli_stmt_num_rows($stmt);
echo "rows: $rows\n";
while (mysqli_stmt_fetch($stmt)) {
    echo "<li><a href=\"/park/$id/\">$name</a></li>\n";
}
mysqli_stmt_close($stmt);
#### Finished reading park names from DB ####
mysqli_close($dbc);
echo "Goodbye\n";
?>

PHPINFO reports the following for mysqli:

Client API library version 5.0.91

Client API header version 5.0.95

I've tried switching between PHP 5.2 and 5.4, no change.

Dharman
  • 30,962
  • 25
  • 85
  • 135
jeremy156
  • 21
  • 2
  • Where do you define `$id` and `$name` ? Do you have error reporting on and at the appropriate level? – Damien Pirsy Jan 15 '14 at 22:55
  • As a bit of a PHP beginner, having learned by hacking away rather than studying, I'm afraid I don't understand the concept of defining variables.... so perhaps this is the root of my problem. Could you point me in the right direction? This would be the first time I refer to these variables - what do I need to do first? – jeremy156 Jan 15 '14 at 22:58
  • 1
    @DamienPirsy: bind_result stores the data _in those variables_ (they are arguments by reference), so there is no need to define them beforehand (that will just be overwritten). jeremy156: look at [how to get useful error messages from PHP](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php). It there's a 500 error, PHP will know why, you just have to configure it to tell you what that problem is. – Wrikken Jan 15 '14 at 23:00
  • If this is used in conjunction with a form using POST as the method, they would first need to be defined like this: `$id=$_POST['id']; $name=$_POST['name'];` <= depending on how your form elements are named. – Funk Forty Niner Jan 15 '14 at 23:02
  • They just don't "magically appear/happen". They need to be defined. Call it an "address" if I may. If the postman doesn't know where you live, how do you expect the mail to make it to your house? ;-) – Funk Forty Niner Jan 15 '14 at 23:04
  • OK, these are intended to be variables to contain the output of the query when it gets to mysqli_stmt_fetch ... they are not input and don't need values until that point.... so how should I declare them? I honestly thought PHP declared variables upon first use, but I'm showing my ignorance here :) – jeremy156 Jan 15 '14 at 23:06
  • Again, and as already asked by Damien; where are `$id,$name` coming from? What are you using this with? There's GIGO, first we need the `GI` as in `garbage in`, then `GO` as in `garbage out`. That's an old computer term (`GIGO`), by the way. Something needs to come IN before it can go OUT, *just like food.* – Funk Forty Niner Jan 15 '14 at 23:08
  • I've added `error_reporting(E_ALL); ini_set('display_errors', '1');` to the beginning, which complained about undeclared variables. I've then added `$id="a";` and `$name="b";` before referring to them later, but again it crashes out and doesn't even display an error message. – jeremy156 Jan 15 '14 at 23:14
  • I'm assuming that you have a column named `id`, am I correct? If so, then you don't need it **IF** it is an `AUTO_INCREMENT` type. As for the crashing part, I can't help you out with that. I've never seen a script cause a 500 error, unless it was a misconfiguration in PHP and/or MYSQL. I don't know what else to tell you that will help. Or, try `$q="SELECT * FROM park WHERE status=? ORDER BY id";` instead of calling 2 columns. – Funk Forty Niner Jan 15 '14 at 23:19
  • There is indeed a field named `id` and the query returns a couple of results when run manually. Thanks anyway Fred, I appreciate your efforts. – jeremy156 Jan 15 '14 at 23:23
  • Did you found a solution? I have exact same problem. – Matej Ukmar Apr 05 '17 at 11:06
  • This code is working perfectly fine. I run it and I didn't get any results. There must have been something else you have not shown us. – Dharman Jan 15 '20 at 20:29

0 Answers0