how to display individual fields from MySQL query in PHP?
Hi there,
I'm relatively new to PHP, although have coded for years in other languages...
I am looking to take the results from a mysql query and display them as needed. However I have created the table, formatted it and completely fail to get any data to display in it!
When I print the results direct tot he screen all info is there however when I try to echo the individual fields into the table all I get is blank space...
Sample code below... please can you point out what I'm doing wrong? It's bound to be something real obvious that I just can't see.
Many thanks
the offending code:
------------------------------
//display the results
echo '
';
echo $row['name'];
echo '
More>>
Contact Name:
Phone:
When I display each field without the table formatting it works fine:
---------------------------------------------------------------------------------------
echo $row["name"];
echo '-';
echo $row["contact"];
echo '-';
echo $row["quals"];
echo '-';
echo $row["phone"];
echo '-';
echo $row["email"];
echo '-';
echo $row["web"];
echo '-';
echo $row["address"];
these are just sub-set of data for examples....
kazimmerman posted this at 12:25 — 19th August 2007.
He has: 698 posts
Joined: Jul 2005
There could be other problems, but the main one I am seeing is the code here:
echo ' <div class="main">
...
Any double quotes (" ") within an echo statement must be escaped.
e.g.
echo "<div class=\"main\">
...
Without escaping them, your code will not display properly.
Kurtis
Fiery Phoenix posted this at 12:58 — 19th August 2007.
They have: 3 posts
Joined: Aug 2007
hmmm....
I went through and replaced the " with \" as suggested in the previous reply... but all this did was remove all formatting completely!
I have reverted to the original " formatting for now...
It's the formatting of the table that is the issue - I cannot see the field results when I echo them.
Specifically :
------------------
[="Times New Roman"]echo $row['name'];[/]
does nothing.
Any suggestions?
Greg K posted this at 18:30 — 19th August 2007.
He has: 2,145 posts
Joined: Nov 2003
This is not the case. It depends on what style quotes you are using for the echo. You must escape any quotes that are the same type that surround the string (and this is for anything with a literal string, not just echo)
GOOD:
echo 'This is the "real" deal!';
echo "Can't think of good example";
echo "This is the \"real\" deal!";
echo 'Can\'t think of a good example';
BAD:
echo "This is the "real" deal!";
echo 'Can't think of a good example";
So the examples in the given code are just fine (other than the missing ending quote and semicolon, but this is probably becasue there was more to the echo statement jsut not posted here)
In fact, I just took that code, added the ending quote, and at the start of it defined the array using
$row = array('name' => 'Greg');
and ran that sniplet, and it worked just fine giving me HTML code.
(also, before someone suggests it, the fact that in your non working code you use $row['name'] and in the test you used $row["name"] makes no difference (again, just tested that to make sure)
So this leads me to beleive one of two things, your CSS for the table is messed up, and it is just not visible (check this by doing VEIW SOURCE, or in Firefox, choose VIEW->Page Style->No Style) or at this point in your code, $row does not actually contain what you think it should. You didn't mention if your test of was a direct replace of the other code or jsut done elsewhere, so I suggest the following:
Right before the line //display the results put this code and view the source:
<?php
echo \"<pre><tt>\n\";
print_r($row);
die('</tt></pre>');
?>
When you view your source code the output of this will be the very end. Check to make sure that vaules you are expecting are in the array this way. This is a simple way to check for lost array values. (the
kazimmerman posted this at 21:28 — 19th August 2007.
He has: 698 posts
Joined: Jul 2005
Ahh, I didn't even pay attention to the beginning quote. I guess since I always like to do " ' ' " order, I just assumed he started with a double quote as well.
Kurtis
kb posted this at 17:18 — 19th August 2007.
He has: 1,380 posts
Joined: Feb 2002
Are you sure you did it correctly? The quotes on the front and end of the statement need to be " WITHOUT a \
Right click, and "View Source". Do you see the data printed into the code somewhere, but just not where expected? If so, fix that.
Your only real issue is the quotes.
kb posted this at 22:19 — 19th August 2007.
He has: 1,380 posts
Joined: Feb 2002
Good call Greg, I just assumed that knowledge of one quote rule would follow another...
Fiery Phoenix posted this at 06:37 — 20th August 2007.
They have: 3 posts
Joined: Aug 2007
Thank you for the tip on checking the source code... why didn't I think of that :*
I spotted the mistake immediately - I was tryin to populate the table outside of the while loop. How stoopid! LOL
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.