gpt4 book ai didi

PHP mysql im 寻找好的格式和消除乱码

转载 作者:行者123 更新时间:2023-11-29 02:02:33 26 4
gpt4 key购买 nike

我有这个 php 代码,其中有很多 mysql_queryor die ( mysql_error() )

我如何减少 mysql_query 或 or die ( mysql_error() ) 是否真的需要我的代码?

我要在这里粘贴我的代码。

$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() );
}*/

如果您注意到调用了很多查询。它是一个游戏应用程序,可以获取所有记录并进行更新。有没有一种方法可以清理代码。

最佳答案

首先:切换到PDOMySQLi .不再推荐 mysql 函数。来自 PHP 手册:

Use of this extension (mysql) is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used.

就实际清理代码而言,您应该研究 Don't Repeat Yourself 的概念.用于常见操作(连接、查询等)的包装类将帮助您稍微清理一下。创建您自己的包装器类是一种很好的学习方式,它将帮助您抽象化大多数常见的数据库查询。

关于PHP mysql im 寻找好的格式和消除乱码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12349481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com