PHP調用mysql函數報錯

來源:互聯網
上載者:User
PHP調用mysql函數出錯


Book-O-Rama Search Result


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->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()函數沒有呢?
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.