• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP调用mysql函数报错

php 搞代码 3年前 (2022-01-24) 20次浏览 已收录 0个评论

PHP调用mysql函数出错

Book-O-Rama Search Result

<body>

Book-O-Rama Search Results

<?
$searchtype = $_POST[‘searchtype’];
$searchterm = trim($_POST[‘searchterm’]);
if (!$searchtype || !$searchterm)
{
echo ‘You have not entered search details. Please go back and try again.’;
exit;
}

if (!get_magic_quotes_gpc())
{
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}

$db = new mysqli(‘localhost’, ‘bookorama’, ‘bookorama123’, ‘books’);
if (mysqli_connect_error())
{
echo ‘Error: Could not connect to database. Please try again later.’;
exit;
}

$db->select_db(“books”);

//$query = “select * from books where “.$searchtype.” like ‘%”.$searchterm.”%'”;
$query = “select * from books where “. $searchtype.”=”.”‘$searchterm'” ;
//$query = “select * from books”;
echo “$query\n”;

$result = $db->query($query);
$num_results = $result->num_rows;
echo “

Number of books found: “.$num_results.”

“;

for ($i = 0; $i < $num_results; $i++)
{
/* 此段被注释的代码运行出错,错误在于$result->fetch_assoc();
$result->fetch_assoc();
echo “

“.($i+1).”. Title: “;
echo htmlspecialchars(stripslashes($row[‘title’]));

echo “
Author: “;
echo stripslashes($row[‘author’]);

echo “
ISBN: “;
echo stripslashes($row[‘isbn’]);

echo “
Price: “;
echo stripslashes($row[‘price’]);

echo “

“;
*/

//下面的代码运行OK
$row = $result->f

5本文来源gao!daima.com搞$代!码#网#

搞代gaodaima码

etch_row();

echo “

“.($i+1).”. Title: “;
echo htmlspecialchars(stripslashes($row[2]));

echo “
Author: “;
echo stripslashes($row[1]);

echo “
ISBN: “;
echo stripslashes($row[0]);

echo “
Price: “;
echo stripslashes($row[3]);

echo “

“;

}
$result->free();
$db->close();
?>

PHP新手。上面的php代码中,For循环实现了从结果集中取出一行数据。奇怪的是当把注释掉的代码打开,把运行OK的代码注释掉,就会出错。出错的地方就在于调用fetch_assoc()函数。那为什么调用fetch_row()函数没有呢?


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP调用mysql函数报错

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址