eMonk Posted May 21, 2011 Share Posted May 21, 2011 The following code works: @ $db = new mysqli('localhost', 'username', 'password', 'database'); if (mysqli_connect_errno()) { echo 'Error: Could not connect to database. Please try again later.'; exit; } $query = "select * from model order by name asc"; $result = $db->query($query); However, I just started to include connect.php to my pages. Now I'm getting an error saying: "Fatal error: Call to a member function query() on a non-object on line 14" Here's my connect.php .... $db = mysql_select_db($dbase , $connection) or die ("Can't select database."); Line 14 is... $result = $db->query($query); Any ideas? I believe it's the $db variable causing the problem. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 22, 2011 Share Posted May 22, 2011 You do realize that the code you posted for connect.php is a mysql select db function call and has absolutely nothing to do with the mysqli code you are attempting to use. Quote Link to comment Share on other sites More sharing options...
eMonk Posted May 22, 2011 Author Share Posted May 22, 2011 I'm trying to replace the mysqli code so I can include connect.php like on my other pages. Complete connect.php code: <?php $hostname='localhost'; $user='username'; $pass='password'; $dbase='database'; $connection = mysql_connect("$hostname" , "$user" , "$pass") or die ("Can't connect to MySQL"); $db = mysql_select_db($dbase , $connection) or die ("Can't select database."); ?> Here's how I'm trying to include it: <?php include("connect.php"); $query = "select url, thumbnail, name, location from model order by name asc"; $result = $db->query($query); $num_results = $result->num_rows; echo "<p>Number of models found: ".$num_results."</p>"; for ($i=0; $i <$num_results; $i++) { $row = $result->fetch_assoc(); echo "<table width=\"400\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">" ; echo "<tr>" ; echo "<td width=\"50\"><a href=\"" . $row["url"] . "\"><img src=\"" . $row["thumbnail"] . "\" border=\"0\" width=\"60\" height=\"60\"></a></td>" ; echo "<td valign=\"top\"><a href=\"" . $row["url"] . "\">" . stripslashes($row['name']) . "</a><br />short desc<br />(" . stripslashes($row['location']) . ")</td>" ; echo "</tr>" ; echo "<tr>" ; echo "<td colspan=\"2\"><hr /></td>" ; echo "</tr>" ; echo "</table>" ; } $result->free(); $db->close(); ?> Quote Link to comment Share on other sites More sharing options...
mgoodman Posted May 22, 2011 Share Posted May 22, 2011 You aren't using mysqli in your connect.php file so you have to use mysql_query. Quote Link to comment Share on other sites More sharing options...
eMonk Posted May 22, 2011 Author Share Posted May 22, 2011 mgoodman, what do you mean? How and where do I do this? $result = mysql_query('SELECT * FROM table'); // is this how?? Quote Link to comment Share on other sites More sharing options...
mgoodman Posted May 22, 2011 Share Posted May 22, 2011 Yes, either that or change the mysql_connect bit to mysqli. Quote Link to comment Share on other sites More sharing options...
eMonk Posted May 22, 2011 Author Share Posted May 22, 2011 It didn't work... $connection = mysqli("$hostname" , "$user" , "$pass") Fatal error: Call to undefined function mysqli() Quote Link to comment Share on other sites More sharing options...
eMonk Posted May 22, 2011 Author Share Posted May 22, 2011 Yes, either that or change the mysql_connect bit to mysqli. Also tried this: $query = "select url, thumbnail, name, location from model order by name asc"; $result = mysql_query('select url, thumbnail, name, location from model order by name asc'); But it doesn't look right... Quote Link to comment Share on other sites More sharing options...
eMonk Posted May 22, 2011 Author Share Posted May 22, 2011 Just tried: $query = "select url, thumbnail, name, location from model order by name asc"; $result = mysql_query($query); But get this error: Fatal error: Call to a member function free() on a non-object Quote Link to comment Share on other sites More sharing options...
mgoodman Posted May 22, 2011 Share Posted May 22, 2011 You must not understand the differences between mysql and mysqli. Look over the manual pages: http://php.net/manual/en/book.mysql.php http://php.net/mysqli If that doesn't help you then you should search Google for "mysql vs mysqli". There's loads of material and examples on the first page. Quote Link to comment Share on other sites More sharing options...
eMonk Posted May 22, 2011 Author Share Posted May 22, 2011 Ah, I understand now... 2 different connect types. I guess that's what I get for looking at someone else's connect.php file. I'll stick with mysqli, thanks mgoodman! Quote Link to comment 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.