home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Display mysql_query results

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1496
Discussions: 200,952, Posts: 2,379,480, Members: 246,318
Old June 8th, 2003, 04:22 AM   Digg it!   #1 (permalink)
Senior Member
 
LittleKing's Avatar
 
Join Date: Oct 2001
Location: Chattanooga, TN
Posts: 660
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 June 8th, 2003, 05:08 AM     #2 (permalink)
Senior Member
 
LittleKing's Avatar
 
Join Date: Oct 2001
Location: Chattanooga, TN
Posts: 660
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 June 9th, 2003, 01:11 AM     #3 (permalink)
Banned
 
qball's Avatar
 
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]
qball is offline   Reply With Quote
Old June 9th, 2003, 04:25 PM     #4 (permalink)
Senior Member
 
LittleKing's Avatar
 
Join Date: Oct 2001
Location: Chattanooga, TN
Posts: 660
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 June 10th, 2003, 01:47 AM     #5 (permalink)
Banned
 
qball's Avatar
 
Join Date: Oct 2001
Posts: 447
you are welcome.

didya learn anything about 'printf'?
qball is offline   Reply With Quote
Old June 12th, 2003, 10:00 PM     #6 (permalink)
Senior Member
 
LittleKing's Avatar
 
Join Date: Oct 2001
Location: Chattanooga, TN
Posts: 660
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
__________________
Want a portrait, drawing, mural or any other custom art? Contact my wife Mindy Herman @ -http://www.mindyherman.com/
LittleKing is offline   Reply With Quote
Old June 13th, 2003, 01:20 AM     #7 (permalink)
Banned
 
qball's Avatar
 
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]
qball is offline   Reply With Quote
Old June 18th, 2003, 01:12 PM     #8 (permalink)
Senior Member
 
LittleKing's Avatar
 
Join Date: Oct 2001
Location: Chattanooga, TN
Posts: 660
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 June 19th, 2003, 12:52 AM     #9 (permalink)
Banned
 
qball's Avatar
 
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?
qball is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2943)
The disrespect of Obama by Russian .. (41)
Making Health Care Worse (178)
Wireless Televisions. (12)
CPU fan stops spinning randomly (9)
windows 7 problem (7)
Regular Build (11)
Is the PSU I received dead? (12)
radeon x850xt platinum & shader.. (6)
Print spooler problem (15)
HIS HD5770 graphic card question (15)
windows vista security holes (10)
Install XP pro and a Vista laptop ?.. (11)
Foreign voltage (10)
Recent Discussions
CPU fan stops spinning randomly (9)
Common Spyware Solutions (105)
EVGA 9800 gtx help with finding a goo.. (11)
Regular Build (11)
Help with onclick and buttons (0)
Modern Warfare 2: Who Bought It? (63)
Virus advise (8)
My monitor won't turn on after instal.. (1)
Laptop with wireless problem. (3)
Internet Lost (3)
Dept. of HS: NSA 'Helped' Develop Vis.. (16)
windows vista security holes (10)
Point and Shoot Camera Suggestions. (4)
Multiple Restarts Required at Boot (2)
Ideal cheap graph card for PC-Gaming? (18)
radeon x850xt platinum & shader 3 (6)
Graphics Card Upgrade Question (4)
For Sale BFG GTX285 OC2 with 10 year .. (3)
How to convert MP3's (4)
Wireless Televisions. (12)
Hp Artist Edition + Matching Bag (0)
Asus P4G8X Mobo (6)
Xbox 360 GTA: SA disk error (1)
Is the PSU I received dead? (12)
windows 7 internet problem (5)


All times are GMT -4. The time now is 04:02 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28