0

i am trying to make an simple if exist check for MySQL via php and then i will create an String if not exist; or if an string exist, i will update and add to the value counter = counter + 1

But now i dont now why nothing happend :s

What i am doing wrong --- i had search for the problem but i didnt has find anything..

thanks ahead

heres the code

$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'");
  }
} 
lighter
  • 2,808
  • 3
  • 40
  • 59
ProJaCore
  • 42
  • 8
  • u need update statement here instead this SELECT server INSERT INTO darkrp_missingmap (map, count) VALUES ('$localmap', '1' – M.chaudhry Jun 11 '14 at 10:43

4 Answers4

1

mysql_num_rows() takes a resource object as a parameter which is your $check1 while you are passing an array $info1 to it.

You should be doing:

$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
}

You should be careful about using ==0 because mysql_num_rows() returns FALSE on failure which can also be loosely compared to 0.

Note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
  • 1
  • 1
AyB
  • 11,609
  • 4
  • 32
  • 47
  • So why not show him the right way, if `==0` is wrong? – Madara's Ghost Jun 11 '14 at 11:04
  • @SecondRikudo I would have wanted to do that but I wasn't 100% sure whether he is checking for query failure or no records. I could have assumed though with the print statement if it wasn't in a different language. Anyways, I think it's worth pointing out. – AyB Jun 11 '14 at 11:08
  • Google translate tells me that "existiert nicht" means "does not exists". So I am guessing that `===` is appropriate. – Madara's Ghost Jun 11 '14 at 11:09
0

You can try this, you can ref [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
}

Or you can use this sql.

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

And get this query's result count.

$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$nums = $row[0]['count'];
lighter
  • 2,808
  • 3
  • 40
  • 59
0

i dont think you select and insert at one time and you dont need that either try this

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'");
}

try this and tell

M.chaudhry
  • 651
  • 1
  • 6
  • 13
  • yeah i know i have tried some codes on phpmyadmin and yeah helpful this thing :) but always thankk you :) – ProJaCore Jun 11 '14 at 11:07
0

Ok i have found my trouble i have already select the db "server" and then i tried to make this INSERT INTO darkrp_missingmap(map, count) VALUES ('zu_kotze', '100')

Here the working example: $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'");
}   
ProJaCore
  • 42
  • 8