pakki pakki - 6 months ago 6
SQL Question

parse error syntax error unexpected $stmt


parse error syntax error unexpected $stmt (T_VARIABLE)


Have been trying this but,Couldn't find the syntax error. Someone please help. Thanks.

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

//Getting values
// $email = $_POST['email'];


$servername = "localhost";
$username = "root";
$password = "";
$database = "androiddb";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else{
echo "connedted";
}


$sql = "select * from userdefault";
$stmt = $conn->prepare($sql);

$stmt->execute();

$result = $stmt->get_result();

while ($row = $result->fetch_assoc()){

echo $row['0'] + " " +$row['1'] + " " +$row['2'] + " ";
}

mysqli_close($conn);

?>


Find the error.

Answer

I see you are missing a closing } for if($_SERVER['REQUEST_METHOD']=='POST'){

See the fixed below:

 if($_SERVER['REQUEST_METHOD']=='POST'){

    //Getting values
//  $email = $_POST['email'];


   $servername = "localhost";
   $username = "root";
   $password = "";
   $database = "androiddb";

 // Create connection
   $conn = new mysqli($servername, $username, $password, $database);

  // Check connection 
   if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
 }  
 else{
 echo "connedted";
  }


  $sql = "select * from userdefault";
   $stmt = $conn->prepare($sql);

   $stmt->execute();

    $result = $stmt->get_result();

   while ($row = $result->fetch_assoc()){   

       echo $row['0'] + " " +$row['1'] + " " +$row['2'] + " ";
    }

     mysqli_close($conn);

    }  ?>