2017-03-22 153 views
-1
<?php 
    include "head-main.php"; 
    include "conn.php"; 

    if(isset($_GET)){ $var = $_GET["cat"]; } 
    $data = mysql_query('SELECT kampword FROM data_dict where category=$var'); 

    $retval = mysql_query($data, $conn); 

    if(! $retval) { 
    die('Could not get data: ' . mysql_error()); 
    } 

    while($row = mysql_fetch_assoc($retval)) { 
    echo "World :{$row['kampword']} <br> ". 
    "--------------------------------<br>"; 
    } 

    ?> 

我是編程新手,我無法追查我的代碼有什麼問題,你能幫助我嗎?我試圖打印在我的DBSQL查詢錯誤查詢空

一些數據,我總是得到這個錯誤 「無法獲取數據:查詢是空的」

+3

僅供參考,[你不應該使用'在新的代碼mysql_ *'功能]( http://stackoverflow.com/questions/12859942/)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[紅盒](http://php.net/manual/en/function.mysql-connect.php)?學習[*準備的語句*](https://en.wikipedia.org/wiki/Prepared_statement),並使用[PDO](http://php.net/pdo)或[MySQLi](http:// php.net/mysqli) - [這篇文章](http://php.net/manual/en/mysqlinfo.api.choosing.php)將幫助你決定哪一個最適合你。 –

+3

你的腳本是在風險[SQL注入攻擊(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)有一個看看發生了什麼事[小鮑比表](http://bobby-tables.com/)即使[如果你逃避投入,它不安全!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around -mysql-real-escape-string)使用[prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)。 –

+0

是'$ var'一個字符串?它有價值嗎? –

回答

0

要調用兩次沒什麼mysql_query功能。嘗試使用,以取代線:

<?php 
if(isset($_GET["cat"])) { 
    $var = $_GET["cat"]; 
    $retval = mysql_query('SELECT kampword FROM data_dict where category=$var'); 
}else { 
    $retval = false; 
} 
+0

錯誤不見了,但它仍然不會對我的數據庫獲取數據 –

0
$data = mysql_query("SELECT kampword FROM data_dict where category=$var"); 

$data = mysql_query('SELECT kampword FROM data_dict where category="'.$var.'"'); 
0

嘗試

if(isset($_GET["cat"])){ $var = $_GET["cat"]; }