tags:

views:

44

answers:

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

+2  A: 

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: http://stackoverflow.com/questions/2122522/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?

KM
Doesn't matter - it's not a public site - and I already made sure that LFlightRadio exists, because I checked it repeatedly in statements that I cut.
DeadMG
when you do code a public site you will fall back into this habit.
KM
Well, that was revealing. $_SESSION['LFlightRadio'] is 8. But the constructed $request doesn't actually contain this value.
DeadMG
Better to use parametrized queries for sure, but session variables should be safe as long as you're not putting user input in them.
Marcus Adams
@DeadMG: defensive coding practices shouldn't be abandoned just because you think it doesn't matter. Guess from where MOST security break ins occur.. Internal applications by current employees.
Chris Lively
The database is full of junk for demonstration purposes, and the exactitudes of my SQL communication are not the subject of the demo.
DeadMG
I don't like your attitude, son.
Adam Backstrom
A: 
    $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.

DeadMG
Concatenation should have worked. It's most likely a typo. Notice the case difference between `'LFlightRadio'` and `'LFLightRadio'`.
Marcus Adams
You see what you want to see, rather than what's there, I guess.
DeadMG
+1  A: 

Did you mean to use

mysql_fetch_assoc

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

SorcyCat
It has both, actually. I just changed to assoc, cause I was dumping duplicates on the screen.
DeadMG
A: 

Several thoughts:

// Use mysql_real_escape_string on your $_SESSION value.
echo $query = 'SELECT * FROM flight WHERE Id = \''.mysql_real_escape_string ($_SESSION['LFLightRadio']).'\'';

// Use mysql_error();
$data = mysql_query($request, $query ) or die( mysql_error() );

// check the output
var_dump( $data );

// Check the result of fetch_array
var_dump( mysql_fetch_array( $data ) );
Christopher W. Allen-Poole
A: 

You have a mis-capitalization in your code.

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

Array keys are case sensitive.

brandon k