0

I've trying to display values from mysql but it return any empty page. The connection is fine but it does not fetch the data from mysql. I tried all the answers from the similar questions asked. But nothing helped. Can somebody please help me? This is the code

    $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 />";
    }
nanobash
  • 5,419
  • 7
  • 38
  • 56
Pradeep
  • 3
  • 3

3 Answers3

0

Try to check if your db user,password are correct! I test the code above :

<?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>"; 
}
?> 

and give me the result without error: 10 -- JOHN 11 -- PRADEEP

I just change mysql_connect to mysqli_connect add in $con= mysql_connect($host, $username, $pwd); a dbname. and $con become $con= mysqli_connect($host, $username, $pwd,$dbname); I use mysqli_query instead of mysql_query. Here is a stackQuestion for the mysql vs mysqli in php which can explain you the difference.

Community
  • 1
  • 1
Milaci
  • 515
  • 2
  • 7
  • 24
  • for me, doing `select *` did'nt show the result! I change `select column one by one` and was all ok. Perfectly show the result – Milaci Mar 20 '14 at 09:39
  • Blindly fixing errors is not right. You should know what the error is before fixing it and you fixed it without knowing what is wrong. **The * is not wrong and it does not cause an error**. – Ali Mar 20 '14 at 09:42
  • Didn't throw any error, but didn't show any result! I have the same situation when i developed in PHP. – Milaci Mar 20 '14 at 09:44
  • Yes. It has two records. – Pradeep Mar 20 '14 at 10:06
  • @Pradeep have you fix it? – Milaci Mar 21 '14 at 12:28
  • @milaci no... still cant select the db – Pradeep Mar 23 '14 at 10:37
  • I don't understand why! The example above post from others is OK. I check this [mysql_select_db](http://it2.php.net/mysql_select_db) for any error but seems ok. A last try is to do a `select * from info.people`, because you don't see your db. Doing that if you connect to MySQL you should be able to execute the query above. Try that and let me now. – Milaci Mar 23 '14 at 11:33
  • still getting the same error.. also the mysqli_error() does not display the error message.. – Pradeep Mar 24 '14 at 07:59
  • Try to check if your db user,password are correct! I test the code above : `"; } ?>` and give me the result without error: `10 -- JOHN 11 -- PRADEEP`. – Milaci Mar 24 '14 at 20:16
  • @Milaci Hey!!!!! it worked... Thanx a lot... i dont see any change in the code.. but how did it work??? – Pradeep Mar 25 '14 at 08:26
  • I just change `mysql_connect` to `mysqli_connect` add in `$con= mysql_connect($host, $username, $pwd);` a `dbname`. and `$con` become `$con= mysqli_connect($host, $username, $pwd,$dbname);`. I use `mysqli_query` instead of `mysql_query`. Here is a stackQuestion for the [mysql vs mysqli in php](http://stackoverflow.com/questions/548986/mysql-vs-mysqli-in-php) which can explain you the difference. – Milaci Mar 25 '14 at 22:35
  • @Pradeep Nothing, it's a pleasure to help the community of stackoverflow but the legend says: if the answer is correct, vote it. So the other can see immediately the solution ;). – Milaci Mar 26 '14 at 10:58
0

check this

     <?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>";
              }


            ?>
codelover
  • 317
  • 1
  • 11
0

Try this

    <?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 />";
    }
?>
Puvanarajan
  • 2,786
  • 6
  • 26
  • 37
  • ,mysql is depriciating..dont appriciae others to use that ...Also your select query should be `select * from tablename` and not `dbname` – codelover Mar 20 '14 at 09:56
  • Sorry for that mistake. Anyway I create a test db and I test this code. It is working properly. – Puvanarajan Mar 20 '14 at 09:59