MySQL检查字符串是否存在以及更新或创建

时间:2014-06-11 10:38:05

标签: php mysql database phpmyadmin

我试图通过php对MySQL进行简单的if exist检查,然后我将创建一个字符串(如果不存在);或者如果存在字符串,我将更新并添加值counter = counter + 1

但现在我不知道为什么没有发生:s

我做错了什么 - 我已经找到了问题,但我没找到任何东西......

提前谢谢

继承人的代码

$rankset = '';
$steamid64 = "76561198070477917";
$localmap = "de_dolls";

mysql_connect("localhost", "######", "###############") or die(mysql_error());  
mysql_select_db("server") or die(mysql_error());

$check1 = mysql_query("SELECT * FROM `darkrp_missingmap` WHERE map = '$localmap'") or die(mysql_error());

while($info1 = mysql_fetch_array( $check1 )) 
{ 
  if(mysql_num_rows($info1) == 0) {
    print "existiert nicht wird erstellt...";
    mysql_query("SELECT server INSERT INTO darkrp_missingmap (map, count) VALUES ('$localmap', '1')");
  } 
  else {
    print "existiert wird aufgestuft...";
    mysql_query("SELECT server UPDATE darkrp_missingmap SET count = count + 1 WHERE map = '$localmap'");
  }
} 

4 个答案:

答案 0 :(得分:1)

当您将数组$check1传递给它时,

mysql_num_rows()将资源对象作为参数$info1

你应该这样做:

$check1 = mysql_query("SELECT * FROM `darkrp_missingmap` WHERE map = '$localmap'") or die(mysql_error());

if (mysql_num_rows($check1) === 0) {
    #no records found
    print "existiert nicht wird erstellt...";
    #rest of your code
} else if (mysql_num_rows($check1) === FALSE) {
    #query failed
} else {
    #records found
    print "existiert wird aufgestuft...";
    #rest of your code
}

使用==0时应小心谨慎,因为mysql_num_rows()会在失败时返回FALSE,这也可能与0进行松散比较。

注意:

Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial

答案 1 :(得分:0)

您可以尝试此操作,您可以参考[this]

$check1 = mysql_query("SELECT * FROM `darkrp_missingmap` WHERE map = '$localmap'") or die(mysql_error());
$nums = mysql_num_rows($check1);

if($nums == 0) {
 // do something
}

或者你可以使用这个sql。

SELECT COUNT(*) AS count FROM `darkrp_missingmap` WHERE map = '$localmap'

获取此查询的结果count

$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$nums = $row[0]['count'];

答案 2 :(得分:0)

我不认为你一次选择和插入,你也不需要 试试这个

if(mysql_num_rows($info1) >0) {

print "existiert nicht wird erstellt...";
mysql_query("INSERT INTO darkrp_missingmap (map, count) VALUES ('$localmap', '1')");

} else {

print "existiert wird aufgestuft...";
mysql_query("UPDATE darkrp_missingmap SET count = count + 1 WHERE map = '$localmap'");
}

试试并告诉

答案 3 :(得分:0)

好的,我发现我的麻烦我已经选择了数据库“服务器”,然后我试图做到这一点 INSERT INTO darkrp_missingmapmapcount)VALUES('zu_kotze','100')

这里是工作示例:     $ steamid64 =“76561198070477917”;     $ localmap =“de_dollsw”;

mysql_connect("localhost", "####", "##########") or die(mysql_error());  
mysql_select_db("server") or die(mysql_error());
//maplog

$check1 = mysql_query("SELECT * FROM `darkrp_missingmap` WHERE map = '$localmap'") or die(mysql_error());
$nums = mysql_num_rows($check1);

if($nums == 0) {
print "existiert nicht wird erstellt...";
mysql_query("INSERT INTO darkrp_missingmap (map, count) VALUES ('$localmap', '1')");
} else {
print "existiert wird aufgestuft...";
mysql_query("UPDATE darkrp_missingmap SET count = count + 1 WHERE map = '$localmap'");
}