1

Having a bit of trouble passing a session variable into a mysql query in php. Tried putting it into a variable and testing against that and no luck. Also tried various formatting already. At a loss. It's just a simple string passed in.

$result = mysqli_query($db_connection, "SELECT * FROM feedback  WHERE StudentID=" . $_SESSION['BCode'] . "");
3
  • session_start() at the top of your file? Commented Dec 12, 2013 at 17:42
  • (probable) Combination of both answers below. Commented Dec 12, 2013 at 17:46
  • en.wikipedia.org/wiki/SQL_injection Commented Dec 12, 2013 at 18:24

2 Answers 2

3

Are you setting:

session_start();

at the header of your .php file?

<?php 
session_start(); 

$studentid = $_SESSION['BCode'];

$result = mysqli_query($db_connection, "SELECT * FROM feedback WHERE StudentID='" . $studentid . "'"); 

$finalResult = array(); 

while ($row = $result->fetch_assoc())
{ 
    $finalResult[] = $row; 
} 

$jsonoutput = json_encode($finalResult); // Encodes the query result into JSON 
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Check for session_start(); and try

mysqli_query($db_connection, "SELECT * FROM feedback WHERE StudentID='" . $_SESSION['BCode'] . "'");

3 Comments

<?php session_start(); mysqli_query($db_connection, "SELECT * FROM feedback WHERE StudentID='" . $_SESSION['BCode'] . "'"); $finalResult = array(); while ($row = $result->fetch_assoc()){ $finalResult[] = $row; } $jsonoutput = json_encode($finalResult); // Encodes the query result into JSON ?>
Now it's jumping down to the while loop. Error
You really should post full code right away when it comes to issues like this. @user3078580 This is the classic "Can of Worms" scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.