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

Link to comment
Share on other sites

<?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' ";
  }
}
?>

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.