slj90 Posted December 11, 2009 Share Posted December 11, 2009 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 More sharing options...
CBStrauss Posted December 11, 2009 Share Posted December 11, 2009 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. Link to comment https://forums.phpfreaks.com/topic/184723-displaying-mysql-records/#findComment-975178 Share on other sites More sharing options...
slj90 Posted December 11, 2009 Author Share Posted December 11, 2009 Hi, the query was using the wrong table.. $query = "SELECT * FROM symbols instead of $query = "SELECT * FROM customer The page loads with no errors now, but only displays the '==' in-between the fields I want it to display Thank you Link to comment https://forums.phpfreaks.com/topic/184723-displaying-mysql-records/#findComment-975179 Share on other sites More sharing options...
CBStrauss Posted December 11, 2009 Share Posted December 11, 2009 well no errors is a good sign but if your field data is not display again make sure you have the names type in your php code as you do in your table field and check to make sure there is actually data in those field in your database. Link to comment https://forums.phpfreaks.com/topic/184723-displaying-mysql-records/#findComment-975192 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.