gpt4 book ai didi

PHP mysql 页面查看计数器

转载 作者:行者123 更新时间:2023-11-29 12:17:49 25 4
gpt4 key购买 nike

我尝试制作一个简单的页面浏览计数器。每次刷新页面时,数字都会增加+1。

当我在 Phpmyadmin 中执行代码时,一切正常。但在 php 中,计数器被返回,但不增加。我做错了什么?

<?php
$username = "username";
$password = "password";
$hostname = "hostname";
$dbname = "dbname";


// Create connection
$conn = new mysqli($hostname, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "UPDATE `Games` SET `Played`= `Played`+1 WHERE 'ID'='3'";

$sql = "SELECT `Played` FROM `Games` WHERE `ID` = 3";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "" . $row[Played]. "<br>";
}
} else {
echo "0 results";
}

$conn->close();
?>

最佳答案

$sql 变量中创建 UPDATE 字符串后,您必须调用 mysqli_query 。您现在所做的只是使用 SELECT 查询字符串覆盖它。

[...]

$sql = "UPDATE `Games` SET `Played`= `Played`+1 WHERE `ID`='3'";

$result = mysqli_query($conn, $sql);

$sql = "SELECT `Played` FROM `Games` WHERE `ID` = 3";

$result = mysqli_query($conn, $sql);

[...]

关于PHP mysql 页面查看计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29493729/

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