Tuesday 21 November 2017
mysql - prepared statement failing for "SELECT" command, using mysqli in PHP
Answer
Answer
I've been able to execute multiple
prepared statements using mysqli (in PHP).
However, for some reason the
prepared statements always encounter an error when I try running a "SELECT"
command.
For example, the following line will
succeed:
$stmt=$mysqli->prepare("UPDATE
events SET category='m' WHERE
id=(?)");
However, the
following line will
fail:
$stmt=$mysqli->prepare("SELECT
* FROM events WHERE
id=(?)");
When I say
fail, I mean that the next three lines will return 1 for the UPDATE command (indicating
that one row was
changed)...
$stmt->bind_param('i',$id);
$stmt->execute();
echo
$stmt->affected_rows;
The
next three lines will return 0 for
SELECT:
$stmt->bind_param('i',$id);
$stmt->execute();
echo
$stmt->num_rows;
For
the record, I'm aware that prepared statements aren't that efficient for a single SELECT
- this question is mostly academic.
This
function (affected_rows) only works with queries which update a table. In order to get
the number of rows from a SELECT query, use mysqli_stmt_num_rows()
instead.
href="http://php.net/manual/en/mysqli-stmt.affected-rows.php"
rel="nofollow">http://php.net/manual/en/mysqli-stmt.affected-rows.php
Make
sure you store the result
first!!
$stmt->execute();
/*
store result */
$stmt->store_result();
printf("Number
of rows: %d.\n", $stmt->num_rows);
php - file_get_contents shows unexpected output while reading a file
I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...
-
I have an app which needs a login and a registration with SQLite. I have the database and a user can login and register. But i would like th...
-
I would like to use enhanced REP MOVSB (ERMSB) to get a high bandwidth for a custom memcpy . ERMSB was introduced with the Ivy Bridge micro...
-
According to my understanding, and my calculator, cos(90 degrees) equals 0 . In my code, I have a funct...
No comments:
Post a Comment