如何浏览参数化查询的结果?
我正在尝试浏览PHP中的对神经分散查询的结果。 我正在尝试这种方式
//creating a prepared statement
$stmt = $mysqli->stmt_init();
$query = "SELECT pcode,price,description FROM products WHERE description like ? ORDER BY PRICE";
$stmt->prepare($query);
$search_criteria = '%'.$search_criteria.'%';
$stmt->bind_param('s', $search_criteria);
$stmt->execute();
$i=0;
while($stmt->fetch())
{
if ($i++%2 == 0) $trow = "even"; else $trow = "odd";
echo " <tr class='{$trow}'>";
echo " <td>{$row['pcode']}</td><td>{$row['price']}</td><td>{$row['description']}</td>";
echo " </tr>";
}
if ($i==0) {
echo "<tr class='even'><td colspan='3'><h3>No Results.</h3></td></tr>";
}
,但是我的代码正在输入IF语句。 我该如何完成结果? 数据库是mySQL。
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname) or die('Did you <a href="setupreset.php">setup/reset the DB</a>? <p><b>SQL Error:</b>' . mysql_error($conn) . '<p><b>SQL Statement:</b>' . $query);
function execute_query($the_query){
global $conn;
$result = mysql_query($the_query) or die('<font size=2><b>SQL Error:</b>' . mysql_error($conn) . '<br/><p><b>SQL Statement:</b>' . $the_query . '<br/>Did you run <i>setupreset.php</i> to setup/reset the DB?</font>');
return $result;
}
?>
I'm trying to go through the result of a paremeterized query in PHP.
I'm trying this way
//creating a prepared statement
$stmt = $mysqli->stmt_init();
$query = "SELECT pcode,price,description FROM products WHERE description like ? ORDER BY PRICE";
$stmt->prepare($query);
$search_criteria = '%'.$search_criteria.'%';
$stmt->bind_param('s', $search_criteria);
$stmt->execute();
$i=0;
while($stmt->fetch())
{
if ($i++%2 == 0) $trow = "even"; else $trow = "odd";
echo " <tr class='{$trow}'>";
echo " <td>{$row['pcode']}</td><td>{$row['price']}</td><td>{$row['description']}</td>";
echo " </tr>";
}
if ($i==0) {
echo "<tr class='even'><td colspan='3'><h3>No Results.</h3></td></tr>";
}
But my code is entering the if statement.
How can I go through the result?
The database is MySQL.
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname) or die('Did you <a href="setupreset.php">setup/reset the DB</a>? <p><b>SQL Error:</b>' . mysql_error($conn) . '<p><b>SQL Statement:</b>' . $query);
function execute_query($the_query){
global $conn;
$result = mysql_query($the_query) or die('<font size=2><b>SQL Error:</b>' . mysql_error($conn) . '<br/><p><b>SQL Statement:</b>' . $the_query . '<br/>Did you run <i>setupreset.php</i> to setup/reset the DB?</font>');
return $result;
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将结果绑定到一些变量:
You need to bind the result to some variables: