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

时间:2015-10-12 05:15:54

标签: php html mysql

使用PHP从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))
{
    //-----
}

任何人都可以帮我吗?

2 个答案:

答案 0 :(得分:0)

在错误结束时,您会看到:

  

mysqli_fetch_array()要求参数1为mysqli_result,在第20行的C:\ wamp \ www \ mbdb \ Biomarkerresult1.php中给出null

这让我相信你的查询没有返回任何内容,所以$ result被定义为null。要检查这一点,我会在尝试while循环之前检查$ result是否为null。

答案 1 :(得分: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))
     {
         //-----
     }
}