PHP调用mysql函数报错
程序员文章站
2022-04-05 23:16:47
...
PHP调用mysql函数出错
Book-O-Rama Search Result
$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 "
for ($i = 0; $i {
/* 此段被注释的代码运行出错,错误在于$result->fetch_assoc();
$result->fetch_assoc();
echo "
*/
//下面的代码运行OK
$row = $result->fetch_row();
echo "
}
$result->free();
$db->close();
?>
PHP新手。上面的php代码中,For循环实现了从结果集中取出一行数据。奇怪的是当把注释掉的代码打开,把运行OK的代码注释掉,就会出错。出错的地方就在于调用fetch_assoc()函数。那为什么调用fetch_row()函数没有呢?
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 {
/* 此段被注释的代码运行出错,错误在于$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->fetch_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()函数没有呢?
相关文章
相关视频
上一篇: PHP实现登陆代理
推荐阅读
-
ajax+php控制所有后台函数调用,ajaxphp后台函数
-
这个SQL里的函数是mysql函数还是php函数,函数意思是,该怎么处理
-
PHP+Mysql实现多关键字与多字段生成SQL语句的函数
-
php调用mysql存储过程的方法
-
mysql 创建函数报错:This function has none of DETERMINISTIC,
-
使用命名参数调用 PHP 函数
-
PHP4 与 MySQL 数据库操作函数详解(2)_MySQL
-
php中,用echo调用JS函数,有什么问题?
-
php调用mysql数据 dbclass类_PHP
-
ajax + php + Controller 控制所有后台函数调用,ajaxcontroller_PHP教程