连接数据库问题(php比jsp,java难的多,郁闷死)

mailghyth 2009-08-31 10:14:41
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
...全文
128 点赞 收藏 10
写回复
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzwen 2009-08-31
echo显示html代码?感觉这样不好。
回复
yaron_org_cn 2009-08-31
LZ 的表名是不是多了个m啊?
评论不是 commment
而是 comment

楼主看看是不是这个问题。
回复
lgzxz999 2009-08-31
PHP应该是简单的要死要活的吧....
回复
ldyanstar 2009-08-31
lz不细心,sql语句有问题,慢慢查查!
回复
tfxg 2009-08-31
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /var/www/betacom/showlist.php on line 23
原因有二:
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很簡單,不要出一點問題就認為啥啥
回复
foolbirdflyfirst 2009-08-31
原来是mysqli...,sql语句也有问题,如ls所说。

好好看看手册就知道了。


例子 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)

回复
dzxccsu 2009-08-31
$sqlqe = "select * from commment;"; 改为$sqlqe = "select * from commment";
回复
dzxccsu 2009-08-31
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /var/www/betacom/showlist.php on line 23 这个错误不是连不上数据库,是你的SQL语句有问题!
回复
TM_skyinfo 2009-08-31
php数据库连接好多了像Java这些还要数据库连接的包更麻烦
回复
fenyao 2009-08-31
被骗了 PHP连接数据库太简单了
回复
发动态
发帖子
基础编程
创建于2007-09-28

2.1w+

社区成员

从PHP安装配置,PHP入门,PHP基础到PHP应用
申请成为版主
社区公告
暂无公告