0

I have a problem with this code. It has syntax error and I don't know what is it.

<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'id1381007_accounts';
$conn = new mysqli($host,$user,$pass,$db) or die($mysqli->error);

if (!$conn) {
die('Could not connect: ' . mysql_error());

$sql  = 'SELECT id FROM users WHERE email=\"[email protected]\"';

echo $sql;
?>
3
  • execute your query by mysqli_query($conn,$sql) and change 'SELECT id FROM users WHERE email="[email protected]"' Commented Jul 8, 2017 at 6:04
  • 1
    missing } just after die() statement Commented Jul 8, 2017 at 6:05
  • you don't need the backslashes in the sql query Commented Jul 8, 2017 at 6:11

2 Answers 2

1

There are some issue with the code. First you forgot to close the if condition over here

if (!$conn) {

And then you forgot to execute the sql query

the complete code would be like

<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'id1381007_accounts';
$conn = new mysqli($host,$user,$pass,$db) or die($mysqli->error);

if (!$conn) {
 die('Could not connect: ' . mysql_error());
}
$sql  = 'SELECT id FROM users WHERE email=\"[email protected]\"';
if ($result = $conn->query($sql)) {
   while ( $row = $result->fetch_assoc()) {
        $data[] = $row;
    }
    echo "<pre>";
    print_r($data);
    echo "</pre>";
}
$conn->close();
?>
Sign up to request clarification or add additional context in comments.

2 Comments

And if I want to change the email address to a variable For example (I know this code isn't good) $email = [email protected] $sql = 'SELECT id FROM users WHERE email=$email';
Yes. you can just enclose it with single quotes like "SELECT id FROM users WHERE email='".$emai.l"'" . Something like this. just check and try
0

There are two errors

  1. You are missing } closing bracket after die
  2. Mysql query is wrong.

So the code should be

if (!$conn) {
die('Could not connect: ' . mysql_error());
}
$sql  = 'SELECT id FROM users WHERE email="[email protected]"';

echo $sql;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.