-3

Hey i'm new to php/mysql and i'm trying to execute a very simple php code that will display the contents of the table. I feel like the code is perfect, and i get no error messages, but for some reason it doesn't work. I know you guys hate debugging questions like this, but if you could help i'd appreciate it. here's the php.

<?php 

 $conn=mysql_connect("localhost","demo","abc") or die(mysql_error()); 
  mysql_select_db("practice");

  $sql="SELECT*FROM contact"; 

  $result=mysql_query($sql,$conn) or die(mysql_error()); 

   while($row=mysql_fetch_assoc($result)){ 
     foreach($row as $name => $value){ 
       print "$name: $value <br>\n";
              } //end foreach
                 print "<br /> \n";
              } //end while



                  ?>
  • `I feel like the code is perfect` you're using mysql_* how is it perfect. – Dave Chen Jul 24 '13 at 18:16
  • 1
    Use mysqli its slightly more perfect – tenstar Jul 24 '13 at 18:16
  • Well your SQL statements has syntax errors in it. There is no way you are not getting any errors. – thatidiotguy Jul 24 '13 at 18:16
  • What debugging have you done already? Are you connecting to the database OK? Is there anything in `contact`? Does your database have rights to that table? Where does the code stop working - does it go into the the `while` loop? The `foreach` loop? What is the value of `$row`? – andrewsi Jul 24 '13 at 18:17
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – revoua Jul 24 '13 at 18:18
  • 1
    http://www.phptherightway.com/ – JimiDini Jul 24 '13 at 18:18
  • `SQL statements has syntax errors` Using `SELECT*FROM contact` works fine on my machine. – Dave Chen Jul 24 '13 at 18:19
  • Favour PDO over MySQLi http://net.tutsplus.com/tutorials/php/pdo-vs-mysqli-which-should-you-use/ – Alfie Jul 24 '13 at 18:23

2 Answers2

1

You're using the old mysql library which is a no no

Get comfy with the Mysqli Extension for all your database access needs. I'll even refactor this a bit for you.

$conn = new Mysqli('localhost', 'demo', 'abc', 'practice');

$sql = "SELECT*FROM contact";

$results = $conn->query($sql);

while($row = $results->fetch_assoc())
{
  var_dump($row);
}

edit: JimiDini posted a link that you should definitely read. http://phptherightway.com/

castis
  • 8,154
  • 4
  • 41
  • 63
0

try this

     // Report simple running errors
    error_reporting(E_ERROR | E_WARNING | E_PARSE); 

   // or if you want to enable all PHP error reports, use this code below and comment out the one above
   //error_reporting(-1);

  $dbhost = 'localhost';// Server name (usually localhost)
  $dbuser = 'user';// SQL Username (Make sure the user has access to the database!).
  $dbpass = 'password';// SQL Password.
  $dbase = 'db name';// SQL Database Name.

  //connection to the database
   $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die(mysql_error()); 


    $sql = "SELECT * FROM `contact`"; 

    $result = mysql_query($sql); 

    while($row = mysql_fetch_assoc($result)){ 
      print_r($row);
    }

but you should use mysqli in the future

srakl
  • 2,565
  • 2
  • 21
  • 32
  • yeah when i run that code i get nothing. Not an error message, just a blank screen. I guess I should switch to mysqli.... i think the issue must be with the database/user configuration, not the code. Thanks for the help though! – user2458161 Jul 24 '13 at 18:30
  • does your code connect to db? try and echo the $sql statement. does it go that far? are your display error messages enabled in php.ini? – srakl Jul 24 '13 at 18:32
  • no it doesn't echo the $sql statement. where do I find the php.ini folder. I used xampp to install everything – user2458161 Jul 24 '13 at 18:40
  • are you using xampp on windows? if you are, it should be in C:/xampp/php/php.ini – srakl Jul 24 '13 at 18:42
  • updated my code above. that 1st line should show you your error messages without you editing your php.ini – srakl Jul 24 '13 at 18:45
  • yes i'm running xampp on windows. And it looks like you were right, apparently i don't even have the right tools to view the php.ini document. I'm assuming that must be the issue. what do i need to install? – user2458161 Jul 24 '13 at 18:47
  • just run the error reporting function by itself, like so: error_reporting(); – user2458161 Jul 24 '13 at 18:52
  • just add it on top of your page. should show errors if you have any – srakl Jul 24 '13 at 18:53
  • just ran it, the screen is still blank – user2458161 Jul 24 '13 at 18:56
  • alright i'll figure this out eventually, thanks for your help – user2458161 Jul 24 '13 at 18:58
  • i just tested the code above, works fine on my side. also added some comments – srakl Jul 24 '13 at 19:07