chordsoflife Posted March 22, 2009 Share Posted March 22, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/150534-solved-single-value-query-question/ Share on other sites More sharing options...
trq Posted March 22, 2009 Share Posted March 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/150534-solved-single-value-query-question/#findComment-790679 Share on other sites More sharing options...
chordsoflife Posted March 22, 2009 Author Share Posted March 22, 2009 $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. Quote Link to comment https://forums.phpfreaks.com/topic/150534-solved-single-value-query-question/#findComment-790682 Share on other sites More sharing options...
trq Posted March 22, 2009 Share Posted March 22, 2009 Your query makes little sense. Your selecting fkPersonID yet according to your WHERE clause you already know its value. Why run the query at all? Quote Link to comment https://forums.phpfreaks.com/topic/150534-solved-single-value-query-question/#findComment-790683 Share on other sites More sharing options...
chordsoflife Posted March 22, 2009 Author Share Posted March 22, 2009 $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? Quote Link to comment https://forums.phpfreaks.com/topic/150534-solved-single-value-query-question/#findComment-790686 Share on other sites More sharing options...
trq Posted March 22, 2009 Share Posted March 22, 2009 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']; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150534-solved-single-value-query-question/#findComment-790692 Share on other sites More sharing options...
chordsoflife Posted March 22, 2009 Author Share Posted March 22, 2009 So something along the lines of: echo $fkPhoneID['fkPhoneID'][0]; won't work? No short way to do it? Quote Link to comment https://forums.phpfreaks.com/topic/150534-solved-single-value-query-question/#findComment-790698 Share on other sites More sharing options...
trq Posted March 22, 2009 Share Posted March 22, 2009 So something along the lines of: echo $fkPhoneID['fkPhoneID'][0]; won't work? No short way to do it? Sorry, but that doesn't make sense. No, it won't work. Quote Link to comment https://forums.phpfreaks.com/topic/150534-solved-single-value-query-question/#findComment-790699 Share on other sites More sharing options...
chordsoflife Posted March 22, 2009 Author Share Posted March 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/150534-solved-single-value-query-question/#findComment-790700 Share on other sites More sharing options...
trq Posted March 22, 2009 Share Posted March 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/150534-solved-single-value-query-question/#findComment-790706 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.