Jump to content


Photo

PHP Include


  • Please log in to reply
14 replies to this topic

#1 ItsWesYo

ItsWesYo
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 05 July 2006 - 02:13 PM

I really must be annoying you all now with my questions :-[ I'm such a php newbie  ;D

Anyway. Instead of having all of this 'connecting-to-mysql' coding everywhere, I want it on one page, where I can include it wherever I want.

I've tried it before and it comes up with errors.

Any help?

I just need it to have it connecting to the db with the user/pass and such.

#2 mrwhale

mrwhale
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 05 July 2006 - 02:16 PM

here:


mysql.php
----------------
<?php

// put all the stuff u wanna include here

?>


yourfile.php
----------------
<?php

include_once( "mysql.php" );

// your files contents

?>


#3 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 05 July 2006 - 02:18 PM

Simple:
<?PHP
$host="localhost" //Hostname (default is localhost)
$login="user"; //username to db
$pwd="pass"; //users pass to db
$db="dbname"; //dbname

$conn = mysql_connect("$host", $login, $pwd) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
?>


#4 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 05 July 2006 - 02:19 PM

very simple
just create a php page and call it something like "config.php"

put your connection info in it, like:
<?php

    $dbhost = "localhost"; 
    $dbuser = "root"; 
    $dbpassword = "password"; 
    $dbname = "myDatabase"; //you could leave this out if you use mulitple databases.
	
mysql_connect($dbhost, $dbuser, $dbpassword) or die("Couldn't establish connection");
mysql_select_db($dbname); //again, leave this out if you are using multiple databases.

 ?>

then on any page that needs to query the database just use the include() function:

<?php 
include('config.php');

//query stuff
$query = mysql_query("SELECT * FROM table WHERE column = '$option' ");
........ etc .......

?>

"Confidence in the face of risk."

#5 ItsWesYo

ItsWesYo
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 05 July 2006 - 02:48 PM

Thanks guys.

Yeah, I knew how to connect and stuff.

Everytime I add the mysql coding to it, I get errors.

#6 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 05 July 2006 - 02:50 PM

Can we see?

#7 ItsWesYo

ItsWesYo
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 05 July 2006 - 02:55 PM

EDIT: Alright ......

http://www.evermoref...om/wes/test.php


The code on the page:
<?php
include ("header.php");
include_once( "mysql.php" );
$query = mysql_query("SELECT * FROM items");
while($r=mysql_fetch_array($result))
{	
   $name=$r["name"];

echo "

$name<br>

";
}
?>


#8 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 05 July 2006 - 03:01 PM

Just a side note, I always put my db connect files in a directory above the root level just in case someone were to be able to bypass my index and see the file listed. Just a little security tip once you have your main problem resolved.

I call it like this:

include ('../includes/yourconnection.php')



#9 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 05 July 2006 - 03:06 PM

The error looks like there is something wrong in your mysql.php. It says line 3, but most likely before that. Check for a missing ";" or something with the syntax. Or post your code for that and we can see if there is something else missing.

#10 ItsWesYo

ItsWesYo
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 05 July 2006 - 03:10 PM

Nevermind freakus. Xyn forgot ";" and that's the code I took.

I get a new error now.

#11 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 05 July 2006 - 03:12 PM

Ohh yeah sorry :P I didn't spot that.

#12 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 05 July 2006 - 03:12 PM

Why not Post your query, Then your error and maybe can help...

#13 ItsWesYo

ItsWesYo
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 05 July 2006 - 03:14 PM

mysql.php:
<?PHP
$host="localhost";
$login="hidden";
$pwd="hidden";
$db="hidden";

$conn = mysql_connect("$host", $login, $pwd) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
?>

test.php:
<?php
include ("header.php");
include_once("mysql.php");
$query = mysql_query("SELECT * FROM items");
while($r=mysql_fetch_array($result))
{	
   $name=$r["name"];

echo "

$name<br>

";
}
?>

the error says:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/evermore/public_html/wes/test.php on line 5

#14 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 05 July 2006 - 03:17 PM

Here's the problem:

$query = mysql_query("SELECT * FROM items");
while($r=mysql_fetch_array($result))

Should be:

$query = mysql_query("SELECT * FROM items");
while($r=mysql_fetch_array($query))


#15 ItsWesYo

ItsWesYo
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 05 July 2006 - 03:23 PM

Thanks guys, it worked!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users