Jump to content

single result setting variable as array


rbragg

Recommended Posts

I am bringing in a value via POST:

 

<?php $bird= mysql_real_escape_string($_POST['searchBird']); ?>

 

If this value is not empty, I would like to look the bird up in its table and get the primary key (s_num) to use in a later select query.

 

<?php
if ( !empty($bird) )
{
  $queryBirdSearch = "
  SELECT s_num
  FROM bird
  WHERE bird = '$bird'
  ";
  $birdSearchResults = mysql_query($queryBirdSearch) or die( "Bird search query failed: " . mysql_error() );
  $sNum = mysql_fetch_assoc($birdSearchResults);

  $querySearch.= " AND call.s_num = '$sNum' ";
}
?>

 

However, I am not finding a match although I KNOW there is one because I'm sitting here looking at it. I echo my two variables,

 

<?php
echo "bird: " . $bird;
echo "<br>s_num: " . $sNum;
?>

 

and get:

 

bird: 845712365

s_num: Array

 

The bird: is right ... but, why is my s_num being set as an array when there is only one result? Have I been staring at this for too long?

Link to comment
https://forums.phpfreaks.com/topic/60799-single-result-setting-variable-as-array/
Share on other sites

To display the s_num, you would have to do this:

 

<?php
echo "bird: " . $bird;
echo "<br>s_num: " . $sNum['s_num'];
?>

 

You also might want to change this line

$querySearch.= " AND call.s_num = '$sNum' ";

 

To:

$querySearch.= " AND call.s_num = '{$sNum['s_num']}' ";

 

And if you wanted to display ALL the matches instead of just the first one, you would have to use a while loop.

<?php
if ( !empty($bird) )
{
  $queryBirdSearch = "
  SELECT s_num
  FROM bird
  WHERE bird = '$bird'
  ";
  $birdSearchResults = mysql_query($queryBirdSearch) or die( "Bird search query failed: " . mysql_error() );
  if (mysql_num_rows($birdSearchResults)) {
    $row = mysql_fetch_assoc($birdSearchResults);
    $sNum = $row['s_num'];
    $querySearch.= " AND call.s_num = '$sNum' ";
  }
}
?>

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.