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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

single field from db into variable

Status
Not open for further replies.

DaveC426913

Programmer
Jul 28, 2003
274
CA
$sql = "SELECT LastName FROM myTable WHERE `UID` = ".$UID.";
$resultA = mysql_query($sql);
$LastName = mysql_result($resultA,0,"LastName");

I know this is like, PHP101, but I can never seem to quite get the right answer.

How DO you get a single field from a single row of the db into a variable?

 
Try:
Code:
$row=mysql_fetch_array($resultA);
$LastName=$row['LastName'];

[code]

After you issue the query.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Well, yeah - that's the way I've been doing it - but it's so graceless! You're getting an entire array so get one cell. How about even fetch_row?




 

from your code snip above all the three options below should work
Code:
$sql = "SELECT LastName as LastName FROM myTable WHERE `UID` = ".$UID.";
$resultA = mysql_query($sql) or die ("cannot perform query ".mysql_error());
if (mysql_num_rows($resultA) !== 0) :
$lastname[] = mysql_result($resultA, 0 , 0); //first column of first record

$lastname[] = mysql_result($resultA, 0 , "LastName"); //better to use aliases to avoid caps issues
or
$lastname[] = mysql_result($resultA, 0); //defaults to first row
endif;

echo "Last name returned from each option is <br/><pre>";
print_r ($lastname);
echo "</pre>";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top