简体   繁体   中英

Using php Session Variables To Fetch Data From MySQL Database

I'm trying to use php session variables in a SELECT Statement to fetch the data stored in a multiple array database. However, I am facing some challenges with my code below:

<?php
if(!empty($_SESSION['array'])) {
    if($_SESSION['array'] == 'arrayname') {

        $query = "SELECT '{$_SESSION['option']}' FROM '{$_SESSION['array']}' WHERE '{$_SESSION['key']}'='".mysqli_real_escape_string( $link, '{$_SESSION['value']}')."'";

        if ($result=mysqli_query( $link, $query)) {

            while ($row = mysql_fetch_array($result)) {

                echo $row['{$_SESSION['value']}'];

            }

        }
        mysqli_close($link); // Closing Connection with Server
    }
} ?>

Any attempt to fetch the data with the above code rather displays an error messages, like;

Parse error: syntax error, unexpected 'value' (T_STRING) in C:\xampp\htdocs\content\fetchData.php on line 5

I will be much grateful for a way out to deal with this challenge. Thanks in advance!

For your query, get your variable first:

$value = mysqli_real_escape_string( $link, $_SESSION['value']);

Then use $value rather than try and embed like you have above.

You don't need the single quotes or the curly braces around $_SESSION when you are echoing the value. Just do this:

echo $row[$_SESSION['value']];

That should fix your current error, however you still have more issues with your code. Namely you are mixing mysql and mysqli functions, which won't work. Move your code over to mysqli completely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM