2013-03-08 19 views
-5

這是我的PHP代碼到目前爲止mysql_fetch返回不定值

<?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()我失去了一些東西?

回答

2

您應該使用$行[ '點']代替$ 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); 
?> 
2

它應該是mysqli_fetch而不是mysql_fetch

1

,你必須使用

istead的

$row = mysqli_fetch($result_set); 

$row = mysqli_fetch_assoc($result_set); 

希望這將是有用的......

+0

我混合3個回答正確的代碼...感謝所有: '$ 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); ?> – user2125726 2013-03-08 05:03:37

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); 
?>