如果条件[关闭],PHP回显将不起作用

问题描述 投票:0回答:1

我不知道怎么了,但以下内容。

在“如果”条件之后,回声不起作用。谁能告诉我如何解决此问题?

    $select_query = mysql_query(some query);
    $fetch_pass = mysql_fetch_array($select_query);
    $getpass = $fetch_pass['field_value'];
    echo $getpass; //Here it is working
    if($_POST['userpass'] == $getpass)
    {
        echo $getpass; // Here it is working
        $select_storename = mysql_query("Some Query");
        $fetch_storename = mysql_fetch_array($select_storename);
        $getstorename = $fetch_storename['field_value'];
echo $getpass; // Here if $getstorename has output then getpass is working else it is not working
            $select_brandname = mysql_query("Some Query"); // This query is never executing
            $fetch_brandname = mysql_fetch_array($select_brandname);  // This is never working
            $getbrandname = $fetch_brandname['field_value'];  // This is never working
echo $gettime; // Here it is not working
// None of these if conditions are working.
                if($getstorename != null) {
                    header("location:http://localhost/stores/");
                }
                if($getbrandname != null) {
                    header("location:http://localhost/brands/");
                }               
    }

[其概念是$select_query将获取密码,在第一个“ if”条件下,我们检查密码是否正确,然后有2个查询$select_storename$select_brandname,只有第一个是工作。如果我先将顺序从$select_brandname更改为只有它有效,则第一个查询有效,第二个查询无效,并且“ if”条件也无法正常工作。

更新1

我认为这是由于查询失败导致的,我如何忽略并绕过失败的查询?

php
1个回答
2
投票

您的代码有以下问题:

  • 没有测试mysql_query()的返回值。查询可能会失败。
  • 没有测试mysql_query()的返回值。查询可能成功,并返回0行。
  • 在标题之前输出。必须将mysql_fetch_array()称为before,除非打开了输出缓冲,否则脚本将产生任何输出。 (并且依赖于输出缓冲不是一个可靠的设计。)

还请注意,不建议使用MySQL扩展(并且可能无法维护)。您应该使用mysql_fetch_array()header()

您的代码应如下所示:[[something:

header()
有关代码的一些注释:

    该代码使用了某些人感到不舒服的提前退出。另一种方法是使用复杂的MySQLi梯形图。
  • 显示错误页面后代码退出。如果这在函数内部,则返回可能更合适。
  • 如果您创建单独的错误页面并将其PDO放置在适当的位置,则可以大大简化代码的读取。
  • 我认为,即使数据库之后会清理,在使用完数据库资源后也应始终调用$select_query = mysql_query(some query); if (!$select_query) { /* Query failed. Display error page. */ exit; } $fetch_pass = mysql_fetch_array($select_query); if (!count ($fetch_pass)) { /* Password incorrect. Display error page. */ exit; } $getpass = $fetch_pass['field_value']; mysql_free_result ($fetch_pass); if($_POST['userpass'] !== $getpass) { /* Password incorrect. Display error page. */ exit; } $getstorename = null; $getbrandname = null; $select_storename = mysql_query ("Some Query"); if ($select_storename) { $fetch_storename = mysql_fetch_array ($select_storename); if (count ($fetch_storename)) { $getstorename = $fetch_storename['field_value']; } mysql_free_result ($select_storename); } $select_brandname = mysql_query ("Some Query"); if ($select_brandname) { $fetch_brandname = mysql_fetch_array ($select_brandname); if (count ($fetch_brandname)) { $getbrandname = $fetch_brandname['field_value']; } mysql_free_result ($select_brandname); } if ($getstorename != null) { header("location:http://localhost/stores/"); } else if ($getbrandname != null) { header ("location:http://localhost/brands/"); } else { /* None found. Display error page or some default page. */ } 。如果没有别的,它会鼓励您编写更简洁的代码。
  • 和,永远不要以明文形式存储密码。使用if...else或类似方法对密码进行加密,并仅存储和比较加密的密码。有关更多信息,请参见require()
© www.soinside.com 2019 - 2024. All rights reserved.