SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 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?

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You need to put:
    PHP Code:
    session_start(); 
    At the top of your code there.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are right, I forgot to start the session. However, it still doesn't work...

  4. #4
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    971
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    $conn is undefined.
    Query is returning successfully nothing;
    Query is being rejected.

    All 3 need to be tested.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  6. #6
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by eruna View Post
    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
    Quote Originally Posted by StarLion View Post
    $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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •