Prepared expression returns nothing

Please forgive me for the possibility of the stupidity of this question, but I just started using prepared statements. I know this particular request works as I tested it with unprepared procedural methods. Here it is:

$name = 'introduction';
$mysqli = new mysqli('localhost', 'user', 'pass', 'db') or die('There was a problem connecting to the database.');
$stmt = $mysqli->prepare("SELECT name, content FROM sections WHERE name = ?");
$stmt->bind_param('s', $name);
$stmt->execute();
$stmt->bind_result($content);
$stmt->fetch();
echo  $content;
$stmt->close();

Any advice or recommendations in the right direction are welcome!

EDIT 1: Just updating my progress when I try to troubleshoot. I realized that since I have an identifier column as an index in the partition table, I also need to link this as a result, given the above statement on php.net (thanks again, Bill).

Here's the new code:

$name = 'introduction';
$mysqli = new mysqli('localhost', 'user', 'pass', 'db') or die('There was a problem connecting to the database.');
$stmt = $mysqli->prepare("SELECT name, content FROM sections WHERE name = ?");
$stmt->bind_param('s', $name);
$stmt->execute();
$stmt->bind_result($id, $name, $content);
$stmt->fetch();
echo $content;
$stmt->close();

, . ( : . , , , ?)

2: , , . , , . , .

, :

$name = 'introduction';
@mysql_connect('host', 'user', 'pass');
@mysql_select_db('db');
$query = "SELECT name,content FROM sections WHERE name = '$name'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_object($result)) {
    $content = $row->content;
    echo $content;
}

, . , , :

$name = 'introduction';
$mysqli = new mysqli('localhost', 'user', 'pass', 'db') or die('There was a problem connecting to the database.');
$stmt = $mysqli->prepare("SELECT name, content FROM sections WHERE name = ?");
$stmt->bind_param('s', $name);
$stmt->execute();
$stmt->bind_result($name, $content);
$stmt->fetch();
echo  $content;
$stmt->close();

( , , ), . , , html ( ), (500), sql. ?

, !

3: , , , , , . , , , .

.

+3
2

, - :

http://php.net/manual/en/mysqli-stmt.bind-result.php :

, mysqli_stmt_execute() mysqli_stmt_fetch().

( )


, .

, . $name, :

PHP Warning:  mysqli_stmt::bind_result(): Number of bind variables doesn't 
match number of fields in prepared statement in mysqli.php on line 9
PHP Stack trace:
PHP   1. {main}() /Users/bill/workspace/PHP/mysqli.php:0
PHP   2. mysqli_stmt->bind_result() /Users/bill/workspace/PHP/mysqli.php:9

.

$name, $content , .

, : , , ? , name = 'introduction'? , SQL .

, , , PHP script, , . , , .

+4

$stmt->bind_result($name, $content);

2

0

Source: https://habr.com/ru/post/1710874/


All Articles