我真的不明白这里的问题是什么,我已经尝试了我可以做的所有事情来诊断问题,并设法隔离我认为引起问题的地方(见下面的隔离):
这是我的完整代码:
include("db_conn.php");
$conn = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error()); mysql_select_db($db_name) or die(mysql_error());;
$timestamp = time();
$add_time = time()+(60*60);
$query = "UPDATE links SET timestamp = '$add_time', hit_counter = '0' WHERE timestamp >= '$timestamp' AND overflow = 'NO'";
$result = mysql_query($query) or die(mysql_error());
$query = "SELECT * FROM links WHERE timestamp <= '$timestamp' AND hit_counter <= max_hits AND overflow = 'NO'";
$result = mysql_query($query);
$size = mysql_num_rows($result) or die(mysql_error());
if($size == 0) {
$query = "SELECT * FROM links WHERE overflow = 'YES'";
$result = mysql_query($query) or die(mysql_error());
$overflow = array();
while($row = mysql_fetch_assoc($result)) {
$overflow[] = $row['link'];
}
header("Location: http://www.google.com/?url=$overflow[0]");
}
$links = array();
$hits = array();
while($rows = mysql_fetch_assoc($result)) {
$links[] = $rows['link'];
$hits[] = $rows['hit_counter'];
}
$key = rand(0,$size);
$link = $links[$key];
$hit_counter = $hits[$key]+1;
$query = "UPDATE links SET hit_counter = '$hit_counter' WHERE link = '$link'";
$result = mysql_query($query) or die(mysql_error());*/
echo $link;
echo $hit_counter;
mysql_close($conn);
当脚本运行时,echo $link;
应该是随机选择的URL,不显示任何内容。
为了诊断问题,我一直在尝试使用echo "Hello World!";
,逐步向上移动脚本,在初始MySQL查询之前放置echo "Hello World!";
之前不显示任何内容我相信问题就在那里,即使没有打印mysql_error()'s
。
另外,我在echo $size;
下面的一行尝试$size = ...
,但仍然没有显示任何内容。
隔离:
$query = "UPDATE links SET timestamp = '$add_time', hit_counter = '0' WHERE timestamp >= '$timestamp' AND overflow = 'NO'";
$result = mysql_query($query) or die(mysql_error());
$query = "SELECT * FROM links WHERE timestamp <= '$timestamp' AND hit_counter <= max_hits AND overflow = 'NO'";
$result = mysql_query($query);
$size = mysql_num_rows($result) or die(mysql_error());
有没有人知道为什么这会导致脚本的其余部分无法运行,但却没有返回任何错误?
非常感谢任何帮助。
答案 0 :(得分:3)
我认为问题出在这一行:
$size = mysql_num_rows($result) or die(mysql_error());
当num_rows调用返回0
时,解释器将解析die(mysql_error())
部分,即使根本没有错误。
课程:最好避免使用... or die()
构造。请进行适当的检查:
$size = mysql_num_rows($result);
if ($size === false) die(mysql_error()); // or, even better, trigger_error()
// so mySQL errors aren't shown
// in production