Jump to content

Mysql connection in include


drumminlogan

Recommended Posts

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. 

Here is the code that works
[code]<?php

if ($dbc=mysql_connect ("localhost", "*****", "*****")) {
if (!@mysql_select_db ('*****))
{
die ('<p>Could not select the database because: <b>'. mysql_error() . '</b></p>');
}
} else {
die ('<p>Could not connect to MYSQL because: <b>' . mysql_error() . '</b></p>');
}

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

?>[/code]

Here is what it looks like with the include.  When pulling this page up, i get the following:

[quote]Could not retrieve the date because: No database selected. The query was SELECT * FROM 123_listings WHERE (id="153").[/quote]

[code]<?php

include ('/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();

?>[/code]

connection.php file

[code]
<?php
if ($dbc=mysql_connect ("localhost", "*****", "*****")) {
if (!@mysql_select_db ('*****))
{
die ('<p>Could not select the database because: <b>'. mysql_error() . '</b></p>');
}
} else {
die ('<p>Could not connect to MYSQL because: <b>' . mysql_error() . '</b></p>');
}
?>
[/code]

Any suggestions on what could be wrong?
Link to comment
https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/
Share on other sites

connect.php
[code]

<?php

//set database server access variables:
$host = "localhost";
$user = "username";
$password = "password";
$database = "database";

//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");

?>
[/code]

in php script:
[code]
require('connect.php');

// then code your query etc

[/code]
Have you entered the username and password etc correctly? Have you entered the correct database name?

If above all OK, try putting something simple like echo "TEST"; in the connect.php script so you can make sure you are including the connect script correctly (ie TEST should be displayed in your browser if including correctly)
The database, username, and password are all spelled correctly.  I can copy everything out the connection.php file and put it in place of the require line in the script and it works fine.

i put in an echo "test"; in the connection.php script and it appears in the browser, so it is including the connection script, but for some reason not selecting the database.
[code]<?php

//set database server access variables:
$host = "localhost";
$user = "myusername";
$password = "password";
$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");

?>[/code]
[code]
<?php
if ($dbc=mysql_connect ("localhost", "*****", "*****")) {
if (!@mysql_select_db ('*****))
{
die ('<p>Could not select the database because: <b>'. mysql_error() . '</b></p>');
}
} else {
die ('<p>Could not connect to MYSQL because: <b>' . mysql_error() . '</b></p>');
}
?>

[/code]

you are missing a ' in your mysql_select_db function. That could be the problem.
[quote author=chiprivers link=topic=118665.msg488665#msg488665 date=1166592736]
are you referencing the location of your include correctly?

ie, if it saved in an includes folder, you will need:

require('includes/connect.php');

no just

require('connect.php');
[/quote]

Yes, I am.  The include is being showed as I put a print statement witht he word hello, and the hello part showed up.
Here is the include

[code]<?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");

?>[/code]

Here is the script

[code]<?php

require('http://147.202.70.151/includes/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();

?>[/code]
I never use parenthesis with the include statement. Maybe I tried it once and it didn't work? Anyway, the manual's example doesn't use parenthesis either.

[code=php:0]include "connect.php";[/code]

Also, if the print command is working, why not put a test mysql query inside your connect.php as well. Then call it as an include, then call it directly. See what it does.

Archived

This topic is now archived and is closed to further replies.

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