Log in

View Full Version : PHP & mysql array



mtran
05-01-2006, 08:46 AM
Hi,

I want to display some field from db and use the followings - learnt from some tutorial - to display with format:


<?php
require ('config.php');
$query="SELECT * FROM movie";
$result=mysql_query($query,$connection);
while($row = mysql_fetch_array($result)){
printf("<b>Title:</b> %s<br> <b>Origin:</b> %s<br>", $row["title"],$row["origin"]);} ?>


Right now, the code print all the rows from the table 'movie'.
How do I make it to print only say the first row?

Thanks.

djr33
05-01-2006, 09:15 AM
eh, here's what I'd do. I don't use printf, nor mysql_fetch_array, so not sure what's "wrong" with those...but... here:


<?php
require ('config.php');
$query="SELECT * FROM movie";
$result=mysql_query($query,$connection);
while($row = mysql_fetch_assoc($result)){
echo "<b>Title:</b> %s<br> <b>Origin:</b> %s<br>".$row["title"].$row["origin"]);
}
?>


assoc vs. array--- you can configure array to be assoc, but why, plus, you didn't.
basically array is numbers ($row[1]) vs assoc is names ($row['name']).

echo.... works for me. why are you using print? I have no clue why there are two commands, so you may be totally right.
however, echo will work as is right now.

mtran
05-01-2006, 09:37 AM
Thanks.
Clarify:

What I meant by first row is 'first recordset' -- maybe this is the correct word.
1st record set will be e.g:
Title: Mystic River
Origin: America
2nd record set:
Title: Fearless
Origin: Hong Kong

What if I only want to print/ echo one recordset.

Reason: I have lots of thumbnails of movies, and use ToolTip with mouseover. I want: when a thumbnail is mouseover, the ToolTip will use info from db, but only 1 recordset that's relevant to the thumbnail.

Twey
05-01-2006, 12:16 PM
Rather than:
while($row = mysql_fetch_array($result)){
printf("<b>Title:</b> %s<br> <b>Origin:</b> %s<br>", $row["title"],$row["origin"]);} ?>Use:
$row = mysql_fetch_array($result);
printf("<b>Title:</b> %s<br> <b>Origin:</b> %s<br>", $row["title"],$row["origin"]);
?>That's some pretty ugly code. Keeping it neat will save you headaches later on.
Both mysql_fetch_assoc() and mysql_fetch_row(), the former returning an associative array and the latter returning a numerical array, are pointless aliases, of which PHP has many. They're not technically deprecated in favour of mysql_fetch_array(), but they should be. mysql_fetch_array() can take a second argument: MYSQL_NUM to behave like mysql_fetch_row(), MYSQL_ASSOC to behave like mysql_fetch_assoc(), and MYSQL_BOTH to fetch an array with both numerical and associative indices, which is the default behaviour.

mwinter
05-01-2006, 12:26 PM
How do I make it to print only say the first row?There are two options, here.

Add a LIMIT clause to the query so that the query itself only returns a single tuple. This has the advantage of speeding up the query itself, but it's clearly not appropriate if you want the other results for some other purpose.



$query = "SELECT * FROM movie LIMIT 1";
However, you should also add an ORDER BY clause, too, to ensure that the returned value is predictable.

The second option is to remove the while loop (as Twey demonstrated).

You'll have to decide which is best under your circumstances.


What I meant by first row is 'first recordset' -- maybe this is the correct word.No, it isn't. :) The word 'recordset' is a synonym for query result. The word you want is record or tuple.

Mike

mtran
05-01-2006, 03:10 PM
I figured it out. Thanks a lot for your help!

-----------------
THANKS!
It sounds silly [and probably it's !] to ask, but I don't know yet how to use that code, either with LIMIT 1 or without WHILE, to display the SECOND record only, and the THIRD record only,...b/c I have other movie thumbnails also.

Or maybe you can help me with a better solution for what I'm doing: I'm doing something like this but much much simpler of course: http://www.netflix.com/BrowseSelection?lnkctr=nmhbs

Only the info for the tooltip [Title, origin, year,...] is from a db -- all others such as image, title on the page are hardcoded.
Thank you! Really appreciate your help!