Forum Moderators: coopster

Message Too Old, No Replies

selecting data

         

learning000

11:02 pm on Sep 25, 2005 (gmt 0)

10+ Year Member



Hi i have about 200 bits of data in one my tables, now i have it displaying perfect, but on a certain page i need just one row of data from that perticular table out of the 200 so i try this.

SELECT * FROM my_table WHERE name='octavio' , hoping it would only show that data for that row.... can anyone tell me what im doing wrong or if they understand what i mean

elgumbo

9:25 am on Sep 26, 2005 (gmt 0)

10+ Year Member



Hi

What is displayed when you use that SQL? It should only return the row that has the name you specified.

You could also specify the rows unique ID instead of the name (assuming you have one?).

grandpa

10:34 am on Sep 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



From your statement hoping it would only show that data for that row it sounds like you are returning multiple rows for 'octavio'

You can limit the number of rows returned with the LIMIT selector, SELECT * FROM my_table WHERE name='octavio' LIMIT 0,1

Be advised that while this will return only one row, it may not be the one row you anticipated. For example, if you wanted the third row for 'octavio' the LIMIT selector isn't the best solution.

However, before I ramble on any more, how about telling us what data you are, or are not, selecting.

learning000

3:10 pm on Sep 26, 2005 (gmt 0)

10+ Year Member



well when im using the above code i get nothing, but if i take out the WHERE= bit its fine so im not sure what im doing wrong. I figured if i add the where= i could select just a certain row of data out of that perticular table and it be displayed but is not the case as of yet, maybe im doing something wrong.

learning000

3:18 pm on Sep 26, 2005 (gmt 0)

10+ Year Member



Id be greatfull if somebody could give me the whole code for this, including the displaying results incase its that which is not working for me.

At the moment im using this:

$sql = 'SELECT * FROM `mydatabase` name=`octavio`;
$result = mysql_query($sql);

elgumbo

4:19 pm on Sep 26, 2005 (gmt 0)

10+ Year Member



Hi

try something like this


<?php
$sql = @mysql_query( "
SELECT *
FROM YourTable
WHERE name='octavio'
");

//make sure the query worked

if (!$sql ) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}

//get the data

$datastuff = mysql_fetch_array($sql);

$name = $datastuff ["name"];
$ID = $datastuff ["ID"];
$morestuff = $datastuff ["morestuff"];

echo "<p>Hey I just grabbed ".$name." with an ID of ".$ID." - from the database!</p>";

?>

Any good?