mysqli problem: Trying to get property of non-object

I am calling a series of queries to a database within a page that manages the uploading of a file to a web server. The file is uploading but the mysql queries are not executing. When I call the first function containing a query I get the message "trying to get property of non-object in [path/page title]", and I cannot understand what the problem is. Code excerpts below. I know this is likely to be obvious to someone!
<!-- In the main body of the file -->
// Get the user's real name
$userName = getOwnerName();
<!-- This is the function -->
function getOwnerName()
	@ $db = new mysqli('host', 'user', 'password', 'database');
	if (!$db) die ('Could not connect to the database: '.mysqli_error($db));
	// Get the document owner's real name
	$query = sprintf("SELECT firstName, lastName FROM user WHERE user_pk = %s", $_SESSION['MM_Username']);
	$result = $db->query($query);
	$row = $result->fetch_assoc();
	$userName = $row['firstName'].' '.$row['lastName'];		
	// free up the resultset
	return $userName;

Open in new window

Who is Participating?

To rectify:

$query = sprintf("SELECT firstName, lastName FROM user WHERE user_pk = '$_SESSION[MM_Username]'");

Hope this helps you.


Replace :

$query = sprintf("SELECT firstName, lastName FROM user WHERE user_pk = %s", $_SESSION['MM_Username']);


$query = "SELECT firstName, lastName FROM user WHERE user_pk = %s", $_SESSION['MM_Username'];

and Try.

kcalderAuthor Commented:
I tried that and received the following error:
"syntax error, unexpected ','  in [.....file]"

I have always used the sprintf syntax when inserting variables into the query string when querying manually as this is the way that Dreamweaver does it.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


$query = sprintf("SELECT firstName, lastName FROM user WHERE user_pk = $_SESSION['MM_Username']");

Use it.

kcalderAuthor Commented:
When the query is configured like that the following error is returned

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
kcalderAuthor Commented:
No I'm afraid that doesn't work. I get the following error using that syntax:
Warning: mysqli::query() [mysqli.query]: Couldn't fetch mysqli

I really don't see how the syntax of my query is incorrect. It reads correctly in Dreamweaver and the other syntax suggestions you have made do not seem to work using the sprintf format (see
kcalderAuthor Commented:
I am not getting any further on this and shall close the question later today if I cannot make any progress with it.
kcalderAuthor Commented:
Thanks for your help
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.