mysql_result到Mysqli转换查询

时间:2016-01-19 05:51:44

标签: php mysqli

我在我的网站上使用mysqli。最初我使用的是MYSql。现在我想要替换下面的代码。

      $picid= mysql_result(mysql_query("SELECT * from actor where id='$id'"),0);

如何在mysqli中使用它    谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

Pls see this can help you

// $output = mysql_result($result,0);

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;
}

Ref : http://mariolurig.com/coding/mysqli_result-function-to-match-mysql_result/

答案 1 :(得分:0)

以下是使用MYSQLi面向对象的代码的完整示例:

<?
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

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

$sql = "SELECT * from actor where id='$id'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        //your stuff
    }    
} 
else 
{
    echo "0 results";
}
$conn->close();

?>