mysqli_fetch_object() issue [solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

mysqli_fetch_object() issue [solved]

Post by social_experiment »

The code below is self-explanatory and the problem is that var_dump($obj) is returning NULL; database connection is ok, query is fine when i print it to the browser. The commented section where the mysqli_error() function returns no errors; i'm confused about where the issue is and would appreciate any ideas about possible causes. Thanks in advance.

Code: Select all

<?php
protected function _retrieveIndividualSurveyPId($subjectNum)
	{
		$subjectNum = trim($subjectNum);
		
		$query = "SELECT `parentsubjectid` FROM `is_table` WHERE 
		`subjectnum` = '" . mysqli_real_escape_string($this->_dbResource, 
		$subjectNum) . "' ";
		$sql = mysqli_query($this->_dbResource, $query);
		//
/*
$sql = mysqli_query($this->_dbResource, $query) or 
		die(mysqli_error($this->_dbResource));
*/
		
		$obj = mysqli_fetch_object($sql);
		
		var_dump($obj);		
	}
?>
Last edited by social_experiment on Tue Jun 12, 2012 7:28 am, edited 1 time in total.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: mysqli_fetch_object() issue

Post by Celauran »

What about var_dump($sql) one line above the fetch_object call? Is that returning mysqli_result like it should?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: mysqli_fetch_object() issue

Post by social_experiment »

It looks like it

Code: Select all

<?php
var_dump($sql);
// returns 
// object(mysqli_result)#4 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) } 
?>
Edit
If i use execute the code without the WHERE clause the problem disappears;
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: mysqli_fetch_object() issue

Post by social_experiment »

Nm. The value i'm searching for seems to be missing from the database. :oops:
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply