jeppers Posted February 18, 2011 Share Posted February 18, 2011 <?php function dbConnect($usertype, $connectionType = 'mysqli') { $host = 'localhost'; $db = 'gallery'; if ($usertype == 'read') { $user = 'psread'; $pwd = '####'; } elseif ($usertype == 'write') { $user = 'pswrite'; $pwd = '####'; } else { exit('Unrecognized connection type'); } if ($connectionType == 'mysqli') { return new mysqli($host, $user, $pwd, $db) or die ('Cannot open database'); } } ?> when i call upon the function i get this message Fatal error: Call to a member function query() on a non-object in C:\webs\phpsolutions\comments.php on line 8 from this code <?php require_once('includes/connection.php'); // connect to MySQL $conn = dbConnect('write'); // prepare the SQL query $sql = 'SELECT * FROM images'; // submit the query and capture the result $result = $conn->query($sql) or die(mysqli_error()); // find out how many records were retrieved $numRows = $result->num_rows; ?> Quote Link to comment https://forums.phpfreaks.com/topic/228124-can-any-one-tell-me-whats-wrong-with-this-function/ Share on other sites More sharing options...
ManiacDan Posted February 18, 2011 Share Posted February 18, 2011 Remove the "or die" from your function return, that's wrong. When you do that, you end up returning a boolean, which is not an object. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/228124-can-any-one-tell-me-whats-wrong-with-this-function/#findComment-1176413 Share on other sites More sharing options...
jeppers Posted February 18, 2011 Author Share Posted February 18, 2011 thanks so very much been stuck on that for some time.... Quote Link to comment https://forums.phpfreaks.com/topic/228124-can-any-one-tell-me-whats-wrong-with-this-function/#findComment-1176417 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.