PHP-mysql_fetch_array什么都不返回

时间:2014-03-20 09:23:25

标签: php mysql

我试图从mysql显示值但它返回任何空页面。连接正常,但它不从mysql获取数据。我尝试了类似问题的所有答案。但没有任何帮助。有人能帮帮我吗?这是代码

    $con=  mysql_connect($host, $username, $pwd);

    if(!$con)
        die("not connected".  mysql_errno());

    echo(Connected);

    mysql_select_db("info",$con);

    $query="select * from people";

    $result=  mysql_query($query,$con) or die(mysql_error());

    while($row = mysql_fetch_array($result))
    {
        echo $row['id']. " - ". $row['people_name'];
        echo "<br />";
    }

3 个答案:

答案 0 :(得分:0)

尝试检查您的db用户,密码是否正确!我测试了上面的代码:

<?php $con=mysqli_connect("localhost","root","","test"); // Check connection 
if (mysqli_connect_errno()){ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
$result = mysqli_query($con,"SELECT * FROM people"); 

while($row = mysqli_fetch_array($result)) { 
    echo $row['id'] . " -- " . $row['people_name']; echo "<br>"; 
}
?> 

并毫无错误地给我结果:10 -- JOHN 11 -- PRADEEP

我只需将mysql_connect更改为mysqli_connect添加$con= mysql_connect($host, $username, $pwd);一个dbname。并且$con成为$con= mysqli_connect($host, $username, $pwd,$dbname);我使用mysqli_query代替mysql_query。这是mysql vs mysqli in php的stackQuestion,它可以解释你的区别。

答案 1 :(得分:0)

检查这个

     <?php
        $con=mysqli_connect("hostname","username","password","info");
        // Check connection
        if (mysqli_connect_errno())
          {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }

        $result = mysqli_query($con,"SELECT * FROM people");

        while($row = mysqli_fetch_array($result))
          {
          echo $row['id'] . " " . $row['people_name'];
          echo "<br>";
          }


        ?>

 OR

 <?php
            $con=mysqli_connect("hostname","username","password");
            // Check connection
            if ($con)
              {
              echo "connected to db";
              }
            else
            {           
               echo "not connected to db";
           }
          $db_selected = mysql_select_db("info", $con);

          if (!$db_selected)
              {
           die ("Can\'t use info: " . mysql_error());
             }

            $result = mysqli_query("SELECT * FROM people");

            while($row = mysqli_fetch_array($result))
              {
              echo $row['id'] . " " . $row['people_name'];
              echo "<br>";
              }


            ?>

答案 2 :(得分:0)

试试这个

    <?php
 $con=  mysql_connect('hostname', 'username', 'password');

    if(!$con)
        die("not connected".  mysql_errno());

    echo("Connected");

    mysql_select_db("test",$con);

    $query="select * from tabale_name";

    $result=  mysql_query($query,$con) or die(mysql_error());

    while($row = mysql_fetch_array($result))
    {
        echo $row['id']. " - ". $row['name'];
        echo "<br />";
    }
?>