1

what to replace mysql_result with?

im watching a tutorial now but it was made in 2011 so it is not working now. Here is the situation, I will be very grateful if you tell me what to replace mysql_result with here, and what to put in the parenthesis

function update_count() {
    global $link;
    $query  = "SELECT `count`, FROM `hits_count`";
    if($query_run = mysqli_query($link, $query)) {
        $count = mysql_result($query_run, 0, 'count');
        $count_inc = $count + 1;
        echo $count;
    } 
}
update_count();
4

4 回答 4

9

mysqli_query返回一个mysqli_result对象。从那里,您可以获得您想要的价值。

if($query_run = mysqli_query($link, $query)) {
    // This gets you one row at a time, use a while if there are multiple rows
    // while($row = mysqli_fetch_assoc($query_run)){}
    $row = mysqli_fetch_assoc($query_run);
    $count = $row['count'];
    // Do whatever with $count
}
于 2013-04-05T16:34:27.733 回答
3

用证明代码支持 tadman 的简洁答案

与 mysqli 不同,PDO有一个长期寻求的 mysql_result 等效项:

function update_count() {
    global $link;
    $query  = "SELECT `count` FROM `hits_count`";
    $stmt = $link->prepare($query);
    $stmp->execute();
    return $stmt->fetchColumn();
}
echo update_count();

任何说 mysqli 不比 PDO 差的人,都必须先尝试IN()使用准备好的语句将数组绑定到语句中。

于 2013-04-05T16:50:36.377 回答
1

根据php.net mysql_result()自 PHP 5.5.0 起已弃用。

因此,在这种情况下,您应该使用mysqli_fetch_all()中的其他方法。mysqli_result::*

于 2013-04-05T16:40:40.237 回答
0
function update_count() {
    global $link;
    $query  = "SELECT `count` FROM `hits_count` ;";
    if(($result = mysqli_query($link, $query)) && $result->num_rows >= 1) {
        $count = 0 ;
        while($row = $result->fetch_assoc()){
          $count+= $row['count'] ;
        }
        echo $count;
    } 
}

通常最好将该链接传递给函数:

function update_count($link) {
    $query  = "SELECT `count` FROM `hits_count` ;";
    if(($result = mysqli_query($link, $query)) && $result->num_rows >= 1) {
        $count = 0 ;
        while($row = $result->fetch_assoc()){
          $count+= $row['count'] ;
        }
        echo $count;
    }
}
于 2013-04-05T16:32:34.797 回答