I have this php codes which have a lot of mysql_query and or die ( mysql_error() )
how can i lessen the mysql_query or is the or die ( mysql_error() ) really need for my codes?
Im going to paste my codes here.
$sqlScore = "UPDATE game SET currentTurn = '1', remainingTiles='".$remainingTiles."' WHERE gameID = '".$gameID."'";
mysql_query( $sqlScore ) or die ( mysql_error() );
$ScoreSelect = set_mysql( "SELECT * FROM game WHERE gameID = '".$gameID."'" );
$up_gameID = $ScoreSelect['gameID'];
$set_p1 = $ScoreSelect['player1'];
// CHECK IF GAMEID EXIST FOR SCORE
$checkData = mysql_query( "SELECT * FROM score WHERE gameID = '".$gameID."' " ) or die ( mysql_error() );
$rcd = mysql_fetch_array( $checkData );
$rcd['gameID'] == $gameID
? mysql_query( "UPDATE score SET score = '".$score."' WHERE gameID = '".$gameID."' " ) or die ( mysql_error() )
: mysql_query( "INSERT INTO score VALUE( '', '".$gameID."', '".$player1."', '".$score."')" ) or die ( mysql_error() );
// INSERT SQL WORD DATA
$sqlWD = mysql_query( "INSERT INTO word_data VALUE( '', '".$gameID."', '', '".$worddata."')" ) or die ( mysql_error() );
// CHECK FOR TILE
$sqlCheckRow = mysql_query( "SELECT * FROM wctilerack WHERE gameID = '".$up_gameID."' AND email = '".$up_email_player1."' " ) or die ( mysql_error() );
if ( mysql_num_rows( $sqlCheckRow ) == "1" ) {
// INSERT wctilerack
$sqlTileUP = mysql_query( "UPDATE wctilerack SET tiles = '$playerRack' WHERE gameID = '".$up_gameID."' AND email = '".$up_email_player1."' ") or die ( mysql_error() );
} else {
// INSERT wctilerack
$sqlTileINSERT = mysql_query( "INSERT INTO wctilerack VALUE('', '".$up_gameID."', '".$up_email_player1."', '$playerRack' ) ") or die ( mysql_error() );
}
// UPDATE lastPlayed
$sqlUpdatePlayed = mysql_query( "UPDATE user SET lastPlayed=NOW() WHERE email = '$up_email_player1' " ) or die ( mysql_error() );
// UPDATE word
$sqlUpdateWord = mysql_query( "UPDATE game SET lastWord = '".$xword[2]."', lastPoints='".$score."' WHERE gameID = '$up_gameID' " ) or die ( mysql_error() );
//$sqlInsertWord = mysql_query( "INSERT INTO word_data VALUE( '', '".$up_gameID."', '', '".$xword[2]."' )" ) or die ( mysql_error() );
/*$sqlCheckSK = mysql_query( "SELECT * FROM gameTileSkins WHERE gameID = '$gameID' AND email = '$up_email_player1' " ) or die ( mysql_error() );
if ( mysql_num_rows( $sqlCheckSK ) == '1' ) {
$skUP = mysql_query( "UPDATE gameTileSkins SET tileSkin='$tileSkinID' WHERE gameID = '$gameID' AND email = '$up_email_player1' " ) or die ( mysql_error() );
} else {
// INSERT gameTileSkins
$sqlSK = mysql_query( "INSERT INTO gameTileSkins VALUE('', '$gameID', '$up_email_player1', '$tileSkinID')" ) or die ( mysql_error() );
}*/
if you noticed lots of query called.
its a game application that fetch all the records and update.
is there a way that we can clean the codes.
If this code is working and years of bug fixes you should not change it as little as possible.
I recommend you grep through you code and find ‘mysql_query’ calls. And replace it with a similar function first.
should be replaced with
and
handle_queryfunction should be defined asAfter that run your tests. If all goes well. Change
handle_queryfunction body.You can use sed/grep. Assuming your code is in
junk_code.phpyou can use this command to get read of thoseor die (mysql_error());But dont forget to take backup before playing with such legacy code. These codes has long years experiences in it. May be lots of bug fixes. You surely dont want to break it.