相当于mysqli中的mysql_result

时间:2016-03-09 08:02:29

标签: php mysql mysqli

我一直试图从MySQL获取我的PHP代码到MySQLi,但是当涉及mysql_result转换时我遇到了问题。

我一直在阅读有关该主题的内容,我尝试了一些建议,但我无法得到任何建议。

我对PHP也很陌生,所以不要指望我这么做:P

我在拥有php版本5.3.3的Centos 6上托管我的apache

这是我如何使用mysql_reslut

$domain = mysql_query("SELECT `vsite` FROM `server1` WHERE `id` = '$id';");

$domain1 = mysql_result($domain);

所以我的问题是,mysqli_result不适用于我的php版本或者它不存在。

错误:

PHP Fatal error:  Call to undefined function mysqli_result() in /var/www/xxx/xx

3 个答案:

答案 0 :(得分:1)

您需要使用mysqli_fetch_assoc()

示例:

$domain = mysqli_query($conn,"SELECT `vsite` FROM `server1` WHERE `id` = '$id'");
while($row = mysqli_fetch_assoc($domain)) {
   // your stuff
}

另请注意,此处$conn是您的连接link identifier

旁注:

停止使用已弃用的mysql_*扩展程序并在PHP 7中关闭,请使用mysqli_*PDO

答案 1 :(得分:0)

如果你是新手,你的代码中会有一些陷阱。您正在使用在新版本的PHP版本中删除的界面。 不要再使用它了并且最好绑定params,因为Sql Injection。所以只需使用此代码即可。还包括一些错误处理。

// create connection to DB_NAME
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);    

// prepare the statement
$stmt = $mysqli->prepare("SELECT `vsite` FROM `server1` WHERE `id` = ?");
// if there was an error in the statement output it and die
if (!$stmt) {
    echo $mysqli->error;
    exit();
}
// bin $id as string param more variables like ("ss",$id,$id2)
$stmt->bind_param("s", $id);
// execute the statement
$stmt->execute();
// bind the result
$stmt->bind_result($domain);
// assuming only on result will come if not use a while($stmt->fetch()) loop
$stmt->fetch();

echo $domain; // just outputs the `vsite`

// close the statement and connection
$stmt->close();
$mysqli->close();

答案 2 :(得分:-1)

问题是没有你想要的精确mysqli_result()函数,这很容易返回结果。但是,我找到了一个便利函数at this link,它复制了你正在寻找的功能:

function mysqli_result($res,$row=0,$col=0){ 
    $numrows = mysqli_num_rows($res); 
    if ($numrows && $row <= ($numrows-1) && $row >=0){
        mysqli_data_seek($res,$row);
        $resrow = (is_numeric($col)) ? mysqli_fetch_row($res) : mysqli_fetch_assoc($res);
        if (isset($resrow[$col])){
            return $resrow[$col];
        }
    }
    return false;
}