Jump to content

Problems connecting to database


eMonk

Recommended Posts

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.

Link to comment
Share on other sites

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();

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.