»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 06-08-2003, 01:22 AM   #1 (permalink)
Registered User
 
Join Date: Oct 2001
Location: Ooltewah, TN
Posts: 483
LittleKing is on a distinguished road
Send a message via Yahoo to LittleKing
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

LittleKing is offline   Reply With Quote
Old 06-08-2003, 02:08 AM   #2 (permalink)
Registered User
 
Join Date: Oct 2001
Location: Ooltewah, TN
Posts: 483
LittleKing is on a distinguished road
Send a message via Yahoo to LittleKing
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
LittleKing is offline   Reply With Quote
Old 06-08-2003, 10:11 PM   #3 (permalink)
Banned
 
qball's Avatar
 
Join Date: Oct 2001
Posts: 447
qball is on a distinguished road
[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]
qball is offline   Reply With Quote
Old 06-09-2003, 01:25 PM   #4 (permalink)
Registered User
 
Join Date: Oct 2001
Location: Ooltewah, TN
Posts: 483
LittleKing is on a distinguished road
Send a message via Yahoo to LittleKing
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
LittleKing is offline   Reply With Quote
Old 06-09-2003, 10:47 PM   #5 (permalink)
Banned
 
qball's Avatar
 
Join Date: Oct 2001
Posts: 447
qball is on a distinguished road
you are welcome.

didya learn anything about 'printf'?
qball is offline   Reply With Quote
Old 06-12-2003, 07:00 PM   #6 (permalink)
Registered User
 
Join Date: Oct 2001
Location: Ooltewah, TN
Posts: 483
LittleKing is on a distinguished road
Send a message via Yahoo to LittleKing
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
LittleKing is offline   Reply With Quote
Old 06-12-2003, 10:20 PM   #7 (permalink)
Banned
 
qball's Avatar
 
Join Date: Oct 2001
Posts: 447
qball is on a distinguished road
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]
qball is offline   Reply With Quote
Old 06-18-2003, 10:12 AM   #8 (permalink)
Registered User
 
Join Date: Oct 2001
Location: Ooltewah, TN
Posts: 483
LittleKing is on a distinguished road
Send a message via Yahoo to LittleKing
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
LittleKing is offline   Reply With Quote
Old 06-18-2003, 09:52 PM   #9 (permalink)
Banned
 
qball's Avatar
 
Join Date: Oct 2001
Posts: 447
qball is on a distinguished road
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?
qball is offline   Reply With Quote
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 04:39 PM.