脚本破坏了MySQL查询,但是没有错误提示? [英] Script breaking below MySQL queries, but no error being given?

查看:43
本文介绍了脚本破坏了MySQL查询,但是没有错误提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白问题出在哪里,我已经尽了一切力所能及地诊断出问题,并设法找出了我认为是引起问题的地方(请参见下面的隔离):

I really don't understand what the issue is here, I've tried everything I can do diagnose the problem, and managed to isolate where I think the issue is being caused (see isolation below):

这是我的完整代码:

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);

运行脚本时,应为随机选择的URL echo $link;不显示任何内容.

When the script is run, echo $link; which should be a randomly selected URL, displays nothing.

为了诊断问题,我一直在尝试使用echo "Hello World!";,将其逐步移至脚本上,直到在初始MySQL查询之前放置echo "Hello World!";之前,什么都没有显示.即使没有打印mysql_error()'s,问题仍然存在.

In order to diagnose the issue, I've been attempting to use echo "Hello World!";, gradually moving it up the script, any nothing is displayed until echo "Hello World!"; is placed before the initial MySQL query, which leads me to believe that the issue lies within there, even though no mysql_error()'s are being printed.

此外,我尝试在$size = ...下面的行上echo $size;仍然不显示任何内容.

Also, I've tried to echo $size; on the line below $size = ... and that still displays nothing.

隔离:

$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());

有人知道为什么这会导致脚本的其余部分无法运行,却没有返回错误吗?

Does anyone have any idea why this would cause the rest of the script to not run, yet return no errors?

任何帮助将不胜感激.

推荐答案

我认为问题出在这一行:

I think the problem is in this line:

$size = mysql_num_rows($result) or die(mysql_error());

当num_rows调用返回0时,即使根本没有错误,解释器也会解析die(mysql_error())部分.

when the num_rows call returns 0, the interpreter will parse the die(mysql_error()) part even if there is no error at all.

课程:最好避免使用... or die()构造.而是进行适当的检查:

Lesson: It's best to avoid ... or die() constructs. Do a proper check instead:

$size = mysql_num_rows($result);

if ($size === false) die(mysql_error()); // or, even better, trigger_error()
                                         // so mySQL errors aren't shown
                                         // in production

这篇关于脚本破坏了MySQL查询,但是没有错误提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆