开发者

Script breaking below MySQL queries, but no error being given?

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

Here's my full code:

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);
开发者_StackOverflow社区
$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);

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

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.

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

Isolation:

$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?

Any help would be greatly appreciated.


I think the problem is in this line:

$size = mysql_num_rows($result) or 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.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜