A
A
Alexander Lebedev2020-05-19 09:42:22
PHP
Alexander Lebedev, 2020-05-19 09:42:22

$_SESSION variables in SQL query. How to insert?

There is a page with a form that prompts the user to select a dictionary. The selected value is passed to the $_SESSION['vocabulary_get'] variable, followed by a redirect to another page.
On another page, you need to display a selection of values ​​(all words from the selected dictionary).
How to insert $_SESSION query into SQL?

include "connection1.php";
            $arr = mysqli_query($connection, "SELECT DISTINCT `word` FROM `words` WHERE `vocabulary` = '$_SESSION['vocabulary_get']'");
            echo '<div>';
            while ($result1 = mysqli_fetch_array($arr))
            {
                echo ' <p>' . $result1['word'] . '</p>';
            }
            echo '</div>';


Or can it be done differently?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-05-19
@alex2pac_vasiliev

Just like any other

include "connection1.php";
$stmt = $connection->prepare("SELECT DISTINCT `word` FROM `words` WHERE `vocabulary` = ?");
$stmt->bind_param("s", $_SESSION['vocabulary_get']);    
$stmt->execute();
$result = $stmt->get_result();

            echo '<div>';
            while ($row = mysqli_fetch_array($result))
            {
                echo ' <p>' . $row['word'] . '</p>';
            }
            echo '</div>';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question