如何更改mysql_result($ res,0,“url”);到mysqli

时间:2014-07-26 20:29:19

标签: php mysqli

我不知道如何像过去一样使用mysqli_result。

这样可以吗?怎么回事?

$res = mysqli_query($con,"SELECT `id`,`usrid`,`url` FROM site WHERE state = 'popup' AND creditsp >= 1 AND (cth < cph || cph=0) order by rand() limit 1");
$urll = mysqli_result($res, 0, "url");
$ownerid = mysqli_result($res, 0, "usrid");
$siteidd = mysqli_result($res, 0, "id");

2 个答案:

答案 0 :(得分:2)

PHP 5.4现在支持函数数组解除引用: http://php.net/manual/en/migration54.new-features.php

所以,为了从sql中获取结果,你需要使用mysqli_fetch_assoc()函数。

用法可以在这里找到: http://php.net/manual/en/mysqli-result.fetch-assoc.php http://www.w3schools.com/php/func_mysqli_fetch_assoc.asp

________小例________

假设我们有一个DB:

Id名称年龄占用
1. William 11学生
2. Uname 14学生
3. Yem 22老师

$query = "SELECT * FROM persons";
$res = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($res));  \\returns an associative array to the row variable. You can use foreach loop as well to loop throgh the various data items.
{
echo $row["Id"] . "<br>";
echo $row["Name"] . "<br>";
echo $row["Age"] . "<br>";
echo $row["Occupation"] . "<br>";
}

答案 1 :(得分:1)

对结果使用fetch_assoc

$row = $res->fetch_assoc();
$urll = $row['url'];
$ownerid = $row['usrid'];
$siteidd = $row['id'];

如果有多行,则:

while ($row = $res->fetch_assoc()) {
    // process $row
}