2014-01-07 215 views
0
<?php 

{ 
session_start(); 
include "dbconnect.php"; 

echo "email=".$_SESSION['email']; 
$result = mysql_query("SELECT uid FROM master WHERE emailid='{$_SESSION['email']}'"); 
while($uid = mysqli_fetch_array($result)) 
    { 
    echo $row[uid]; 

    } 

它給了第一個回波即電子郵件結果,但對於第二個錯誤是 [email protected] 警告:mysqli_fetch_array()預計參數1被mysqli_result,資源在給定的C:\ xampp \ htdocs \ mymainproject \ upload1.php on line 9 請幫忙會話變量

+2

'mysql_query','mysqli_fetch_array' - 你正在使用的功能,從不同** **擴展 – zerkms

回答

3

您正在使用屬於不同數據庫驅動程序的mysql_querymysqli_fetch_array函數。

你應該選擇一個。在這種情況下 - 它是mysqli

PS:<?php後右側的第一個大括號{看起來怪怪

0

這裏有幾個例子:

// restricted/dbconnect.php 
<?php 
function db(){ 
    return new mysqli('host', 'username', 'password', 'database'); 
} 
?> 

// mysqli::fetch_object() 
<?php 
session_start(); 
if(isset($_SESSION['email'])){ 
    include 'restricted/dbconnect.php'; $db = db(); 
    $result = $db->query("SELECT uid FROM master WHERE emailid='{$_SESSION['email']}'"); 
    if($result->num_rows > 0){ 
    while($row = $result->fetch_object()){ 
     echo "<div>column name:{$row->columnName}</div><div>other column name:{$row->otherColumnName}</div>"; 
    } 
    } 
    else{ 
    echo 'No results were found'; 
    } 
    $result->free(); $db->close(); 
} 
else{ 
    header('LOCATION:otherpage.php'); die(); 
} 
?> 

// mysqli::fetch_assoc() 
<?php 
session_start(); 
if(isset($_SESSION['email'])){ 
    include 'restricted/dbconnect.php'; $db = db(); 
    $result = $db->query("SELECT uid FROM master WHERE emailid='{$_SESSION['email']}'"); 
    if($result->num_rows > 0){ 
    while($row = $result->fetch_assoc()){ 
     echo "<div>column name:{$row['columnName']}</div><div>other column name:{$row['otherColumnName']}</div>"; 
    } 
    } 
    else{ 
    echo 'No results were found'; 
    } 
    $result->free(); $db->close(); 
} 
else{ 
    header('LOCATION:otherpage.php'); die(); 
} 
?> 

// mysqli::fetch_row() 
<?php 
session_start(); 
if(isset($_SESSION['email'])){ 
    include 'restricted/dbconnect.php'; $db = db(); 
    $result = $db->query("SELECT uid FROM master WHERE emailid='{$_SESSION['email']}'"); 
    if($result->num_rows > 0){ 
    while($row = $result->fetch_row()){ 
     echo "<div>column name:$row[0]</div><div>other column name:$row[1]</div>"; 
    } 
    } 
    else{ 
    echo 'No results were found'; 
    } 
    $result->free(); $db->close(); 
} 
else{ 
    header('LOCATION:otherpage.php'); die(); 
} 
?> 

利用面向對象的方法將節省您的打字。請注意,您可以將單維數字陣列放在雙引號內,而不使用大括號。