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;
?>
asked Jul 8, 2017 at 6:03
Donat Treszler
471 silver badge12 bronze badges
2 Answers 2
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
Donat Treszler
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';
Vivek Shah
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
There are two errors
- You are missing
}closing bracket afterdie - 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;
answered Jul 8, 2017 at 6:06
urfusion
5,4995 gold badges56 silver badges91 bronze badges
Comments
default
mysqli_query($conn,$sql)and change'SELECT id FROM users WHERE email="[email protected]"'}just afterdie()statement