Jump to content

PHP Include


ItsWesYo

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.
Link to comment
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]
Link to comment
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]
Link to comment
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]
Link to comment
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]

Link to comment
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
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.