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
https://forums.phpfreaks.com/topic/13735-php-include/
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
https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53337
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
https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53338
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
https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53365
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
https://forums.phpfreaks.com/topic/13735-php-include/#findComment-53373
Share on other sites

Archived

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

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