到目前为止,这是我的PHP代码
<?php
$con=mysqli_connect("host","user","pass","db_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result_set = mysqli_query($con,"SELECT points FROM total WHERE id = 1");
$row = mysql_fetch($result_set);
$old_total = $row['total'];
$new_total = $old_total + $_REQUEST['total'];
mysqli_query($con,"UPDATE total SET points = $new_total WHERE id = 1");
mysqli_close($con);
?>
当我运行它时,它返回此错误: 调用未定义的函数mysql_fetch()我错过了什么吗?
答案 0 :(得分:1)
应该是mysqli_fetch
而不是mysql_fetch
。
答案 1 :(得分:1)
你必须使用
$row = mysqli_fetch_assoc($result_set);
而不是
$row = mysqli_fetch($result_set);
希望它有用......
答案 2 :(得分:1)
你应该使用$ row ['points']代替$ row ['total']和mysqli_fecth_array / assoc。
尝试以下代码:
<?php
$con=mysqli_connect("host","user","pass","db_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result_set = mysqli_query($con,"SELECT points FROM total WHERE id = 1");
$row = mysqli_fetch_assoc($result_set);
$old_total = $row['points'];
$new_total = $old_total + $_REQUEST['total'];
mysqli_query($con,"UPDATE total SET points = $new_total WHERE id = 1");
mysqli_close($con);
?>
答案 3 :(得分:0)
最终守则:
$result_set = mysqli_query($con,"SELECT points FROM total WHERE id = 1");
$row = mysqli_fetch_assoc($result_set);
$old_total = $row['points'];
$new_total = $old_total + $_REQUEST['total'];
mysqli_query($con,"UPDATE total SET points = $new_total WHERE id = 1");
mysqli_close($con);
?>