Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 />";
    }
share|improve this question
5  
Oligatory "mysql is deprecated, use mysqli_* functions" comment. php.net/manual/en/book.mysqli.php –  pduersteler Mar 20 at 9:25
    
What is the error ? Actually echo(Connected); is an error ! –  crypticous Mar 20 at 9:25
    
I tried using mysqli too. It did not help. –  Pradeep Mar 20 at 9:26
    
I dont get any error. I just get a page saying Connected –  Pradeep Mar 20 at 9:26
    
Is this really saying connected? No error for undefined constant? –  Royal Bg Mar 20 at 9:27
show 6 more comments

3 Answers 3

up vote 0 down vote accepted

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.

share|improve this answer
1  
* should work perfectly whether he used it or used the field names.. –  Ali Mar 20 at 9:36
    
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 at 9: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 at 9: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 at 9:44
    
@Pradeep : Are you sure the people table isn't empty???? –  ASNAOUI Ayoub Mar 20 at 9:46
show 10 more comments

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 />";
    }
?>
share|improve this answer
    
,mysql is depriciating..dont appriciae others to use that ...Also your select query should be select * from tablename and not dbname –  codelover Mar 20 at 9:56
    
Sorry for that mistake. Anyway I create a test db and I test this code. It is working properly. –  PUVAN Mar 20 at 9:59
add comment

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


            ?>
share|improve this answer
    
I get only an empty page. –  Pradeep Mar 20 at 9:40
    
@Pradeep ,I have given 2 codes..just check both of them –  codelover Mar 20 at 9:44
    
This is what i get. connected to dbCan\'t use test_db : –  Pradeep Mar 20 at 9:49
    
@Pradeep ,There is problem in your db probably..did u use use yourdbname after creating the db?. –  codelover Mar 20 at 9:52
    
Yes. My db name is info and i tried using it. –  Pradeep Mar 20 at 9:59
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.