我目前正在将所有旧的MySQL更改为MySQLI。但是,在使用预准备语句时尝试从表中进行SELECT时遇到问题。标签是一个字符串。
我的测试网址是:
http://example.com/retreiveCustomArticle.php?Tags=the
我的输出:
string(3) "the" string(44) "SELECT `ID` FROM `Articles` WHERE `Tags` = ?" Success!: 0
代码:
<?php
//Open a new connection to the MySQL server
$mysqli = new mysqli('localhost','createyo_james','password','createyo_TestDatabase');
//Output any connection error
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
//values to be inserted in database table
$Tags = $_GET["Tags"];
var_dump($_GET["Tags"]);
$query = "SELECT `ID`, `NewsStory`, `Summary1`, `Summary2` FROM `Articles` WHERE `Tags` = ?";
$statement = $mysqli->prepare($query);
var_dump($query);
//bind parameters for markers, where (s = string, i = integer, d = double, b = blob)
$statement->bind_param('s', $Tags);
if($statement->execute()){
$result = $stmt -> get_result();
/* bind result variables */
$stmt->bind_result($ID,$NewsStory,$Summary1,$Summary2);
/* fetch values */
while ($stmt->fetch()) {
$output[]=array($ID,$NewsStory,$Summary1,$Summary2);
}
print(json_encode($output));
$stmt -> close();
}else{
die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
$statement->close();
?>
答案 0 :(得分:1)
除了@VotetoClose所说的,我建议你:
$statement=$mysqli->prepare('SELECT ID FROM Articles WHERE Tags = ?');
$mysqli->execute(array($_GET['Tags']))
答案 1 :(得分:0)
您错过了bind_result()
和fetch()
if($statement->execute()){
if ($statement->bind_result($theId)) { // $theId will be the result that is returned from the database
if ($statement->fetch()) { // if fetched successfully
print 'Success!: ' . $theId . '<br />'; // do this