Php 检查记录是否存在(PDO)
我试图检查数据库中是否存在记录,但是当我运行下面的代码时,我得到的只是确认数据库连接的消息。解析代码后,我没有收到这两条消息中的任何一条。我是PDO的新手,尝试了各种方法来实现这一点,但仍然没有结果。有人能帮忙吗Php 检查记录是否存在(PDO),php,pdo,Php,Pdo,我试图检查数据库中是否存在记录,但是当我运行下面的代码时,我得到的只是确认数据库连接的消息。解析代码后,我没有收到这两条消息中的任何一条。我是PDO的新手,尝试了各种方法来实现这一点,但仍然没有结果。有人能帮忙吗 <?php $telephone= ($_GET [ 'telephone' ]); try { $dbh = new PDO("mysql:host=$hostname;dbname=gosdirect", $username, $password); /***
<?php
$telephone= ($_GET [ 'telephone' ]);
try {
$dbh = new PDO("mysql:host=$hostname;dbname=gosdirect", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';
$sql = "SELECT COUNT(*) FROM directory WHERE telephone == $telephone";
if ($res = $dbh->query($sql)) {
/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 0) {
echo 'The telephone number: ' . $telephone. ' is already in the database<br />';
}
/* No rows matched -- do something else */
else {
echo 'No rows matched the query.';
}
}
$res = null;
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
不需要==输入
$sql = "SELECT COUNT(*) FROM directory WHERE telephone == $telephone";
应该是
$sql = "SELECT COUNT(*) FROM directory WHERE telephone = $telephone";
希望它能帮上忙,不需要
$sql = "SELECT COUNT(*) FROM directory WHERE telephone == $telephone";
应该是
$sql = "SELECT COUNT(*) FROM directory WHERE telephone = $telephone";
希望它能帮助你做几件事。MySQL不使用==相等运算符,而应该只使用=。此外,由于您使用的是PDO,因此最好设置准备好的语句 最后,由于使用COUNT*,查询将始终返回1条记录。您需要按如下方式更新代码:
$sql = $dbh->prepare("SELECT COUNT(*) AS `total` FROM directory WHERE telephone = :phone");
$sql->execute(array(':phone' => $telephone));
$result = $sql->fetchObject();
if ($result->total > 0)
{
echo 'The telephone number: ' . $telephone. ' is already in the database<br />';
}
else
{
echo 'No rows matched the query.';
}
echo 'The telephone number: ' . strip_tags($telephone). ' is already in the database<br />';
有几件事。MySQL不使用==相等运算符,而应该只使用=。此外,由于您使用的是PDO,因此最好设置准备好的语句 最后,由于使用COUNT*,查询将始终返回1条记录。您需要按如下方式更新代码:
$sql = $dbh->prepare("SELECT COUNT(*) AS `total` FROM directory WHERE telephone = :phone");
$sql->execute(array(':phone' => $telephone));
$result = $sql->fetchObject();
if ($result->total > 0)
{
echo 'The telephone number: ' . $telephone. ' is already in the database<br />';
}
else
{
echo 'No rows matched the query.';
}
echo 'The telephone number: ' . strip_tags($telephone). ' is already in the database<br />';
没有SQL注入的情况下会发生类似的情况;:
$sql = 'SELECT COUNT(*) FROM directory WHERE telephone = :phone';
$stmt = $conn->prepare($sql);
$sth->bindParam(':phone', $_GET['telephone'], PDO::PARAM_STR, 12);
$stmt->execute();
if($stmt->fetchColumn()) die('found');
没有SQL注入的情况下会发生类似的情况;:
$sql = 'SELECT COUNT(*) FROM directory WHERE telephone = :phone';
$stmt = $conn->prepare($sql);
$sth->bindParam(':phone', $_GET['telephone'], PDO::PARAM_STR, 12);
$stmt->execute();
if($stmt->fetchColumn()) die('found');
谢谢,但结果还是一样的。我在开始时尝试了单=和trying==是没有区别的备选方案之一。很多人都很失望,因为似乎有人认为我的问题没有显示任何研究成果,并将其标记为负数。我已经尝试了两天,在许多论坛上研究了许多答案,也仔细阅读了不同书籍中的章节,但仍然没有结果。最好提及你在这个问题上的努力。这样人们就可以毫无问题地帮助你。谢谢你,但结果是一样的。我在开始时尝试了单=和trying==是没有区别的备选方案之一。很多人都很失望,因为似乎有人认为我的问题没有显示任何研究成果,并将其标记为负数。我已经尝试了两天,在许多论坛上研究了许多答案,也仔细阅读了不同书籍中的章节,但仍然没有结果。最好提及你在这个问题上的努力。这样人们可以毫无问题地帮助你。非常感谢@BenM。那很好用。它盯着我的脸,但我看不见-lol@Togfather没问题;很高兴我能帮忙!非常感谢@BenM。那很好用。它盯着我的脸,但我看不见-lol@Togfather没问题;很高兴我能帮忙!