Script for autocomplete not working

Ok, the title is not exactly excellent, so let me explain the situation here. I use a javascript plugin to make suggestions to the user while they type in a form field. This javascript file uses a PHP file to know the values to suggest. This PHP file queries the database to return these values.

What I would like to do is to return different results based on the user who is connected, but when I try to do this, everything stops working. It only works if I specify one single query.

After the connection to the database, here is the code:


<?php
$query = $_GET['query'];
$users=array();

if ($_SESSION['privilege'] == 100) {
	$sql = "SELECT username FROM users WHERE username LIKE 'something'";
} else {
        $sql = "SELECT username FROM users WHERE username LIKE 'somethingelse'";
}
	$result = mysqli_query($conn,$sql);
	while ($row = mysqli_fetch_array($result))
	{ 
		$users['suggestions'][] = stripslashes($row['username']);
	}

$users['query'] = $query;
echo json_encode($users);
?>

It looks like the script is not able to check the value of $_SESSION[‘privilege’], but it should, because I am able to check it inside other pages. This script works if I delete the if then else.

Any ideas?:confused:

You need to put:

session_start();

At the top of your code there.

You are right, I forgot to start the session. However, it still doesn’t work…

So are you saying it stopped working after switching the query based on the session privileges?
Have you done the following:

  • Tested the queries in phpmyadmin or command line.
  • Tested to check if the ‘if’ statements have been triggered
  • Echoed the query to verify it is using what you think its using

E

$conn is undefined.
Query is returning successfully nothing;
Query is being rejected.

All 3 need to be tested.

Solved. The problem was a combination of the two lines in bold: somehow the line where I assigned the privilege to a variable session had disappeared from my code, therefore I never enter the if section of the if then else, and then I ran the second query on a wrong table and so it returned zero results.