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
Share on other sites

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

 

Put a WHERE clause in your query and only grab the record you need? We really need more details of exactly what it is your doing.

Link to comment
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.

Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

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'];
  }
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.