PHP回显显示空白屏幕的变量

时间:2013-03-20 21:39:29

标签: php mysql

所以我有以下PHP代码

<?php 

error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

    $poscote = $_POST['postcode'];

    mysql_real_escape_string($poscote);

    //! Checks for direct access to page

    if (empty($_POST)) {
        header('location:index.php?nothingentered');
        die();
    }


    require_once('../Connections/PropSuite.php'); 


    mysql_select_db($database_Takeaway, $Takeaway);
    $query_PC = "SELECT * FROM Postcodes WHERE pc = '$postcode'";
    $PC = mysql_query($query_PC, $Takeaway) or die(mysql_error());
    $row_PC = mysql_fetch_assoc($PC);

    if( mysql_errno() != 0){
     // mysql error
     // note: message like this should never appear to user, should be only stored in log
     echo "Mysql error: " . htmlspecialchars( mysql_error());
     die();
     }

    else {

    echo $row_PC['oc'];

    }

?>

这是使用以下代码处理表单

<form action="search_postcode.php" method="post">
        <input type="text" name="postcode" />
        <button>Go</button>
    </form>

奇怪的是它只是显示一个空白的屏幕,没有错误,没有我检查过,似乎无法找到解决方案。

非常感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

由于您的$postcode变量未定义,因此您在数据库中查找pc是错误消息的行。

该查询可以很好地完成而没有错误,但它可能产生0行,因此您没有错误,也没有结果。在这种情况下,你什么都不输出,所以你会看到一个空白的屏幕。

你可能想要:

$postcode = mysql_real_escape_string($poscote);

而不是:

mysql_real_escape_string($poscote);

并将其放在数据库连接部分下面。

此外,您应该切换到PDO(或mysqli)和预处理语句以避免SQL注入问题,并且因为mysql_*函数已被弃用。请注意,当您没有打开数据库连接时,mysql_real_escape_string不执行任何操作(除了删除变量的内容...)。

答案 1 :(得分:1)

除了其他答案,并且未提及您应该使用PDOmysqli,您可能会遇到字符编码问题。尝试做这样的事情:

define('DB_CHARSET', 'ISO-8859-1');
define('REPLACE_FLAGS', ENT_COMPAT | ENT_XHTML);

...

echo "Mysql error: " .  htmlentities(mysql_error(), REPLACE_FLAGS, DB_CHARSET);

DB_CHARSET的值替换为您使用的数据库编码。如果您尝试将htmlentities()与无效字符一起使用,则会产生一个空字符串。

答案 2 :(得分:0)

从php.net开始,要使用ini_set启用php错误,你必须这样做

ini_set('display_errors', '1')

这取自link