gpt4 book ai didi

php - mysql 所有查询都不能在 php 同一个文件中工作

转载 作者:行者123 更新时间:2023-11-29 05:50:56 28 4
gpt4 key购买 nike

我需要在一个文件中运行三个 php MySQL 查询,但一次可以运行两个,但并非所有三个都在运行。我分别运行它们,它们可以工作。我也使用了连接,但仍然没有用。

我尝试使用逗号和 进行连接。在等号之前,它没有帮助

同时执行两个查询,但不是三个正在执行我也使用逗号和点,但它也没有帮助。我不知道这里的问题是为什么所有查询都不能在单个 php 文件中工作

    <?php
include 'databaseconfig.php';

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$Emails = $_GET['email'];
$Cards = $_GET['card'];
$Table = $_GET['table'];
$Token_key=$_GET['Token_key'];
$Bet_amount=$_GET['Bet_amount'];
//1st query
$sql = "INSERT INTO Game_table (Email,Card_no,Table_no,Token_key,Bet_amount)
VALUES ('$Emails', '$Cards', '$Table', '$Token_key', '$Bet_amount')";
//2nd query
$sql .= " UPDATE LaborRegistered SET balance = balance - '$Bet_amount' WHERE Phone_no ='$Emails';";



if ($conn->query($sql) === TRUE) {
echo "New record created successfully";


sleep(10);
include 'push_notification.php';

function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);

$headers = array(
'Authorization:key = AIzaSyDAep_Hxa8iY5FxvlDKF1e_9Ws9EUhFCEw ',
'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}



//3rd query
$sql .= " Select Token_key From Game_table";
//this is not working
$result = mysqli_query($conn,$sql);
$tokens = array();

if(mysqli_num_rows($result) > 0 ){

while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["Token_key"];
}
}

mysqli_close($conn);
//sending notification
$message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
$message_status = send_notification($tokens, $message);
echo $message_status;


return 1;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
return 0;
}

$conn->close();
?>

我的 php 文件应该执行所有的查询

最佳答案

mysqli_query() 一次只能执行一个查询。如果你想执行很多查询,你必须使用 multi_query() .

请注意,您的查询必须用半列分隔。

例子

$sql = "INSERT INTO Game_table (Email,Card_no,Table_no,Token_key,Bet_amount)
VALUES ('$Emails', '$Cards', '$Table', '$Token_key', '$Bet_amount');";
//2nd query Notice this ------------------------------------^
$sql .= " UPDATE LaborRegistered SET balance = balance - '$Bet_amount' WHERE Phone_no ='$Emails';";

if ($conn->multi_query($sql) === true)
{
// ...
}

关于返回值,文档指出:

Returns FALSE if the first statement failed. To retrieve subsequent errors from other statements you have to call mysqli_next_result() first.


请考虑使用 prepared statementsparameters将用户的输入数据绑定(bind)到查询。他们实际上容易受到 SQL Injections 的攻击.

关于php - mysql 所有查询都不能在 php 同一个文件中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54303745/

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