简体   繁体   中英

First parameter in Mysqli Query is null

I have this problem, when i am printing the second parameter from mysqli query it prints out the query to be performed but when i print the first parameter it prints nothing and when I perform the query it produces an error and it cannot insert the data into database. Here's my code:

            $Insert_Patient_Data = "INSERT INTO patient_account(P_Password,P_Fname,P_Lname,P_Mname,P_Age,P_Gender,P_Email) 
            VALUES('$Encrypted_Password', '$Inputted_First_Name', '$Inputted_Last_Name', '$Inputted_Middle_Name', '$Inputted_Age', '$Inputted_Gender', '$Inputted_Email')";

            $Patient_Query = mysqli_query($Connection, $Insert_Patient_Data);

            if(!$Patient_Query)
                echo "<script type = 'text/javascript'> alert('Error: Database Connection error. Please try again Later.') </script>";
            else
                echo "<script type = 'text/javascript'> alert('Succesfully Registered.') </script>";
  1. I tried to print the second parameter which is $Insert_Patient_Data , it prints the query. there is nothing wrong in the output.
  2. I tried to print the first paramter which is $Connection and it gives an error: Recoverable fatal error: Object of class MySQLi could not be converted to string
  3. I tried to print the $Patient_Query but it prints nothing, even the second parameter (which is $Insert_Patient_Data) became null at this line.
  4. At this time i tried to perform the $Patient_Query (Not printing it) and it goes to the if(!$Patient_Query) decision.

Question: Why it becomes a null? and what happens? and what is the solution on it? There are some queries on this page accessing the same table but they had no problem.

PS i checked the table name and column names from the database and they are all okay. I also included the System_Connector.php (Where the connection to database and the variable $connection is declared).

var_dump($Connection) will show you have a mysqli object, you cant echo objects!

Anyway, call mysqli_error($Connection) if it fails http://php.net/manual/en/mysqli.error.php

$Patient_Query = mysqli_query($Connection, $Insert_Patient_Data) or die(mysqli_error($Connection));

Or even:

    if(!$Patient_Query = mysqli_query($Connection, $Insert_Patient_Data);)
        //fail
    else
       // pass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM