Link to home
Start Free TrialLog in
Avatar of kcalder
kcalderFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

Avatar of shobinsun
shobinsun
Flag of India image

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.

Avatar of kcalder

ASKER

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.
Hi,

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

Use it.

Avatar of kcalder

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of shobinsun
shobinsun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kcalder

ASKER

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).
Avatar of kcalder

ASKER

I am not getting any further on this and shall close the question later today if I cannot make any progress with it.
Avatar of kcalder

ASKER

Thanks for your help