drumminlogan Posted December 14, 2006 Share Posted December 14, 2006 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]<?phpif ($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 More sharing options...
chiprivers Posted December 14, 2006 Share Posted December 14, 2006 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 databasemysql_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 https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-141436 Share on other sites More sharing options...
drumminlogan Posted December 14, 2006 Author Share Posted December 14, 2006 Thanks for the help. i tried that and got the same message:Could not retrieve the data because: No database selected. The query was SELECT * FROM 123_listings WHERE (id="153"). Link to comment https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-141441 Share on other sites More sharing options...
chiprivers Posted December 14, 2006 Share Posted December 14, 2006 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 https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-141444 Share on other sites More sharing options...
drumminlogan Posted December 14, 2006 Author Share Posted December 14, 2006 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 https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-141449 Share on other sites More sharing options...
matto Posted December 15, 2006 Share Posted December 15, 2006 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 https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-141542 Share on other sites More sharing options...
drumminlogan Posted December 15, 2006 Author Share Posted December 15, 2006 I tried and that didn't work either. Link to comment https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-141829 Share on other sites More sharing options...
complex05 Posted December 15, 2006 Share Posted December 15, 2006 please post the code you have in your connection.php file. Link to comment https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-141833 Share on other sites More sharing options...
drumminlogan Posted December 15, 2006 Author Share Posted December 15, 2006 [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 databasemysql_select_db($database) or die ("Unable to select database: Error at Stage 02");?>[/code] Link to comment https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-141839 Share on other sites More sharing options...
complex05 Posted December 15, 2006 Share Posted December 15, 2006 [code]<?phpif ($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 https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-141843 Share on other sites More sharing options...
drumminlogan Posted December 15, 2006 Author Share Posted December 15, 2006 Nope, the ' was missed when posting on here, but it is in my script. i can copy everything exactly as it is in my connection.php file and paste in in place of where the include is at the script works fine. Link to comment https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-141853 Share on other sites More sharing options...
drumminlogan Posted December 20, 2006 Author Share Posted December 20, 2006 I still haven't been able to figure this out. Anyone have any other ideas or could it have something to do with my server? Link to comment https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-144925 Share on other sites More sharing options...
chiprivers Posted December 20, 2006 Share Posted December 20, 2006 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 justrequire('connect.php'); Link to comment https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-144930 Share on other sites More sharing options...
drumminlogan Posted December 20, 2006 Author Share Posted December 20, 2006 [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 justrequire('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 https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-144933 Share on other sites More sharing options...
chiprivers Posted December 20, 2006 Share Posted December 20, 2006 can you post your current code then, first the include and then the script Link to comment https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-144934 Share on other sites More sharing options...
drumminlogan Posted December 20, 2006 Author Share Posted December 20, 2006 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 databasemysql_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 https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-144936 Share on other sites More sharing options...
chiprivers Posted December 20, 2006 Share Posted December 20, 2006 and what is happening when you run script? Link to comment https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-144937 Share on other sites More sharing options...
drumminlogan Posted December 20, 2006 Author Share Posted December 20, 2006 I get the following:[quote]Could not retrieve the data because: No database selected. The query was SELECT * FROM 123_listings WHERE (id="153").[/quote] Link to comment https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-144938 Share on other sites More sharing options...
chiprivers Posted December 20, 2006 Share Posted December 20, 2006 Are these the correct values?[code]$host = "localhost";$user = "myusername";$password = "mypassword";$database = "mydatabase";[/code]Specifically the database variables as it is saying you not selected a database! Link to comment https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-144981 Share on other sites More sharing options...
drumminlogan Posted December 20, 2006 Author Share Posted December 20, 2006 Yes they are correct. I copy everything out of the include and paste it into the script without using the include and it works fine. Link to comment https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-145182 Share on other sites More sharing options...
chiprivers Posted December 20, 2006 Share Posted December 20, 2006 have you tried changing:[code]<?phprequire('http://147.202.70.151/includes/connection.php');?>[/code]for:[code]<?phprequire('includes/connection.php');?>[/code] Link to comment https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-145187 Share on other sites More sharing options...
drumminlogan Posted December 20, 2006 Author Share Posted December 20, 2006 Yes. Still doesn't work. Link to comment https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-145190 Share on other sites More sharing options...
chiprivers Posted December 20, 2006 Share Posted December 20, 2006 you havn't done something silly like mis-spell the name of the include file when saving? Link to comment https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-145194 Share on other sites More sharing options...
drumminlogan Posted December 20, 2006 Author Share Posted December 20, 2006 Nope. the include file shows the word hello when i include print ("hello"); in it, so that is telling me the include file is being pulled up correctly. it is just not connecting to the database. Link to comment https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-145209 Share on other sites More sharing options...
michaellunsford Posted December 20, 2006 Share Posted December 20, 2006 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 https://forums.phpfreaks.com/topic/30696-mysql-connection-in-include/#findComment-145214 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.