为什么PHP代码没有回应

时间:2014-10-08 11:49:54

标签: php mysql echo

我一直在阅读许多不同的代码,我很困惑为什么我的代码底部的$ echo行[0]没有返回任何内容。

$dbhost = 'localhost';

$uname = $_POST["uname"];

//***create connection object
$connection = mysql_connect($dbhost, "bc572fsdf", "abcdfsds") or die(mysql_error());

$dbname = "bc57db";
mysql_select_db($dbname) or die(mysql_error());

//***select a random security question

//*** need this to import session variables
session_start();

echo ($_SESSION["ValidUser"] . "\n");

$rq       = array('q1', 'q2', 'q3');
$rand_key = array_rand($rq, 1);
echo $rq[$rand_key];

$question = $rq[$rand_key];

$qtoanswer = mysql_query("select '$question' from users where uname = '$uname'");
if (!$qtoanswer) {
    echo "Could not run query:" . mysql_error();
    exit;
}

echo $qtoanswer;
$row = mysql_fetch_row($qtoanswer);

echo $row[0];

?>

2 个答案:

答案 0 :(得分:1)

错误就在这一行:

$qtoanswer = mysql_query("select '$question' from users where uname = '$uname'");

您应该对列名使用严重标记,例如:

$qtoanswer = mysql_query("select `$question` from users where uname = '$uname'");
                                 ^         ^

另外,你应该使用MySQLi / PDO,这样你就可以准备这个,或者至少,逃避$uname

答案 1 :(得分:0)

因为你无法回显数组....尝试var_dump或print_r,或者使用循环:

foreach($row[] as $result) {
    echo $row[], '<br>';
}