Moritz Wagner
Moritz Wagner

Reputation: 21

Can I save a SQL result in a variable in PHP

I want to save a SQL result in a variable.

<?php
    $sqlhost='localhost';
            $username='user';
            $passwort='passwd';
            $database='...';

    mysql_connect ($sqlhost,$username,$passwort);

mysql_select_db ($database);
    $query= "SELECT * FROM Test";
    $result = mysql_query($query) or die( mysql_error() );

    while($row = mysql_fetch_array($result)){
        echo $row['FB'];
    }
    $varibale = $row['ROW'];
    echo $varibale;
?>

I want to save it in a variable to make another SQL query with WHERE ROW2 = '$varibale'

Upvotes: 1

Views: 1171

Answers (1)

Grynets
Grynets

Reputation: 2525

In this part of code:

while($row = mysql_fetch_array($result)) {
   echo $row['FB'];
}
$varibale = $row['ROW'];
echo $varibale;

You can access $row only inside while loop, so the simpliest solution is to create array, fill it, and than access. Like this:

$rowArray = [];
while($row = mysql_fetch_array($result)){
   array_push($rowArray, $row['FB']);
}
$varibale = $rowArray['ROW'];
echo $varibale;

Upvotes: 1

Related Questions