Help converting mysql to mysqli

I have a script that runs everyday using a cron job but I can’t figure out why the database won’t update.

This script is from 2005, I haven’t scripted since then so learning mysqli has been difficult. I would appreciate any help.

<?php $con=mysqli_connect("domain","user","password","database"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } if ($_GET[pass] == 'password') { mysqli_query("update players set age=age + 1"); mysqli_query("update players set donator=donator - 1 where donator > 0 , $con"); mysqli_query("update players set zonedays=zonedays + 1 where zone > 0 , $con"); mysqli_query("update players set sentence=sentence - 1 where sentence > 0 , $con"); mysqli_query("update players set jailed='N' where sentence = 0 AND jailed='Y' , $con"); exit; } print "ERROR COMMAND NOT GIVEN
Email:
Password:
"; mysqli_close($con); ?>

All of your query lines are wrong syntax. You need to move the ending " to the end of the query string, right now you have it at the end of the entire line after the $con. The $con is the second parameter. You’re also missing the $con on the first query call.
This
[php]mysqli_query(“update players set donator=donator - 1 where donator > 0 , $con”);[/php]
Should be this
[php]mysqli_query(“update players set donator=donator - 1 where donator > 0”, $con);[/php]
And not that it’s critical, but you should really make all sql reserved words in upper case like this, it’s much easier to read and know exactly what is doing what.
[php]mysqli_query(“UPDATE players SET donator=donator - 1 WHERE donator > 0”, $con);[/php]
Also the code in the if() likely isn’t running either cause you forgot the ‘single’ quotes around pass in the $_GET[pass]. Needs to be like this
[php]$_GET[‘pass’][/php]
Lastly, it may not make any difference depending on how this script is accessed, but having a $_GET[‘pass’] that literally equals password is pretty stupid and virtually pointless, since any idiot could guess that.

I just changed the script and it still doesn’t work. I don’t understand what is wrong.

[php]mysqli_query(“UPDATE players SET age=age + 1” , $con);
mysqli_query(“UPDATE players SET donator=donator - 1 WHERE donator > 0” , $con);
mysqli_query(“UPDATE players SET zonedays=zonedays + 1 WHERE zone > 0” , $con);
mysqli_query(“UPDATE players SET sentence=sentence - 1 WHERE sentence > 0” , $con);
mysqli_query(“UPDATE players SET jailed=‘N’ WHERE sentence = 0 AND jailed=‘Y’” , $con);
exit;
}[/php]

Did you change the $_GET[pass] thing too like I said? Do you have error_reporting turned on? Does the page show any errors when you just visit it in the browser?
Put this at the top of the page and see if any errors show up (if you don’t already have error reporting turned on).
[php]
ini_set(‘display_errors’,1);
error_reporting(E_ALL);
[/php]

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in /home1/goosepri/public_html/arednecklife/day.php on line 14

I do have the url, user, password and database for connection set correctly. It works on another file.

The get password isn’t password, its something else. Just called password for this forum.

[php]<?php
$con=mysqli_connect(“url”,“user”,“password”,“database”);

// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

ini_set(‘display_errors’,1);
error_reporting(E_ALL);

if ($_GET[‘pass’] == ‘password’) {

mysqli_query("UPDATE players SET age=age + 1" , $con);
mysqli_query("UPDATE players SET donator=donator - 1 WHERE donator > 0" , $con);
mysqli_query("UPDATE players SET zonedays=zonedays + 1 WHERE zone > 0" , $con);
mysqli_query("UPDATE players SET sentence=sentence - 1 WHERE sentence > 0" , $con);
mysqli_query("UPDATE players SET jailed='N' WHERE sentence = 0 AND jailed='Y'" , $con);
exit; 

}
print “ERROR COMMAND NOT GIVEN

Email:
Password:
”;

mysqli_close($con);
?>
[/php]

Oh ok, I don’t use mysqli so I didn’t know that the $con comes first, then the sql string.
http://us2.php.net/manual/en/mysqli.query.php

Is there a better way to write the script other then mysqli?

I use (along with many others) PDO. I used to use mysql (not i) and found it easier to convert to PDO rather than mysqli.

Ohh okay, thanks for the information. I will have to read more about POD. My website’s scripts are written in mysql from 2005. I didn’t know which scripting language to use, mysqli or POD.

I got the script to work with the $con at the beginning. Thank you!

Sponsor our Newsletter | Privacy Policy | Terms of Service