doulai2025 2012-10-01 11:17
浏览 16
已采纳

否则阻止不执行

I have a php script, with if and else blocks. Both contain echo statements only. The if block executes perfectly but when the else block executes nothing at all is printed to screen (not even the echo statements OUTSIDE the blocks). I have tried many things such as capitalising the ELSE statement, checking all the braces are there, double and triple checking the syntax to no avail. Please help.

<?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?'; 

?>
  • 写回答

1条回答 默认 最新

  • dounong5373 2012-10-01 11:20
    关注

    You will have an error in

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

    Since the number of results is zero, your script will print a warning. Depending on your configuration, it's possible you see a white-page in stead of the warning.

    Please note the or die(..) part.

    Since the first part failed (fetching an empty result), the script will die and stop. No code lower than this line will be executed, so no logs, no 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序