Jump to content

[SOLVED] Single Value Query Question


chordsoflife

Recommended Posts

I'm working with a lot of M:N databases, so I'm having to do a lot of querying for ONLY the foreign key or ONLY the primary key, given the opposite. As such, it's a pain in the neck to loop through when I know it's a single result.

 

Is there a way to echo out the query result without a foreach or while loop?

Link to comment
https://forums.phpfreaks.com/topic/150534-solved-single-value-query-question/
Share on other sites

$pkPersonID = 56;

$fkPersonID = mysql_query("SELECT fkPersonID FROM tblPersonPhone WHERE fkPersonID = $pkPersonID", $conn);
echo $fkPersonID['fkPersonID'][0];

 

This is essentially what I'm looking to do, but I can't seem to get it to work. However, if I loop through it and echo it, then I get the value I'm looking for. Sorry for being vague.

 

Edit: the concept here is what I'm looking for - not necessarily the query. I just want to be able to store the value without dealing with an array.

$fkPhoneID = mysql_query("SELECT fkPhoneID FROM tblPersonPhone WHERE fkPersonID = $pkPersonID", $conn);

 

That should make more sense. I just typed it up real quick, so I wasn't thinking. My question remains the same though. I know there's only going to be one result, so is there a way to access that without looping through?

I know there's only going to be one result, so is there a way to access that without looping through?

 

Of course.

 

<?php

if ($result = mysql_query("SELECT fkPhoneID FROM tblPersonPhone WHERE fkPersonID = $pkPersonID", $conn)) {
  if (mysql_num_rows($result)) {
    $row = mysql_fetch_assoc($result);
    echo $row['fkPhoneID'];
  }
}

?>

Oh well. My logic was that mysql_query returns an array. Arrays fill up starting at 0. So if I echo'd out whatever was in the first "cell" of the array, I'd get what I wanted.

 

Alright, thanks a lot for you help, thorpe.

 

mysql_query returns a result resource, not an array.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.