6

Im trying to pass two variables into a mysql query, and its getting stuck when passing the session variable in below:

$check = mysql_query("SELECT * 
                      FROM Clients 
                      WHERE Username = '$new_username' 
                      AND Username != '$_SESSION['Username']'") or die(mysql_error()); 

Any tips? Thanks in advance.

6
  • Try this "SELECT * FROM Clients WHERE Username = '$new_username' AND Username != '".$_SESSION['Username']."'" Commented Feb 11, 2011 at 12:24
  • 3
    "Getting stuck" isn't really an error message. What output is this generating? Commented Feb 11, 2011 at 12:27
  • Cheers Shakti thats worked :) Commented Feb 11, 2011 at 12:31
  • I hope you see now that your question has nothing to do with sessions nor with mysql, but just PHP strings syntax issue Commented Feb 11, 2011 at 12:34
  • Also note Salman's answer. SQL injections is a thing to avoid... Commented Feb 11, 2011 at 12:36

4 Answers 4

12

It is because your single quotes '$_SESSION['Username']' have been ended by the value in the session. Changing this to '" . $_SESSION['Username'] . "' will solve the problem.

Sign up to request clarification or add additional context in comments.

Comments

8

This will work but this is VERY, VERY BAD:

$check = mysql_query("
    SELECT * 
    FROM  Clients 
    WHERE Username = '$new_username' 
    AND   Username != '{$_SESSION['Username']}'
") or die(mysql_error());

This too shall work and recommended way of doing it:

$check = mysql_query("
    SELECT * 
    FROM  Clients 
    WHERE Username  = '" . mysql_real_escape_string($new_username) . "' 
    AND   Username <> '" . mysql_real_escape_string($_SESSION['Username']) . "'
") or die(mysql_error());

Comments

1

Why no one say about "text text $array[key] text" syntax?

SELECT * 
FROM  Clients 
WHERE Username = '$new_username' 
AND   Username != '$_SESSION[Username]'

Comments

0

When you embed arrays in to strings, you have to seperate them by concatenating them or using curly braces.

$string = "text text {$array['key']} text";

or

$string = "text text " . $array['key'] . " text";

Comments

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.