Yogendra Karekar
Yogendra Karekar

Reputation: 1

session variable in select query

<?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];

   }

it is giving result for 1st echo ie email but for 2nd the error is [email protected] Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, resource given in C:\xampp\htdocs\mymainproject\upload1.php on line 9 please help

Upvotes: 0

Views: 90

Answers (3)

Eegan
Eegan

Reputation: 1

just use mysqli_query instead. i recommend switching to pdo, its easier to pass variables.

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

Upvotes: 0

StackSlave
StackSlave

Reputation: 10627

Here are a few examples:

// 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();
}
?>

Using the Object Oriented approach will save you typing. Note that you can put single dimensional Numeric Arrays inside double quotes without curly braces.

Upvotes: 0

zerkms
zerkms

Reputation: 254886

You're using mysql_query and mysqli_fetch_array functions which belong to different database drivers.

You should choose one. In this case - it's mysqli.

PS: the first curly brace { right after <?php looks weird

Upvotes: 3

Related Questions