I am brand new to Php and just learning. i have creating a page that pulls up data from a mysql database. The code i wrote works fine and pulls it up how i want it. However I wanted to put the mysql connection in an include. Everytime I do that it doesn't work, however if I copy that code that is in the include exactly and paste that into my script it works just fine.
Here is the code that works
<?php
require('/connection.php');
$query = 'SELECT * FROM 123_listings WHERE (id="153")';
if ($r = mysql_query ($query)) {
while ($row = mysql_fetch_array ($r)) {
if($row['buttonurl']==""){ $logo = "No Button";
}else{
$logo = "<a href='/christian/".$row['id'].".php'><img border='0' src='../online/images/".$row['buttonurl']."' width='120' height='60'></a>";
}
print "
<tr align=\"left\">
<td align=\"center\" width=\"24%\" height=\"110\"><a href=\"{$row['id']}.php\">$logo</a></td>
<td width=\"76%\" height=\"110\"><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"100%\" align=\"left\"><a href=\"/christian/{$row['id']}.php\"><b>{$row['businessname']}</b></a></td></tr></table>
<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"100%\" align=\"left\"><b>Contact:</b> {$row['firstname']} {$row['lastname']}<br>{$row['shortdescription']}</td></tr></table>
</td>
</tr>
<tr align=\"left\">
<td bgcolor=\"#cccccc\" height=\"1\" colspan=\"2\"><img height=\"1\" src=\"/images/spacer.gif\" width=\"1\"></td>
</tr>
";
}
} else {
die ('<p>Could not retrieve the data because: <b>' . mysql_error() . "</b>. The query was $query.</p>");
}
mysql_close();
?>
Here is what i have in the include connection.php file
<?php
//set database server access variables:
$host = "localhost";
$user = "myusername";
$password = "mypassword";
$database = "mydatabase";
//open connection
$connection = mysql_connect($host, $user, $password) or die ("Unable to connect: Error at Stage 01");
//select database
mysql_select_db($database) or die ("Unable to select database: Error at Stage 02");
?>
and when i run this, I get
Could not retrieve the date because: No database selected. The query was SELECT * FROM 123_listings WHERE (id="153").
The strange thing is if I copy the script out of the include and paste it in my script it works fine. Also i know that the include is working as I tried putting a print tag in the inculde and that showed up.
Any ideas what could be causing this?