0

I made this query in PHP for MySQL:

$query = "SELECT `username` FROM `usersdata` (select @user := van FROM `usersdata`) WHERE `username` = @user AND `password` = ? ";

but it gives this error:

Fatal error: Call to a member function bind_param() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/healthyfoodsite/authentication.php on line 14.

How do I make a simple variable for MySQL in PHP? What did I do wrong? I need to do it all in one query.

Here is all of my code:

require('db.php');
if(isset($_POST['submit'])){
    if(isset($_POST['username']) && isset($_POST['password'])){
        global $connection;

        $username = $_POST['username'];
        $password = $_POST['password'];

        $query = "SELECT `username` FROM `usersdata` (select @user := van FROM `usersdata`) WHERE `username` = @user AND `password` = ? ";

        $stmt = $connection->prepare($query);
        $stmt->bind_param("ss",$username,$password);
        $stmt->execute();
        $stmt->store_result();
        if($stmt->num_rows > 0){
            $stmt->bind_result($username);
            while($stmt->fetch()){
                echo $username;
            }
        }else{
            echo "nothing happen";
        }
    }else{
        header('location: ');
    }
}
?>
Cache Staheli
  • 3,510
  • 7
  • 32
  • 51
jakson
  • 255
  • 3
  • 10
  • @VARS are only MySQL variables and not PHP variables and cant transfer. the only way to retun the value of a MySQL var is * SELECT @USER;;** this will be return the content of the var USER – Bernd Buffen Jan 11 '17 at 22:58
  • i meant to use the mysql in php – jakson Jan 11 '17 at 22:59
  • sorry i not understand your query. to pass parameter to Mysql in prepared statement is to use the ? operand like **SELECT `username` FROM `usersdata` WHERE `username` = ? AND `password` = ? ";** then you give the both parameter with **$stmt->bind_param("ss",$username,$password);** – Bernd Buffen Jan 11 '17 at 23:06
  • in php that is prepare statement, the ? mark will be replace by $stmt->bind_param("s",$username). each ? mark will be replace by the bind_param. – jakson Jan 11 '17 at 23:10
  • thats correct. in your sample you give 2 variables from type string "ss" to the statement. the first mark will be replaced by the content of $username and the second with $password. – Bernd Buffen Jan 11 '17 at 23:29
  • Your SQL syntax is incorrect. You need to `JOIN` with the subquery that sets the variable. That's why you're getting an error. – Barmar Jan 11 '17 at 23:40
  • Possible duplicate of [PDO: Call to a member function fetch() on a non-object?](http://stackoverflow.com/questions/3349612/pdo-call-to-a-member-function-fetch-on-a-non-object) – miken32 Jan 12 '17 at 00:16
  • no i am asking on how to make variable in SQL cause my query does not work. the php have no problem. how do you make a sql variable? – jakson Jan 12 '17 at 01:04
  • can you please give me the query example Barmar. i didnt know where to put JOIN. – jakson Jan 12 '17 at 01:04

0 Answers0