Display mysql_query results  | |
June 8th, 2003, 04:22 AM
|
#1 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Chattanooga, TN
Posts: 660
| Display mysql_query results
It seems that this should be fairly straight forward but I'm not able to display my results from my SQL query.
Here is the code: PHP Code:
<table width="75%" border="0">
<tr>
<th>Wanted By</th>
<th>Item</th>
<th>Description</th>
<th>Price</th>
<th>Store</th>
<th>Web Address</th>
</tr>
<?php
// Query the database for items table
$sql = "SELECT * FROM items";
$itemList = mysql_query($sql) or die(mysql_error());
// Display results of query
while ($row = mysql_fetch_row($itemList)) {
printf(
"<tr>
<td> %s </td>
<td> %s </td>
<td> %s </td>
<td> %s </td>
<td> %s </td>
<td> %s </td>
</tr>\n",
$itemList[0],
$itemList[1],
$itemList[2],
$itemList[3],
$itemList[4],
$itemList[5]);
}
?>
</table> I get the header, but the results doesn't display. What am I doing wrong, or am I doing anything right. I followed an example I found online, but it was for an older version of PHP and mySQL.
I've been trying to figure this out all day, but haven't been able to.
Thanks,
LK |
| |
June 8th, 2003, 05:08 AM
|
#2 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Chattanooga, TN
Posts: 660
|
Well I finally figured it out.
Original Code PHP Code: while ($row = mysql_fetch_row($itemList)) {
printf(
"<tr>
<td> %s </td>
<td> %s </td>
<td> %s </td>
<td> %s </td>
<td> %s </td>
<td> %s </td>
</tr>\n",
$itemList[0],
$itemList[1],
$itemList[2],
$itemList[3],
$itemList[4],
$itemList[5]);
}
It should be PHP Code: while ($row = mysql_fetch_row($itemList)) {
printf(
"<tr>
<td> %s </td>
<td> %s </td>
<td> %s </td>
<td> %s </td>
<td> %s </td>
<td> %s </td>
</tr>\n",
$row[0],
$row[1],
$row[2],
$row[3],
$row[4],
$row[5]);
}
I had to change $itemList to $row, and that did the trick.
Thanks,
LK |
| |
June 9th, 2003, 01:11 AM
|
#3 (permalink)
| | Banned
Join Date: Oct 2001
Posts: 447
|
[2 cents]
as each:
while*($row*=*mysql_fetch_row($itemList))
gives you a $row array, nest another while loop:
while ($row*=*mysql_fetch_row($itemList))
while ($res = $row.next())
print ("<td>" + $res + "</td>")
ew
ew
[/2 cents] |
| |
June 9th, 2003, 04:25 PM
|
#4 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Chattanooga, TN
Posts: 660
|
Thanks for the help.
I'm fairly novice when it come to programming (mainly self taugh so far, with a few basic classes - not the language). I really need to keep a better eye out for more optimization like the nested loop.
Something like that should be fairly basic, and easy to see, but I just need to learn to see it easier.
Again Thanks,
LK |
| |
June 10th, 2003, 01:47 AM
|
#5 (permalink)
| | Banned
Join Date: Oct 2001
Posts: 447
|
you are welcome.
didya learn anything about 'printf'? |
| |
June 12th, 2003, 10:00 PM
|
#6 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Chattanooga, TN
Posts: 660
|
If your asking what 'printf' does? I personally can't be much help. However, I did find this definition of the difference between echo vs. print vs. printf
echo simply spits out what you want. print() is a function that will return either 0 or 1 based on failure or success. printf is similar but is used for printing formatted output.
LK
__________________
Want a portrait, drawing, mural or any other custom art? Contact my wife Mindy Herman @ -http://www.mindyherman.com/
|
| |
June 13th, 2003, 01:20 AM
|
#7 (permalink)
| | Banned
Join Date: Oct 2001
Posts: 447
| Quote: |
didya learn anything about 'printf'?
| ok,
if echo spits, but no return?
how does the print functions spit and return?
[xtra credit]
how (exactly) does spit work?
how (exactly) does return work?
[/xtra credit] |
| |
June 18th, 2003, 01:12 PM
|
#8 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Chattanooga, TN
Posts: 660
|
Don't know if this is what your wanting or not, but I pulled this from the PHP.net website: print ( string arg )
Outputs arg. Returns TRUE on success or FALSE on failure printf ( string format [, mixed args])
Produces output according to format, which is described in the documentation for sprintf().
Reading the differences at PHP's site, I got a better understanding, I would recommend just reading the PHP pages to understand the differences.
Basiclly I would say print "spits" out whatever you have in quotes and also return either true or false. Now I don't completelly understand when it when it's true or false.
Printf basiclly prints what is in quotes while subsituting '%' variables for variables at the end of the string.
EXAMPLE:
$foo = "example";
printf ("This is an %s.", $foo);
// output: This is an example.
This will replace "%s" with "example". There are other '%' variables. Read the sprintf @ php.net for an explenation.
I don't know if any of this makes since or not, but I tried. I would recommend reading the PHP site for a better explenation.
LK |
| |
June 19th, 2003, 12:52 AM
|
#9 (permalink)
| | Banned
Join Date: Oct 2001
Posts: 447
| Quote: |
Don't know if this is what your wanting or not...
| I'm not wanting here, just answering.
Good work, and hope you learned something.
I think I know how echo and print functions work (PHP or whatever). The reason I am so smug...
What you may want to look into is:
[xtra credit]
how (exactly) does spit work?
how (exactly) does return work?
[/xtra credit]
Let me splain.
Get real low level, or not.
Obviously, both output something, somewhere. Probably the same way, just to be efficient.
The difference being one returns something, other doesn't.
But what does that mean?
Nothing, or not.
Welcome to a short lesson on "synchronous" and "asynchronous" programming.
echo is "asynchronous".
print functions, generally, "synchronous" .
means, echo spits, nothing else.
print functs, spit, and tells you spit done.
btw, you can echo functions in PHP! gets confusing.
Think of it this way:
Clickey jobber to open garage door.
echo can do that. print, can do that and return garage door open, or not.
what do you want, if parking car in garage? |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |