选择查询中的会话变量

时间:2014-01-07 02:59:03

标签: php

<?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=asdas@g.com 警告:mysqli_fetch_array()要求参数1为mysqli_result,在第9行的C:\ xampp \ htdocs \ mymainproject \ upload1.php中给出资源 请帮忙

3 个答案:

答案 0 :(得分:3)

您正在使用属于不同数据库驱动程序的mysql_querymysqli_fetch_array函数。

你应该选择一个。在这种情况下 - 它是mysqli

PS:{之后的第一个大括号<?php看起来很奇怪

答案 1 :(得分: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();
}
?>

使用面向对象的方法可以节省您的输入。请注意,您可以将单维数字数组放在双引号内,而不使用大括号。

答案 2 :(得分:0)

只需使用mysqli_query即可。我建议切换到pdo,它更容易传递变量。

http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/