Puppy
Puppy

Reputation: 146910

SQL returns non-array value in PHP

    $request = 'SELECT * FROM flight WHERE Id = \''.$_SESSION['LFLightRadio'].'\'';
    $data = mysql_fetch_array(mysql_query($request, $SQL));
    echo '<table class="table">';
    foreach($data as $key => $value) {
        echo '<th class="head" align="center" height="19">'.$key.'</th>';
    }
    echo '<tr>';
    foreach($data as $key => $value) {
        echo '<td class="cell" align="center" height="19">'.$value.'</td>';
    }
    echo '</tr></table>';

I know that the LFlightRadio value is set, and is a value returned by the Id value of a previously returned row from the flight database. So within "flight", a record definitely exists with this Id. But, this still gives me a non-array result, so that when I try to use foreach on it, it errors out. Suggestions?

Upvotes: 1

Views: 112

Answers (4)

brandon k
brandon k

Reputation: 620

You have a mis-capitalization in your code.

$_SESSION['LFLightRadio'] is most likely intended to be $_SESSION['LFlightRadio']

Array keys are case sensitive.

Upvotes: 1

Puppy
Puppy

Reputation: 146910

    $var = $_SESSION['LFlightRadio'];
    $request = "SELECT * FROM flight WHERE Id = '$var'";
    $data = mysql_fetch_array(mysql_query($request, $SQL));

This works! For some reason, when I concatenated it straight, it didn't actually add the variable into the string. But when I've done that, it got put in and I got the expected.

Upvotes: 0

KM.
KM.

Reputation: 103579

before the echo '<table class="table">';, add:

echo '<br>$_SESSION[\'LFLightRadio\']="'.$_SESSION['LFLightRadio'].'"<br>';

to make sure you actually have a value to compare to flight.Id in the query. The way you are doing this is a huge SQL injection attack waiting to happen! See this question: mysql_real_escape_string() for $_SESSION variables necessary?

EDIT

add this before the echo '<table class="table">';:

echo '<br>$request="'.$request.'"<br>';

run that query on the database, are any rows returned?

Upvotes: 2

SorcyCat
SorcyCat

Reputation: 1216

Did you mean to use

mysql_fetch_assoc

instead of mysql_fetch_array? Array would have keys like 0,1,2...

Upvotes: 1

Related Questions