0

我有一个 php 脚本,带有 if 和 else 块。两者都只包含 echo 语句。if 块执行得很好,但是当 else 块执行时什么都没有打印到屏幕上(甚至不在块之外的 echo 语句)。我尝试了很多方法,例如将 ELSE 语句大写、检查所有大括号是否存在、双重和三重检查语法均无济于事。请帮忙。

<?php

// Inialize session
session_start();

// Include database connection settings
include('config.inc');

$usr = mysql_real_escape_string($_POST['****ID']);

// Retrieve email address and message from database according to user's input
$query = "SELECT * FROM users WHERE username = '$usr'";

$result = mysql_query($query) or die(mysql_error());

// Put the id numbers in array $row

$row = mysql_fetch_array($result) or die(mysql_error());

// Test for ID match

if (mysql_num_rows($result) == 1)
{
echo 'The email address of ';
echo $usr;
echo ' is: ';
echo $row['email'];
echo '<p>Message: ';
echo $row['firstname'];
}
else
{
echo 'Sorry it appears that ';
echo '$usr';
echo ' has not registered yet. Why dont you tell him about ****Jacker?'; 
}
// Executing outside  if..else blocks for testing
echo 'Sorry, it appears that ';
echo '$usr';
echo ' has not registered yet. Why dont you tell him about ****Jacker?'; 

?>
4

1 回答 1

5

你会有一个错误

$row = mysql_fetch_array($result) or die(mysql_error());

由于结果数为零,您的脚本将打印警告。根据您的配置,您可能会看到白页而不是警告。

请注意 or die(..) 部分。

由于第一部分失败(获取空结果),脚本将终止并停止。不会执行低于此行的代码,因此没有日志,没有 if,...

Best solution is to put the fetch_array inside the if (mysql_num_rows(..) == 1 statement, so we are sure there is at least 1 user to fetch

于 2012-10-01T11:20:47.870 回答