0

我假设我是 php 新手。我试图创建一个显示 MySQL 数据库内容的 php 页面。当我在 localhost 中尝试时,我收到以下警告:“致命错误:在第 18 行的 C:\xampp\htdocs\phplessons\guestbook_displayer_2.php 中调用未定义函数 mysql_results()”。似乎数据库连接有效。有人有小费吗?

这是我的代码:

<html>
<head></head>
<title>Display MySQL db</title>
<body>
<?php
$db=mysql_connect("localhost","root","mypassword"); //db connection
mysql_select_db ("prova001"); //choose a db
$res=mysql_query("SELECT * from php_guestbook"); //query a table
$num=mysql_num_rows($res);
// begin table
echo "<table border=1>";
echo "<tr><td>Nr.</td><td>First name</td>";
echo"<td>Last name</td><td>Country</td>";
echo"<td>E-Mail address</td><td>Telephone</td></tr>";
// contatore
for ($i=0; $i<$num; $i++)
{
$cg=mysql_results($res,$i,"firstname"); // line 18 this var is undefined. 
$nm=mysql_results($res,$i,"lastname"); //Probably also the others have a similar problem.
$np=mysql_results($res,$i,"country"); //Can it be due to a bad record counter?
$st=mysql_results($res,$i,"email");
$tl=mysql_results($res,$i,"telephone");
$lf=$i+1;
//
echo "<tr><td>$lf</td><td>$cg</td><td>$nm</td><td>$np</td><td>$st</td><td>$tl</td></tr>";
}
echo "</table>";
mysql_close($db);
?>
</body>

4

1 回答 1

1

你可能打错了。该方法是没有's'的mysql_result() 。

但是,您可以通过这种方式缩短查询结果处理;

// query
$res=mysql_query("SELECT * from php_guestbook"); //query a table
// begin table
echo "<table border=1>";
echo "<tr><td>Nr.</td><td>First name</td>";
echo"<td>Last name</td><td>Country</td>";
echo"<td>E-Mail address</td><td>Telephone</td></tr>";

while ($item = @mysql_fetch_assoc($res)) {
    // do something with var $item;
    $cg = $item['firstname'];
    $nm = $item['lastname'];
    // ect
}
于 2013-07-28T10:10:41.490 回答