AllofHumanity Posted August 7, 2010 Share Posted August 7, 2010 I recently found a little guide/tutorial about OOP in php. I followed it and I was able to re-create the guide to something I can use but I'm running into a problem. I try to use the database connection from the __construnt function but it doesn't want to work. I get a Fatal error: Call to a member function execute() on a non-object in D:\allofhum\xampp\htdocs\allofhum\classes\Mysql.php on line 65. I'm not quite sure why, I would love some help. <?phprequire_once '../includes/constants.php';class Mysql { private $conn; function __construct(){ $this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There was a problem connecting to the DB'); } //End __construct function verify_Username_and_Pass($un, $pwd){ $query = "SELECT * FROM users WHERE username = ? AND password = ? LIMIT 1"; if($stmt = $this->conn->prepare($query)){ $stmt->bind_param('ss', $un ,$pwd); $stmt->execute(); if($stmt->fetch()){ $stmt->close(); return true; } } } //End verify_Username_and_Pass function checkContent($file){ print "checking"; $maxsize = 2097152; $size = $maxsize / 1024; if ((($file["type"] == "image/gif") || ($file["type"] == "image/jpeg") || ($file["type"] == "image/png")) && ($file["size"] < $maxsize)){ return true; } else print "Invalid filetype and or size.<br />Size:". $file["size"] / 1024 . "kb Max size:". $size." kb"; } //End checkContent function addContent($switch, $file, $heading, $desc){ print $switch; $target = "../images/content/"; $target = $target . basename($file['name']); if(file_exists($target . $file['name'])){ print $file['name'] . " already exists."; } else{ $success_up = move_uploaded_file($file["tmp_name"], $target); if($success_up){ print "<br /> File Uploaded"; $time = time(); $query = "INSERT INTO $switch ( `pic` , `heading` , `desc` , `time`) VALUES ( `$file[name]`, `$heading`, `$desc`, `$time` )"; $stmt = $this->conn->prepare($query); $stmt->execute(); //ERROR IS HERE $stmt->close(); return true; } } } //End addContent } //End class Mysql?> Link to comment Share on other sites More sharing options...
justsomeguy Posted August 7, 2010 Share Posted August 7, 2010 The reason that the error is here: $stmt = $this->conn->prepare($query);$stmt->execute(); //ERROR IS HERE is because the SQL query failed, so the prepare method returned false. Since $stmt got set to the return of prepare, and the return value was false, $stmt got set to false so that's why the error said it's not an object. That means there's an error in the SQL query. You can get the error message like this:$stmt = $this->conn->prepare($query) or exit($this->conn->error());In this case, the problem is that you have the values surrounded with `backticks`, they should be regular 'single quotes'; only table names, database names, etc use backticks. Link to comment Share on other sites More sharing options...
AllofHumanity Posted August 7, 2010 Author Share Posted August 7, 2010 In this case, the problem is that you have the values surrounded with `backticks`, they should be regular 'single quotes'; only table names, database names, etc use backticks.Oh I see. I'm glad was something simple. Thank you, you just made me a very happy guy. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.