从MySQL获取数据时出现未定义的变量错误

I get an error when fetching data from a MySQL database using PHP.

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

My dropdown list coding:

<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>

Here is displaying data:

<?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))
{
    //-----
}

Can anyone help me with this?

At the end of the error you see:

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

This leads me to believe that your query is returning nothing so $result is being defined as null. To check this I would check to see if $result is null before you try the while loop.

Replace your code with your code, then hopefully it will work fine.

<?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))
     {
         //-----
     }
}