I try to make an application and this application have a login page were i want to chech if the user exist in database , i have try to implement the prepared statement but i have a error comming up , but my code sounds ok i only check for is name .
This is the error
mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in /home/nicetaxi/public_html/mobilapp/getdata.php on line 16 [05-Nov-2021 06:31:34 UTC] PHP Warning: array_push() expects parameter 1 to be array, null given in /home/nicetaxi/public_html/mobilapp/getdata.php on line 21
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$servername = "localhost";
$username = "username";
$password = "pass";
$dbname = "databasename";
//Here we will get name entered by the user from the key 'name'
$name = $_POST["name"];
//This will contain a JSON array of your data.
$result_array = array();
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// prepare and bind
$stmt = $conn->prepare("SELECT name FROM users WHERE name='$name'");
$stmt->bind_param("s", $name);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
array_push($result_array, $row);
echo $row['name'];
}
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
array_push($result_array, $row);
}
}
/*This sends a JSON encded array to client */
echo json_encode($result_array);
$stmt->close();
$conn->close();
?>
CodePudding user response:
In Fact it was a distraction of me on a missing line //This will contain a JSON array of your data.
$result_array = array();
CodePudding user response:
Your prepared statement seems incorrect there
$stmt = $conn->prepare("SELECT name FROM users WHERE name='$name'");
$stmt->bind_param("s", $name);
The above should be
$stmt = $conn->prepare("SELECT name FROM users WHERE name=?");
$stmt->bind_param("s", $name);
please check the example example prepared staement