很奇怪的PHP错误提示。

atomy9 2005-03-24 01:09:06
我下面的代码如果在function里面就会出现“Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in D:\html\php\bbs\def.php on line 33”错误。
错误句子是:$data1=mysql_query($sql_1,$conn) or die(mysql_error());

但是假如我把function定义的VIEWTOP1 给去掉,内容就能正常显示了。。。完整代码如下

=============代码===========================


<?

$cn_server="localhost";
$cn_user="root";
$cn_password="passowrd";
$cn_database="bbs";
//====================表结构============================

$users="bbs_user"; //用户表

$top1 = "bbs_top1"; //标题栏目

//=======================================================
$conn=mysql_connect($cn_server,$cn_user,$cn_password)or die("不能连接到服务器上的数据库");

mysql_select_db($cn_database) or die( "不能连接到相应的数据库");


function viewtop1() {

$sql_1="select * from ".$top1." where iid=0";

$data1=mysql_query($sql_1,$conn) or die(mysql_error());

$num1=mysql_num_rows($data1);

if ($num1==0) {
mysql_close($conn);
mysql_free_result($data1);
exit;
} else {


for ($si=0; $si<$num1; $si++) {
$rs=mysql_fetch_array($data1);
$value1 = $value1 . "<li>" . $rs["object"] . "</li>";
}
mysql_free_result($data1);

}
echo "<ul>". $value1 . "</ul>";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<link href="include/sytle.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header">
<? viewtop1(); ?></div>
<div id="contain">
<div id="mainbg">
<div id="right">
<div
class="text">
<p> </p>
</div>
</div>
<div id="left">
<div class="text"></div>
</div>
</div>
</div>
<div id="footer">
<p> </p></div>
</body>
</html>
...全文
88 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
atomy9 2005-03-24
  • 打赏
  • 举报
回复
行了,谢谢。
xuzuning 2005-03-24
  • 打赏
  • 举报
回复
function viewtop1() {
global $conn,$top1;
$sql_1="select * from ".$top1." where iid=0";
$data1=mysql_query($sql_1,$conn) or die(mysql_error());
....
}

function viewtop1() {
$sql_1="select * from ".$GLOBALS['top1']." where iid=0";
$data1=mysql_query($sql_1,$GLOBALS['conn']) or die(mysql_error());
....
}

function viewtop1($connect,$tbl_name) {
$sql_1="select * from ".$tbl_name." where iid=0";
$data1=mysql_query($sql_1,$connect) or die(mysql_error());
....
}
<? viewtop1($conn,$top1); ?></div>

在函数中使用全局变量需显式的传递或在函数内声明
atomy9 2005-03-24
  • 打赏
  • 举报
回复
$sql_1="select * from ".$top1." where iid=0";我试着改为
$sql_1="select * from $top1 where iid=0";还是出一样的错误提示的,如果不使用在FUNCTION中就能正常使用。
f2bx 2005-03-24
  • 打赏
  • 举报
回复
你没有注意到变量的作用域!!!

$conn变量是定义在函数viewtop1() 之外,对于函数viewtop1() 来说$conn是不可见的。在函数内部使用$conn时,$conn是未定义的变量,当然会报错。

解决方法:
(1)将$conn作为函数参数传入函数viewtop1() 里面
function viewtop1($conn){
……
}

调用viewtop1($conn);

(2)将$conn声明为全局变量

建议使用方案1

你的代码中有一些问题:
(1)函数viewtop1() 中$top1同样没有定义,数据库操作将会报错;
(2)当$num1==0时,$value1没有定义,会输出"<ul></ul>";$value1应放入$num1!=0的那个代码块中

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧