• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

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 -->
<?php
// 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
	$result->free();
	
	return $userName;
}

Open in new window

0
kcalder
Asked:
kcalder
  • 5
  • 3
1 Solution
 
shobinsunCommented:
HI,

Replace :

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

with

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

and Try.

0
 
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.
0
 
shobinsunCommented:
Hi,

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

Use it.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
0
 
shobinsunCommented:
Hi,

To rectify:

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

Hope this helps you.

Regards
0
 
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 http://uk3.php.net/sprintf).
0
 
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.
0
 
kcalderAuthor Commented:
Thanks for your help
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now