Mysqli prepare error message?
$ mysqli = new mysqli ("localhost", "root", "," test "); $ mysqli-> query ('PREPARE mid FROM" SELECT name FROM test_user WHERE id =? "'); // run the working code / / $ res = $ mysqli-> query ('PREPARE mid FROM "SELECT name FROM test_user"'); // $ res = $ mysqli-> query ('EXECUTE mid;') or die (mysqli_error ($ mysqli)); // working code finished .. $ res = $ mysqli-> query ('EXECUTE mid 1;') or die (mysqli_error ($ mysqli));
while($resu = $res->fetch_object()) {
echo '<br>' .$resu->name;
}
Error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
my php PHP Version 5.3.0
and mysql version
mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.27 $
I got the correct result using where clause
a source to share
Use the prepare function for the SELECT query:
http://php.net/manual/en/mysqli.prepare.php
/* create a prepared statement */
if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {
/* bind parameters for markers */
$stmt->bind_param("s", $city);
/* execute query */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($district);
/* fetch value */
$stmt->fetch();
/* close statement */
$stmt->close();
}
a source to share
http://dev.mysql.com/doc/refman/5.1/en/execute.html
The EXECUTE statement must use the use clause. Thus, you can do something like:
$mysqli = new mysqli("localhost", "root", "", "test");
$mysqli->query('PREPARE mid FROM "SELECT name FROM test_user WHERE id = ?"');
$mysqli->query('SET @var1 = 1;');
$res = $mysqli->query( 'EXECUTE mid USING @var1;') or die(mysqli_error($mysqli));
a source to share