Search code examples
phpstring-concatenation

Cant insert string into mysql query


I'm trying to make a login page in PHP, and I'm trying to construct the query here:

$q = 'SELECT * FROM users WHERE userid="'+$username+'"';

When I echo it out with

echo $q

I get 0. When I do

$q = 'SELECT * FROM users WHERE userid="'+"test"+'"';

I get 0. When I do

$q = 'SELECT * FROM users WHERE userid="michael"';

I get my expected result of the string being printed out


Solution

  • Use a . for concatenation, also don't forget to clean the data to prevent mysql injection.

    $user_id = 'test';
    $q = 'SELECT * FROM users WHERE userid="' . $user_id . '"';