简体   繁体   English

PHP和SQL —从数据库中选择

[英]PHP and SQL — Select from database

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=\"donat12@icloud.com\"';

echo $sql;
?>

There are some issue with the code. 代码有问题。 First you forgot to close the if condition over here 首先,您忘记在此处关闭if条件

if (!$conn) { 如果(!$ conn){

And then you forgot to execute the sql query 然后您忘记执行sql查询

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=\"donat12@icloud.com\"';
if ($result = $conn->query($sql)) {
   while ( $row = $result->fetch_assoc()) {
        $data[] = $row;
    }
    echo "<pre>";
    print_r($data);
    echo "</pre>";
}
$conn->close();
?>

There are two errors 有两个错误

  1. You are missing } closing bracket after die 您缺少} die右括号
  2. Mysql query is wrong. MySQL查询是错误的。

So the code should be 所以代码应该是

if (!$conn) {
die('Could not connect: ' . mysql_error());
}
$sql  = 'SELECT id FROM users WHERE email="donat12@icloud.com"';

echo $sql;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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