In mysql, we use
$userdata = mysql_fetch_assoc(mysql_query("select * tablename where id=".$_SESSION['user_id']."));
And then we can use $userdata
anywhere on that page....
How can we do it with mysqli
without using foreach or while statement ?
What I have tried is
<?
include ("db.php");
session_start();
if(isset($_SESSION['user_id']))
{
$userid = $_SESSION['user_id'];
$query = "SELECT * FROM mytable where id= '$userid'";
$userdataraw = $database->get_results($query);
$userdata = $userdataraw ->fetch_assoc();
}
?>
Class Used as :
public function get_results( $query, $object = false )
{
self::$counter++;
//Overwrite the $row var to null
$row = null;
$results = $this->link->query( $query );
if( $this->link->error )
{
$this->log_db_errors( $this->link->error, $query );
return false;
}
else
{
$row = array();
while( $r = ( !$object ) ? $results->fetch_assoc() : $results->fetch_object() )
{
$row[] = $r;
}
return $row;
}
}
When I tried This, I got error
Fatal error: Call to undefined function fetch_assoc()
I want set $userdata
without foreach
or while
, is it possible in Mysqli??