2015-10-12 21 views
0

從PHP使用MySQL數據庫獲取數據時出現錯誤。從MySQL獲取數據時發生未定義的變量錯誤

Undefined variable: result in C:\wamp\www\mbdb\Biomarkerresult1.php on line 20 
mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\mbdb\Biomarkerresult1.php on line 20 

我的下拉列表編碼:

<select name="names" value="name"> 

<option value="Biomarker">Select a Biomarker</option> 

<option value="Diagnostic">Diagnostic</option> 

<option value="Prognsotic">Prognostic</option> 

<option value="Predictive">Predictive</option> 

下面是顯示數據:

<?php 
$con=mysqli_connect('localhost','root','','mbdb'); 
if(mysqli_errno($con)) 
{ 
    echo "Can't Connect to mySQL:".mysqli_connect_error(); 
} 

if(isset($_POST['names'])) 
{ 
    $name = $_POST['names']; 
    $fetch="SELECT * FROM metabolites WHERE Biomarker_Category = '".$name."'"; 
    $result = mysqli_query($con,$fetch); 
} 
while($row=mysqli_fetch_array($result)) 
{ 
    //----- 
} 

誰能幫助我?

+0

「Biomarker_Category」是否正確。 –

+2

你的'$ result'變量是空的。首先檢查你對phpmyadmin的查詢。 –

+0

把'while'放在'if'裏 –

回答

0

在錯誤的端部看到:

mysqli_fetch_array()預計參數1被mysqli_result,在C空給出:\瓦帕\ WWW \ MBDB \ Biomarkerresult1.php上線20

這使我相信你的查詢沒有返回任何內容,所以$ result被定義爲null。要檢查這一點,我會檢查$ result是否爲null,然後再嘗試while循環。

0

用你的代碼替換你的代碼,然後希望它能正常工作。

<?php 
$con=mysqli_connect('localhost','root','','mbdb'); 
if(mysqli_errno($con)) 
{ 
    echo "Can't Connect to mySQL:".mysqli_connect_error(); 
} 

if(isset($_POST['names'])) 
{ 
    $name = $_POST['names']; 
    $fetch="SELECT * FROM metabolites WHERE Biomarker_Category = '".$name."'"; 
    $result = mysqli_query($con,$fetch);//Seems to be your your posting the data, or u'r using get and post for the same form 

    while($row=mysqli_fetch_array($result)) 
    { 
     //----- 
    } 
} 
+0

謝謝Jitendra庫馬爾。但我沒有得到什麼變化,我應該在我的編碼 –

+0

你必須在if語句中寫while循環。 –

+0

我已編輯該部分。但仍然沒有得到輸出頁面中的結果表。 –