The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP MySQL Update Not Working
Discuss PHP MySQL Update Not Working in the PHP Development forum on Dev Shed. PHP MySQL Update Not Working PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 9th, 2013, 11:03 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 69

Time spent in forums: 14 h 5 m 2 sec
Reputation Power: 1
|
|
|
PHP MySQL Update Not Working
PHP Code:
$queryUpdate = mysql_query("UPDATE servers SET serverStatus='1', serverMessage='$serverMOTD', serverPlayersOnline='$serverPOnline', serverPlayersTotal='$serverMPlayers'") or die(mysql_error());
$serverMOTD = $stats->motd;
$serverPOnline = $stats->online_players;
$serverMPlayers = $stats->max_players;
When I use this, my rows in the database are not being updated. Well, there is one and it is serverStatus. I really think there is something wrong with using variables ($serverMPlayers).
I am not sure on what to do. Any help is greatly appreciated.
Thanks,
TJ
|

February 10th, 2013, 12:38 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
You're trying to use the variables before you've actually given them values.
|

February 10th, 2013, 03:24 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|

February 10th, 2013, 07:15 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 69

Time spent in forums: 14 h 5 m 2 sec
Reputation Power: 1
|
|
|
Thanks for the replies. But right now security isn't that important as I am going to rewrite my code later. I put my variables before the query and it still doesn't work.
|

February 10th, 2013, 07:37 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Quote: | Originally Posted by tjswebdev But right now security isn't that important as I am going to rewrite my code later. |
... famous last words.
No, seriously: This isn't "only" about security. Inserting unescaped variables is technically wrong, because the whole thing will blow up with a syntax error as soon your variables happen to contain special characters like quotes, slashes, hyphens etc. If your query doesn't do anything at all, this "tiny security issue" could very well be the reason.
So don't even start with sloppy programming. Make it right from the beginning. This includes replacing the ancient mysql_ functions with one of the contemporary extensions.
Quote: | Originally Posted by tjswebdev I put my variables before the query and it still doesn't work. |
"Doesn't work" doesn't tell us anything. What exactly happens? What's the content of the variables?
PHP Code:
var_dump($serverMOTD);
var_dump($serverPOnline);
var_dump($serverMPlayers);
Last edited by Jacques1 : February 10th, 2013 at 07:47 AM.
|

February 10th, 2013, 08:55 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 69

Time spent in forums: 14 h 5 m 2 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by Jacques1 ... famous last words.
No, seriously: This isn't "only" about security. Inserting unescaped variables is technically wrong, because the whole thing will blow up with a syntax error as soon your variables happen to contain special characters like quotes, slashes, hyphens etc. If your query doesn't do anything at all, this "tiny security issue" could very well be the reason.
So don't even start with sloppy programming. Make it right from the beginning. This includes replacing the ancient mysql_ functions with one of the contemporary extensions.
"Doesn't work" doesn't tell us anything. What exactly happens? What's the content of the variables?
PHP Code:
var_dump($serverMOTD);
var_dump($serverPOnline);
var_dump($serverMPlayers);
|
Oh. PHP is such an involving code. I'm pretty intermediate to it but this project I am doing paused me here. I'm only 16.5 so college isn't really an option at this time, so I'm stuck to reason books, tutorials, and YouTube videos. I also communicate with a few people that have been to college, but finding them is a hit an miss operation.
So enough chit-chat, I was seeing the var dump.
So is this code here correct?
PHP Code:
$serverMOTD = $stats->motd;
$serverPOnline = $stats->online_players;
$serverMPlayers = $stats->max_players;
These var dumps:
PHP Code:
var_dump($serverMOTD);
var_dump($serverPOnline);
var_dump($serverMPlayers);
Should those vars be put into the main query? Instead of having just $serverMOTD
Thanks,
And sorry from any headaches!
|

February 10th, 2013, 09:23 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 69

Time spent in forums: 14 h 5 m 2 sec
Reputation Power: 1
|
|
Alright. Amazing discovery - lol. I put the following in the body and I finally got it working - well somewhat.
PHP Code:
<?php
var_dump($serverMOTD);
var_dump($serverPOnline);
var_dump($serverMPlayers);
?>
So, I'm not sure why but only the last server is being ran through this and it updates ALL servers in the database. Not really sure why. But here is my code:
PHP Code:
foreach(array('Server', 'Stats') as $file) {
include sprintf('../MCServerStatus/Minecraft/%s.php', $file);
}
// I have my connection code here, but I excluded it.
$query = mysql_query("SELECT * FROM servers") or die(mysql_error());
$servers = array();
while($row = mysql_fetch_assoc($query)) {
array_push($servers, "{$row['serverIP']}");
}
foreach($servers as $server) {
$stats = \Minecraft\Stats::retrieve(new \Minecraft\Server($server));
if($stats->is_online){
$serverMOTD = $stats->motd;
$server;
$serverPOnline = $stats->online_players;
$serverMPlayers = $stats->max_players;
} else {
$serverMOTD = offline;
$server;
$serverPOnline = offline;
$serverMPlayers = offline;
}
}
$queryUpdate = mysql_query("UPDATE servers SET serverStatus='1', serverMessage='$serverMOTD', serverPlayersOnline='$serverPOnline', serverPlayersTotal='$serverMPlayers'") or die(mysql_error());
|

February 10th, 2013, 09:57 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Quote: | Originally Posted by tjswebdev I put the following in the body and I finally got it working - well somewhat.
PHP Code:
<?php
var_dump($serverMOTD);
var_dump($serverPOnline);
var_dump($serverMPlayers);
?>
|
That's not possible. The var_dump()s were supposed to tell us the content of the variables. That's all they do.
Please put the var_dump()s after the
PHP Code:
foreach($servers as $server) {
...
}
and post the output here.
You should also turn on your error messages and fix the syntax errors with the unquoted "offline" string:
And what is the line
supposed to do? An expression alone doesn't do anything, it's just evaluated and then discarded.
Quote: | Originally Posted by tjswebdev So, I'm not sure why but only the last server is being ran through this and it updates ALL servers in the database. Not really sure why. |
As long as you don't have a condition in your UPDATE query, the query will indeed update every single row in the table. That's how SQL works. If you only want to update certain rows, you need to actually specify them with a WHERE clause:
http://dev.mysql.com/doc/refman/5.5/en/update.html
|

February 10th, 2013, 11:59 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 69

Time spent in forums: 14 h 5 m 2 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by Jacques1 That's not possible. The var_dump()s were supposed to tell us the content of the variables. That's all they do.
Please put the var_dump()s after the
PHP Code:
foreach($servers as $server) {
...
}
and post the output here.
You should also turn on your error messages and fix the syntax errors with the unquoted "offline" string:
And what is the line
supposed to do? An expression alone doesn't do anything, it's just evaluated and then discarded.
As long as you don't have a condition in your UPDATE query, the query will indeed update every single row in the table. That's how SQL works. If you only want to update certain rows, you need to actually specify them with a WHERE clause:
http://dev.mysql.com/doc/refman/5.5/en/update.html |
I want to let you know I have got the script working. I want to thank you for your help. Do you want to know what I did?
|

February 10th, 2013, 03:16 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Quote: | Originally Posted by tjswebdev Do you want to know what I did? |
Sure.
|

February 10th, 2013, 03:18 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 69

Time spent in forums: 14 h 5 m 2 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by Jacques1 Sure. |
All I had to do was copy the line to both.
PHP Code:
foreach($servers as $server) {
$stats = \Minecraft\Stats::retrieve(new \Minecraft\Server($server));
if($stats->is_online){
$serverStatus = 1;
$serverMOTD = $stats->motd;
$server;
$serverPOnline = $stats->online_players;
$serverMPlayers = $stats->max_players;
$serverVersion = $stats->game_version;
$queryUpdate = mysql_query("UPDATE servers SET serverStatus='$serverStatus', serverMessage='$serverMOTD', serverPlayersOnline='$serverPOnline', serverPlayersTotal='$serverMPlayers', serverVersion='$serverVersion' WHERE serverIP='$server'") or die(mysql_error());
} else {
$serverStatus = 0;
$serverMOTD = '?';
$server;
$serverPOnline = '?';
$serverMPlayers = '?';
$serverVersion = '?';
$queryUpdate = mysql_query("UPDATE servers SET serverStatus='$serverStatus', serverMessage='$serverMOTD', serverPlayersOnline='$serverPOnline', serverPlayersTotal='$serverMPlayers', serverVersion='$serverVersion' WHERE serverIP='$server'") or die(mysql_error());
}
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|