2014-07-26 35 views
0

我不知道如何使用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"); 
+0

@mohammed'$ urll = $ RES [ 'URL']','$ OWNERID = $ RES [ 'usrid']'和'$ siteidd = $水庫[」 id']' –

+0

@AdamElsodaney - 你怎麼知道他的名字是?我不得不使用谷歌翻譯... – raidenace

+0

@raidenace我是半埃及人,我可以閱讀阿拉伯語(但不能說話);) –

回答

1

在結果使用fetch_assoc

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

如果有多個行,則:

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

如何使用$ urll = mysqli_result($ res,1,「url」);或$ urll = mysqli_result($ res,2,「url」); ? –

+0

@محمدعلیپورفتحکوهی,在更新的答案中使用while循環 – Fabricator

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

________Small Example________

假設我們有一個DB:

ID名稱年齡職業
威廉·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>"; 
}