检查数据库中是否存在某些内容

时间:2020-06-30 12:06:14

标签: php mysql database

我正在尝试检查daet是否已存在于数据库中,但是它显示警告:mysqli_num_rows()期望参数1为mysqli_result。

<?php
$sql_d = "SELECT * FROM programarii WHERE date_time='$dateTime";
$res_u = mysqli_query($conn, $sql_d);
if (mysqli_num_rows($res_u) > 0) {
    $dateTimeErr = "Sorry date not available";
}
?>

1 个答案:

答案 0 :(得分:0)

您在查询中错过了'。 试试:

$sql_d = "SELECT * FROM programarii WHERE date_time='$dateTime'";

重要:我强烈建议您使用PDO来构建查询,因为您现在容易受到SQL注入的攻击。<​​/ p>

编辑:

尝试这样的方法:

$dsn = "mysql:host=localhost;dbname=mydb";
$user = "root";
$passwd = "andrea";

$pdo = new PDO($dsn, $user, $passwd);

$dateTime = '2020-05-23 23:00';

$stm = $pdo->prepare("SELECT * FROM programarii WHERE date_time = ?");
$stm->bindValue(1, $dateTime);
$stm->execute();

$rows = $stm->fetchAll(PDO::FETCH_ASSOC);

if(count($rows)>0)
{
    print('date not available');
}