Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning only one row and displaying the result PHP

Status
Not open for further replies.

Programz8

Programmer
Mar 27, 2007
33
US
Hello all I'm having a problem getting the result of just one row. Most info I've found is returning an entire array. I'm getting the word Object printed out instead the value of $result does anyone know why?

$request = "SELECT article_name FROM Articles WHERE id=1";
$result = mysql_query ($request,$connection);
<?php echo "mysql_result($result)"; ?>
 
Code:
<?
 $request = "SELECT article_name FROM Articles WHERE id=1";
 $result = mysql_query ($request,$connection) or die(mysql_error());
 echo mysql_result($result,0,0); 

//or
$row=mysql_fetch_assoc($result);
echo $row['article_name'];
?>
 
Thank you Jpadie I would also like to learn from this experience so can you tell me why these other pieces of code I tried to use wouldn't work?

$row = @ mysql_fetch_object ($mysql_result);

// or

$variablename = mysql_fetch_row($result);

? I've been trying different variations for hours, lol Thanks.
 
the manual is really good on this. i heartily commend it to you.

the mysql_fetch_* functions return arrays or objects containing the data from that row. you need to address the individual member of thos objects and arrays to get to the actual data.

mysql_fetch_object() fetches the row as an object. you would then need to access each member of the object like so
Code:
echo $row->article_name;


mysql_fetch_row fetches the row as a numeric array. so to see what's inside it you would have to do something like
Code:
echo $row[0];

mysql_fetch_assoc does the same as _row() but the array is associative (i.e. the keys are the field names).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top