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
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]
Link to comment
Share on other sites

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)
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

You could try amending your code from

[code]
if ($dbc=mysql_connect ("localhost", "*****", "*****")) {
if (!@mysql_select_db ('*****))
[/code]

to this:

[code]
if ($dbc=mysql_connect ("localhost", "*****", "*****")) {
if (!@mysql_select_db ("database_name", $dbc))
[/code]
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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