comment.php文件
<?php
class Comment
{
var $conn;
function __construct() //连接数据库
{
$this->conn = mysqli_connect("localhost", "root", "12345", "infomanage");
mysqli_query($this->conn, "SET NAMES gbk");
} function __destruct() // 关闭连接
{ mysqli_close($this->conn);
}
function viewcontent() //查询所有评论信息
{
$sqlqe = "select * from commment;";
$arr=$this->conn->query($sqlqe);
return $arr;
}
}
showlist文件:
<?php
require("/var/www/betacom/Comment.php");
$comment=new Comment();
$result=$comment->viewcontent(); echo "<table>\n";
echo "<tr>\n";
while($row=mysql_fetch_row($result))
{
echo '<td bgcolor="#00FF00">';
echo "$row[1]";
echo '</td>'; echo '<td bgcolor="#00FF00">';
echo "$row[2]";
echo '</td>'; echo '<td bgcolor="#00FF00">';
echo "$row[3]";
echo '</td>';
}
echo "</tr>\n";
echo "</table>\n";
?>
数据库表:
comment (表名)四列 id是标志列,其他是字符串非空
id title username content怎么老连接不上数据???
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /var/www/betacom/showlist.php on line 23
<?php
class Comment
{
var $conn;
function __construct() //连接数据库
{
$this->conn = mysqli_connect("localhost", "root", "12345", "infomanage");
mysqli_query($this->conn, "SET NAMES gbk");
} function __destruct() // 关闭连接
{ mysqli_close($this->conn);
}
function viewcontent() //查询所有评论信息
{
$sqlqe = "select * from commment;";
$arr=$this->conn->query($sqlqe);
return $arr;
}
}
showlist文件:
<?php
require("/var/www/betacom/Comment.php");
$comment=new Comment();
$result=$comment->viewcontent(); echo "<table>\n";
echo "<tr>\n";
while($row=mysql_fetch_row($result))
{
echo '<td bgcolor="#00FF00">';
echo "$row[1]";
echo '</td>'; echo '<td bgcolor="#00FF00">';
echo "$row[2]";
echo '</td>'; echo '<td bgcolor="#00FF00">';
echo "$row[3]";
echo '</td>';
}
echo "</tr>\n";
echo "</table>\n";
?>
数据库表:
comment (表名)四列 id是标志列,其他是字符串非空
id title username content怎么老连接不上数据???
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /var/www/betacom/showlist.php on line 23
例子 1. Object oriented style<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";if ($result = $mysqli->query($query)) { /* fetch object array */
while ($row = $result->fetch_row()) {
printf ("%s (%s)\n", $row[0], $row[1]);
} /* free result set */
$result->close();
}/* close connection */
$mysqli->close();
?>
例子 2. Procedural style<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";if ($result = mysqli_query($link, $query)) { /* fetch associative array */
while ($row = mysqli_fetch_row($result)) {
printf ("%s (%s)\n", $row[0], $row[1]);
} /* free result set */
mysqli_free_result($result);
}/* close connection */
mysqli_close($link);
?>
上例将输出:Pueblo (USA)
Arvada (USA)
Cape Coral (USA)
Green Bay (USA)
Santa Clara (USA)
原因有二:
1.連接數據庫時失敗
2.執行查詢時失敗
解決:
1.
function __construct() //连接数据库
{
$this->conn = mysqli_connect("localhost", "root", "12345", "infomanage");
if (!$this->conn)
echo "連接數據庫失敗";//查看是否連接成功
mysqli_query($this->conn, "SET NAMES gbk");
}
2.
function viewcontent() //查询所有评论信息
{
$sqlqe = "select * from commment;";
$arr=$this->conn->query($sqlqe) or die(mysql_error().$sqlqe);//如果查詢失敗,則提示
return $arr;
}
最後:php很簡單,不要出一點問題就認為啥啥
评论不是 commment
而是 comment楼主看看是不是这个问题。