问题
I have this PHP function which will insert urls into MySQL, but only those which didn't exist until moment of executing. I have a problem, my if condition is simply ignored, so everything goes to database, ignoring the condition. Code is here:
function storeLink($url,$gathered_from) {
global $conn;
$querycheck = "SELECT COUNT(*) FROM test WHERE link = '$url'";
$resultcheck = mysqli_query($conn, $querycheck);
$row = mysqli_fetch_array($resultcheck, MYSQLI_ASSOC);
if($row['COUNT(*)'] < 1);
{
echo "<font color='red'>".$row['COUNT(*)']."</font>";
$url = mysqli_real_escape_string($conn,$url);
$gathered_from = mysqli_real_escape_string($conn,$gathered_from);
$query = "INSERT INTO test (link, source) VALUES ('$url', '$gathered_from')";
mysqli_query($conn,$query) or die('Error, insert query failed');
}
}
I tried with MySQL count and also with PHP mysqli_num_rows, but still the same. No matter what the condition in if statement is, it simply ignores it. Help please...
回答1:
function storeLink($url,$gathered_from) {
global $conn;
$querycheck = "SELECT COUNT(*) as CNT FROM test WHERE link = '$url'";
$resultcheck = mysqli_query($conn, $querycheck);
$row = mysqli_fetch_array($resultcheck, MYSQLI_ASSOC);
if($row['CNT'] < 1) {
echo "<font color='red'>".$row['CNT']."</font>";
$url = mysqli_real_escape_string($conn,$url);
$gathered_from = mysqli_real_escape_string($conn,$gathered_from);
$query = "INSERT INTO test (link, source) VALUES ('$url', '$gathered_from')";
mysqli_query($conn,$query) or die('Error, insert query failed');
}
}
OR
function storeLink($url,$gathered_from) {
global $conn;
$querycheck = "SELECT link FROM test WHERE link = '$url'";
$resultcheck = mysqli_query($conn, $querycheck);
$row = mysqli_fetch_array($resultcheck, MYSQLI_ASSOC);
if(mysqli_num_rows($resultcheck)==0) {
echo "<font color='red'>".mysqli_num_rows($resultcheck)."</font>";
$url = mysqli_real_escape_string($conn,$url);
$gathered_from = mysqli_real_escape_string($conn,$gathered_from);
$query = "INSERT INTO test (link, source) VALUES ('$url', '$gathered_from')";
mysqli_query($conn,$query) or die('Error, insert query failed');
}
}
回答2:
try this if($row['COUNT(*)'] < 1){
instead of if($row['COUNT(*)'] < 1);{
function storeLink($url,$gathered_from) {
global $conn;
$querycheck = "SELECT COUNT(*) as TotalRow FROM test WHERE link = '$url'";
$resultcheck = mysqli_query($conn, $querycheck);
$row = mysqli_fetch_array($resultcheck, MYSQLI_ASSOC);
if($row['TotalRow'] == 0)
{
echo "<font color='red'>".$row['TotalRow']."</font>";
$url = mysqli_real_escape_string($conn,$url);
$gathered_from = mysqli_real_escape_string($conn,$gathered_from);
$query = "INSERT INTO test (link, source) VALUES ('$url', '$gathered_from')";
mysqli_query($conn,$query) or die('Error, insert query failed');
}
}
回答3:
INSERT INTO test (link, source) VALUES ('$url', '$gathered_from')
WHERE NOT EXISTS (
SELECT COUNT(*) as TotalRow FROM test WHERE link = '$url'
) LIMIT 1;
As you can see you can do whole work with just 1 query
来源:https://stackoverflow.com/questions/19976555/insert-data-if-number-of-rows-is-greater-than-0-does-not-work