使用 PHP 检查 SQL 行是否存在

发布于 2024-11-19 04:22:53 字数 134 浏览 1 评论 0原文

我将 MySQL 与 PHP 结合使用,我需要做这样的事情(伪代码):

if (sql row exists where username='bob')
{
    // do this stuff
}

I'm using MySQL with PHP and I need to do something like this (pseudocode):

if (sql row exists where username='bob')
{
    // do this stuff
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东走西顾 2024-11-26 04:22:53

如果您想使用 PDO(PHP 数据对象),然后使用以下代码:

$dbh = new PDO("mysql:host=your_host_name;dbname=your_db_name", $user, $pass);
$stmt = $dbh->prepare("SELECT username from my_table where username = ':name'");
$stmt->bindParam(":name", "bob");
$stmt->execute();

if($stmt->rowCount() > 0)
{
    // row exists. do whatever you want to do.
}

If you would like to use PDO (PHP Data Object), then use the following code:

$dbh = new PDO("mysql:host=your_host_name;dbname=your_db_name", $user, $pass);
$stmt = $dbh->prepare("SELECT username from my_table where username = ':name'");
$stmt->bindParam(":name", "bob");
$stmt->execute();

if($stmt->rowCount() > 0)
{
    // row exists. do whatever you want to do.
}
原谅过去的我 2024-11-26 04:22:53

Sayem 的答案得到了最多的支持,但我认为它对于 PDO 来说是不正确的。

来自 PHP 文档

对于大多数数据库, PDOStatement::rowCount( ) 不返回受 SELECT 语句影响的行数。相反,使用 PDO::query() 发出 SELECT COUNT(*) 语句,其谓词与预期的 SELECT 语句相同,然后使用 PDOStatement::fetchColumn() 检索将返回的行数。

$sql = "SELECT COUNT(*) FROM fruit WHERE calories > 100";
$res = $conn->query($sql);

/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 0) {
    /* Issue the real SELECT statement and work with the results */
    $sql = "SELECT name FROM fruit WHERE calories > 100";
    foreach ($conn->query($sql) as $row) {
        print "Name: " .  $row['NAME'] . "\n";
    }
}
/* No rows matched -- do something else */
else {
    print "No rows matched the query.";
}

Sayem's answer has the most upvotes, but I believe it is incorrect regarding PDO.

From the PHP docs:

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned.

$sql = "SELECT COUNT(*) FROM fruit WHERE calories > 100";
$res = $conn->query($sql);

/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 0) {
    /* Issue the real SELECT statement and work with the results */
    $sql = "SELECT name FROM fruit WHERE calories > 100";
    foreach ($conn->query($sql) as $row) {
        print "Name: " .  $row['NAME'] . "\n";
    }
}
/* No rows matched -- do something else */
else {
    print "No rows matched the query.";
}
不顾 2024-11-26 04:22:53

另一种方法

$user = "bob";
$user = mysql_real_escape_string($user);
$result = mysql_query("SELECT COUNT(*) AS num_rows FROM my_table WHERE username='{$user}' LIMIT 1;");
$row = mysql_fetch_array($result);
if($row["num_rows"] > 0){
   //user exists
}

another approach

$user = "bob";
$user = mysql_real_escape_string($user);
$result = mysql_query("SELECT COUNT(*) AS num_rows FROM my_table WHERE username='{$user}' LIMIT 1;");
$row = mysql_fetch_array($result);
if($row["num_rows"] > 0){
   //user exists
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文