Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/87257-solved-mysql-selecting-one-field/
Share on other sites

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

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"];
?>

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?

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.

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.

 

 

 

 

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

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.