Jump to content

Archived

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

ItsWesYo

PHP Include

Recommended Posts

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.

Share this post


Link to post
Share on other sites
here:


[code]mysql.php
----------------
<?php

// put all the stuff u wanna include here

?>


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

include_once( "mysql.php" );

// your files contents

?>[/code]

Share this post


Link to post
Share on other sites
Simple:
[code=php:0]<?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());
?>[/code]

Share this post


Link to post
Share on other sites
very simple
just create a php page and call it something like "config.php"

put your connection info in it, like:
[code]<?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.

?>[/code]

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

[code]<?php
include('config.php');

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

?>[/code]

Share this post


Link to post
Share on other sites
Thanks guys.

Yeah, I knew how to connect and stuff.

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

Share this post


Link to post
Share on other sites
EDIT: Alright ......

http://www.evermoreforums.com/wes/test.php


The code on the page:
[code]<?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>

";
}
?>[/code]

Share this post


Link to post
Share on other sites
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:

[code]include ('../includes/yourconnection.php')[/code]

Share this post


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

Share this post


Link to post
Share on other sites
Nevermind freakus. Xyn forgot ";" and that's the code I took.

I get a new error now.

Share this post


Link to post
Share on other sites
Ohh yeah sorry :P I didn't spot that.

Share this post


Link to post
Share on other sites
Why not Post your query, Then your error and maybe can help...

Share this post


Link to post
Share on other sites
mysql.php:
[code]<?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());
?>[/code]

test.php:
[code]<?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>

";
}
?>[/code]

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

Share this post


Link to post
Share on other sites
Here's the problem:

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

Should be:

[code]$query = mysql_query("SELECT * FROM items");
while($r=mysql_fetch_array($query))[/code]

Share this post


Link to post
Share on other sites

×

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.