获取 SQL 语法错误


Getting a SQL syntax error

我正在尝试从我的php代码中的应用程序中获取这两个值。 但在这样做之前,我正在尝试通过 URL 进行检查。但我的问题是,如果给值手册,我会得到正确的输出,但是当我通过传递值来检查它时,我得到了语法错误。任何人都可以帮助我解决这个问题。

<?php
$hostname_localhost ="localhost";
$database_localhost ="mobiledb";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);
 $response = array();
mysql_select_db($database_localhost, $localhost);
$day = $_POST['day'];
$Q = $_POST['Qno'];

    // get a product from products table
$result = mysql_query("SELECT $Q FROM `Questions` WHERE `day`='$day'") or die(mysql_error()); 
    //echo $result;
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["question"] = array();
    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $product = array();

        $product["question".$i] = $row["$Q"];
     $i = $i + 1;
     // push single product into final response array
        array_push($response["question"], $product);

    }

    // success
    $response["success"] = 1;
    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No users found";
    // echo no users JSON
    echo json_encode($response);
} 

?>

我正在尝试检查网址

我假设你的意思是你正在尝试访问网址:

http://localhost/yoursite/yourpage?Qno=5&day=thing

在这种情况下,这些变量将作为$_GET['Qno']$_GET['day']进行访问。

您可以使用 $_REQUEST['Qno']$_REQUEST['day'] 以两种方式接收变量。当然,您的应用程序有很多安全漏洞,我什至不会碰。

我会尝试转义你的价值观。 可能会修复您的错误,还可以保护您免受您应该谷歌和阅读的SQL注入的影响。

$day = mysql_real_escape_string($_GET['day']);
$Q = mysql_real_escape_string($_GET['Qno']);

在此示例中,我们使用 $_GET,因为您尝试直接从 URL 获取值。

此外,我们转义字符串以确保我们不会破坏字符串语法并将坏怪物注入您的数据库!

另外:Mysql_功能已停止使用,您应该停止使用它。阅读这里的大红框:http://php.net/manual/en/function.mysql-query.php