Jump to content

Displaying mysql records


slj90

Recommended Posts

Hello,

 

I want to display data in a table I have using the following code. It works using the table and field names in the code I have displayed but when I change it to another table name and attributes in another table i get the error.. Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in H:\xampp\xampp\htdocs\displayCustomer.php on line 14

 

Code:

<?php
// (1) Open the database connection – always use localhost
$connection = mysql_connect("localhost", "root");

// (2) Select your  database id
mysql_select_db("animals", $connection);

// (3) Create a query 
   	$query = "SELECT * FROM symbols"; 
   
   	// (4) Run the query on the table through the connection 
   	$result = mysql_query ($query); 

  	// (5) While there are still rows in the result set, fetch the current 
   	while ($row = mysql_fetch_array($result)) 
   	{ 
     	// (6) Print out each element in $row, that is, print the values of 
     	  print $row["animal"]; //print the animal field 
     	  print "=="; //print 2 equals signs 
     	  print  $row["country"]. "<br>";  
              //print the country field and a new line 
  	 }
?>

 

 

And here is the the same code with the table info I want to use...

<?php
// (1) Open the database connection – always use localhost
$connection = mysql_connect("localhost", "root");

// (2) Select your  database id
mysql_select_db("customer", $connection);

// (3) Create a query 
   	$query = "SELECT * FROM symbols"; 
   
   	// (4) Run the query on the table through the connection 
   	$result = mysql_query ($query); 
  	// (5) While there are still rows in the result set, fetch the current 
   	while ($row = mysql_fetch_array($result)) 
   	{ 
     	// (6) Print out each element in $row, that is, print the values of 
     	  print $row["firstnames"]; //print the animal field 
     	  print "=="; //print 2 equals signs 
     	  print  $row["surname"]. "<br>";  
              //print the country field and a new line 
  	 }
?>

 

Thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/184723-displaying-mysql-records/
Share on other sites

I guess the first question I have is the field names correct? You selecting from the same table in the second code as the first but the field names are different. In the first table your wanting data from fields called animal and country and in the second code your wanting fields firstname and surname.

 

an easy way to use field name as variables is to add code like this in your while statement

 

while ($row = mysql_fetch_array($result)) 
      { 
             extract($row);
             // Rest code here
       }

 

now with extract($row) function you dont have to write out $row['firstname'] you can just type $firstname and it knows its the firstname field.

 

As for the rest of your code if its the same as the first in the second except for the db I would double check your field names it seems like you might not have the names right.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.