blackcell Posted January 22, 2008 Share Posted January 22, 2008 I have tried to find a way to select one field from one record in a database but I can't do it without the usual while loop. Here is how I do it: <?php $sqlQuery = "SELECT `persInfo_ID` FROM `jam_persinfo` WHERE `persInfo_nameLast` = '$nameLast'"; $sqlResult = mysql_query( $sqlQuery ); //I know there is a simpler way of doing this part. while($row = mysql_fetch_array($sqlResult)){ $persInfo_ID = $row["persInfo_ID"]; break; } ?> I have tried a few things based upon reading but I don't understand. Thanks. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 22, 2008 Share Posted January 22, 2008 <?php $sqlQuery = "SELECT `persInfo_ID` FROM `jam_persinfo` WHERE `persInfo_nameLast` = '$nameLast' LIMIT 1"; $sqlResult = mysql_query( $sqlQuery ); $row = mysql_fetch_array($sqlResult); $persInfo_ID = $row["persInfo_ID"]; ?> The LIMIT is not required Quote Link to comment Share on other sites More sharing options...
blackcell Posted January 22, 2008 Author Share Posted January 22, 2008 I tried this and still no cigar. Is it still mysql_fetch_array? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 22, 2008 Share Posted January 22, 2008 Try this out and send back the output.. <?php $sqlQuery = "SELECT `persInfo_ID` FROM `jam_persinfo` WHERE `persInfo_nameLast` = '$nameLast' LIMIT 1"; $sqlResult = mysql_query( $sqlQuery ); if(!$sqlResult) die("Query failed: ".mysql_error()); $row = mysql_fetch_array($sqlResult); var_dump($row); $persInfo_ID = $row["persInfo_ID"]; ?> Quote Link to comment Share on other sites More sharing options...
blackcell Posted January 22, 2008 Author Share Posted January 22, 2008 echo = array(2) { [0]=> string(45) "Last_First_[2008-01-22]_JAM_APPLICATION.pdf" ["fileTrack_fileName"]=> string(45) "Last_First_[2008-01-22]_JAM_APPLICATION.pdf" } [] This works with the loop btw. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 22, 2008 Share Posted January 22, 2008 Well...there is no difference between while($row = mysql_fetch_array($sqlResult)){ $persInfo_ID = $row["persInfo_ID"]; break; } and $row = mysql_fetch_array($sqlResult); $persInfo_ID = $row["persInfo_ID"]; that aside, there is no way the output you provided can be produced with the code provided. Where is the "echo =" coming from? And your output says that $row is an array with 2 strings, but your query selects only one column. Is there code you are omitting? Quote Link to comment Share on other sites More sharing options...
blackcell Posted January 22, 2008 Author Share Posted January 22, 2008 i am changing $row to $row2 because i modified from another doc. I also added the echo = to tell you that is what it said. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 22, 2008 Share Posted January 22, 2008 so...your code looks like this then? <?php $sqlQuery = "SELECT `persInfo_ID` FROM `jam_persinfo` WHERE `persInfo_nameLast` = '$nameLast' LIMIT 1"; $sqlResult = mysql_query( $sqlQuery ); if(!$sqlResult) die("Query failed: ".mysql_error()); $row2 = mysql_fetch_array($sqlResult); var_dump($row2); // $persInfo_ID = $row["persInfo_ID"]; ?> If possible please take a direct copy/paste from your code as it is most likely some small mistake. Obviously remove any sensitive data, but make notes about what was changed/removed. Sorry if this sounds remedial, but the code you say you are running and the output you are providing don't match up. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 22, 2008 Share Posted January 22, 2008 Actually, because mysql_fetch_array() returns a numerical array and an associative array, the output is not totally unexpected, however it does indicate that what was SELECTED is not what the posted query actually is. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 22, 2008 Share Posted January 22, 2008 good catch...i always use mysql_fetch_assoc() so don't have that problem. man...that is twice today i've fouled up a mysql_fetch_array() problem. and yes...the output is weird. it suggests that the query is running something along the lines of: SELECT `fileTrack_fileName` FROM .... Quote Link to comment Share on other sites More sharing options...
blackcell Posted January 22, 2008 Author Share Posted January 22, 2008 It is, and I asked earlier whether it should be mysql_fetch_array or something else but hey we got it. Thanks man. Quote Link to comment 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.