0

我不知道这是可能的,但这就是我想要实现的目标。

我有一个简单的 php 语句:

$ID = mysql_real_escape_string(stripslashes($_POST['ID']));

$sqlApp = "SELECT * from apps WHERE appID = ".$ID;
$resultApp = mysql_query($sqlApp);
$rowApp = mysql_fetch_array($resultApp);

$fileName = $rowApp['fileName'];

假设我的 ID 号是 1,文件名是“mobi”。

我想在此查询中根据另一个文件名检查我的文件名。

SELECT fileName, count(appId) as ct
FROM `apps`
GROUP BY filename
HAVING ct<2

如果在此查询中找到我的文件名“mobi”,那么我想回显它没有重复项。有没有办法将我的 php 变量与查询进行比较?如果这是不可能的,我可以尝试解决这个问题的另一种方法是什么?谢谢!

4

4 回答 4

0

鉴于您知道文件名,为什么不这样做: select count(*) from apps where filename = $filename

如果您想确保文件名永远不会重复,请在其上创建一个唯一索引。

于 2013-07-26T20:05:13.840 回答
0

坚持你的 SQL...

$sqlApp = "SELECT fileName, count(appId) as ct FROM `apps` WHERE fileName = '' GROUP BY filename HAVING ct<2";
$resultApp = mysql_query($sqlApp);
$has_duplicates = false;
while ( $rowApp = mysql_fetch_array($resultApp) ) {
    $has_duplicates = true;
}

如果查询有结果,只需运行您的查询,然后将“true”分配给一个变量。

于 2013-07-26T20:05:20.623 回答
0

这个查询怎么样:

$query = "select * from
(
SELECT fileName, count(appId) as ct
FROM apps
GROUP BY filename
HAVING ct<2
)cs
where cs.fileName = 
(SELECT fileName from apps WHERE appID = ".$ID.")";
于 2013-07-26T20:08:02.050 回答
0

你可以这样做:

$id = (isset($_POST['ID']) ? intval($_POST['id']) : 0);

$mysqli = new mysqli ('host', 'db_user', 'db_pwd', 'db_name');
$sql = 'SELECT fileName from apps WHERE appID = ?';
$fileName = '';
if ($statement = $mysqli->prepare($sql)) {
    $statement->bind_param('i', $id);
    $statement->execute();
    if ($result = $statement->get_result()) {
        $row = $result->fetch_assoc();
        $fileName = $row['fileName'];
        $result->free();
    }
    $statement->close();
}
$sql = 'SELECT COUNT(fileName) AS count
                FROM   apps
                WHERE  fileName = ?';
$count = 0;
if ($statement = $mysqli->prepare($sql)) {
    $statement->bind_param('s', $fileName);
    $statement->execute();
    if ($result = $statement->get_result()) {
        $row = $result->fetch_assoc();
        $count = $row['count'];
        $result->free();
    }
    $statement->close();
}
$mysqli->close();

if ($count == 1) {
    echo 'The file '.$fileName.' has no duplicates';
}
于 2013-07-26T20:11:35.417 回答