2016-04-08 75 views
-1

這是我的腳本,它顯示一個空白,我不知道問題是什麼。幫幫我!!!! #NEW到PHPPHP腳本顯示空白頁

<?php include "connection.php"; 
    // Get the ID from URL. 
    if(isset($_GET['id'])); 
    $id = $_GET['id']; 
    $query="SELECT * FROM module WHERE id= '$id'"; 
    $result= mysqli_query($m, $query); 
    while ($row = mysqli_fetch_array($result)){ 
    $title=$row['title']; 
    $level=$row['level']; 
    $credits=$row['credits']; 
    $school=$row['school']; 
echo $title. " " . $level. " " . $credits. "<br />"; 
}  
?> 

錯誤:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE id= 'Careers'' at line 1

+0

添加'的ini_set( 'display_errors設置',1);函數ini_set( 'display_startup_errors',1);使用error_reporting(E_ALL);'你的代碼的頂部,並告訴我們,如果有被報告的任何錯誤。另外,'$ _GET'的內容是什麼。 – apokryfos

+0

你能確保connection.php工作正常嗎? –

+0

你是對的!我刪除它,以避免其他人做這個msitake – Delphine

回答

5

一個明顯的這裏的問題是,你已經包括了字符「語句的結束」是一個分號。

分號(如果不是拼寫錯誤)就是這樣做的,「結束」聲明。

if(isset($_GET['id'])); 
        ^right there. 

這應該是一個括號{它來代替,而作爲閱讀:

if(isset($_GET['id'])){ 

,並應該有一個右括號}爲它那個條件語句。

旁註:分號在PHP中被認爲是一個有效的字符,如果GET數組有值,它不會給你一個錯誤。

但是,您應該檢查其餘代碼的錯誤。將您的文件(S)的頂部

添加錯誤報告後立即開啓你的PHP代碼 例如<?php error_reporting(E_ALL); ini_set('display_errors', 1);那麼你的代碼的其餘部分,看它是否得到任何東西, 以及or die(mysqli_error($m))mysqli_query()

儘管確實使用MySQLi_ API連接(不同的MySQL API不混合),並且GET數組有一個值。


這是一個重寫,並假設使用MySQLi_ API爲它成功DB連接。

<?php 

    error_reporting(E_ALL); 
    ini_set('display_errors', 1); 

if(isset($_GET['id'])){ 
    $id = $_GET['id']; 
} else{ 
    echo "ID is not set. You need to investigate it."; 
    exit; // This will stop your script, dead in its tracks. 
} 

$query="SELECT * FROM module WHERE id= '$id'"; 
$result= mysqli_query($m, $query) or die(mysqli_error($m)); 

while ($row = mysqli_fetch_array($result)){ 
$title=$row['title']; 
$level=$row['level']; 
$credits=$row['credits']; 
$school=$row['school']; 

    echo $title. " " . $level. " " . $credits. "<br />"; 
}  

參考文獻:


編輯:

從評論摘自:

"this is my query, $query="SELECT id, title module WHERE id= '$id'"; – user5579012 38 mins ago"

Link to that comment...

那不是發表在你原來的問題。

您發佈了SELECT * FROM module WHERE id= '$id'

這裏有一個語法錯誤,在title之後是一個缺失的逗號。

它應閱讀:

$query="SELECT id, title, module WHERE id= '$id'"; 

所有列需要用逗號分隔,但不是最後一個是module這裏。

+0

發佈的錯誤不支持該問題,與我給出的答案不符,以及您爲代碼編寫的代碼以及爲OP編寫的重寫代碼。 –

0

確保一切正常。如果可能,回聲錯誤。

<?php include "connection.php"; 
// Get the ID from URL. 
if(isset($_GET['id'])){ 
$id = $_GET['id']; 
$query="SELECT * FROM module WHERE id= '$id'"; 
$result= mysqli_query($m, $query); 

if($result){ //query is ok 
    if(mysqli_num_rows($result) > 0){//check if a record exists 

     while ($row = mysqli_fetch_array($result)){ 
       $title=$row['title']; 
       $level=$row['level']; 
       $credits=$row['credits']; 
       $school=$row['school']; 
      echo $title. " " . $level. " " . $credits. "<br />"; 
     } 
    }else{ //no result found 
     echo "no results found!"; 
    } 
    }else{ //some error in querying 
    echo mysqli_error($m); 
    } 
} 
?> 
+0

你甚至測試過這個代碼嗎?提示:它永遠不會檢查$結果。 –

+0

使用您提供給我的代碼後,我現在得到此錯誤,您的SQL語法中有錯誤;檢查與您的MariaDB服務器版本相對應的手冊,以便在第1行的'WHERE id ='Careers''附近使用正確的語法。 – user5579012

+0

是的,我測試了它,並且返回了沒有找到的結果 – user5579012