将mysql数组与mysql中的select语句进行比较

时间:2013-07-26 19:59:43

标签: php mysql

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

我有一个简单的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'];

说我的身份证号码是1,而fileName是“mobi。”

我想在此查询中针对另一个fileName检查我的fileName。

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

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

4 个答案:

答案 0 :(得分:0)

鉴于您知道文件名,为什么不这样做:从应用程序中选择count(*),其中filename = $ filename

如果你想确保文件名永远不会重复,那么就在它上面创建一个唯一的索引。

答案 1 :(得分: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”赋给变量。

答案 2 :(得分: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.")";

答案 3 :(得分: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';
}