http://zeel.dyndns.org/ss/home/
这是我的测试服务器上我的网站的主页,它工作得很好。其他页面也是如此。然而,在我的永久远程服务器(justhost)上
http://cvca.ohiofbla.org/store/home/
我得到:
警告:mysql_fetch_array():已提供参数不是有效的MySQL结果
中的资源/home/ohiofbl1/public_html/schools/cvca/store/home/index.php在线43
我看不出代码中有任何问题,同样的代码在测试服务器上运行良好
唯一的区别是,我有一个包含的文件,该文件定义了SQL用户/pass/dbname的变量,这些变量因服务器而异。但唯一不同的文件是include文件。
我是不是错过了什么?
<?php session_start(); require("../includes/config.php");?>
<?php $title = "Home"; ////////////////////////////////////////////?>
<?php require("../includes/head.php");
///////////////////////////////////////////////////////////////////?>
<?php
$con = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}?>
<div id="index_structure">
<div id="index_left">
<div id="index_top">
<h2>Welcome to the CVCA School Store! </h2>
<p>Here you can purchase items from the school store, and then pick them
up from school the next day. </p>
</div>
<div id="index_bottom">
<p align="center"><img src="../images/School Store 014.png" width="30%" />
<img src="../images/School Store 015.png" width="30%" />
<img src="../images/School Store 016.png" width="30%" /></p><br />
<p><strong>Hours:</strong> Mon - Fri, 7:45 - 8:05 AM. On school days. </p>
<p><strong>Location:</strong> Corner of west cafe </p>
<p>Backpack sales start in May - August for all new 7th graders.
Supplies are available year round.</p>
<p>Empty shoeboxes are accepted for Operation Shoebox at all times. </p>
<p>We are always looking for parent helpers and student employees. </p>
<p>School uniforms are available for fitting purposes.
Uniforms are available for purchase at
<span class="bluelink"><a href="http://cvcastore.com/">cvcastore.com</a>
</span></p>
<p><img src="../images/shirts.png" width="32%" /></p></div></div>
<div id="index_sidebar">
<h3 align="left">Featured items:</h3><hr />
<?php
function random_one($dbname, $con)
{
mysql_select_db($dbname, $con);
unset($result);
unset($col);
$i = 1;
while($i = 1)
{
$result = mysql_query("SELECT * FROM items'n"."ORDER BY RAND()'n"."LIMIT 1");
$col = mysql_fetch_array($result); //This is line 43 from the error
if (file_exists("../" . $col['ImagePath']) == true)
{
echo "<h4><a href='../item/?item=" . $col['ItemNumber'] . "'>" .
$col['ItemName'] . "</h4><br />
<img src='../" . $col['ImagePath'] . "' border='0' /></a>";
break;
}
}
?><hr /><?php
}
random_one($dbname, $con);
random_one($dbname, $con);
random_one($dbname, $con);
?></div></div>
<?php require("../includes/foot.php"); ///////////////////////////////?>
我自己检查的查询字符串没有任何问题(尽管我同意rineez的观点,但没有任何原因)。问题可能是变量$dbname,也可能是while循环本身。这是一个不会退出的中篇循环,因为您没有对$i变量做任何更改,从而退出while循环。
我自己发现了这个问题。
"SELECT * FROM items'n"."ORDER BY RAND()'n"."LIMIT 1"
表"items"实际上是"items",但我的基于Windows的测试服务器忽略了这一差异。然而,永久服务器是基于Linux的,它看到了"i"就迷路了。我改了名字,一切都很好。
在迭代之前,您应该始终检查是否返回了任何行。
通过终端或GUI(如PHPMyAdmin、SQLYog等)直接对生产数据库运行查询,并查看返回了哪些记录(如果有的话)。
尝试回显变量$dbname并验证其值是否正确。我认为当你创建数据库名称时,主机会添加你的cpanel用户名作为前缀。。。
我想知道为什么必须使用和串联来形成查询字符串。