Shoutbox

PHP Help - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP Help (/showthread.php?tid=53145)

PHP Help by DragonX on 11-21-2005 at 05:41 AM

Alright, i'm making my own site in wish i use php/mysql so therefore i need to make a connection. Since my connection details changes from my pc to my host i though of making a conn.php file to include in my other files which i could change. It looks something like this...

conn.php

code:
<?php
  $conn = mysql_connect("localhost", "user", "pass") or die ("Problem connecting to Database");
  mysql_select_db("dbname", $conn);

  .....
?>

other.php
code:
<?php
  include (conn.php);

  .....
?>

but when i do:
code:
$query_string = "select * from table";
$result = mysql_query($query_string, $conn);
it gives an error that $conn isn't declared. So i was just looking for some insight, or if there's a better way of doing it.

Thx in advance :)
RE: PHP Help by ipab on 11-21-2005 at 06:57 AM

i believe that once you are including conn.php you dont need to keep connecting to the db over and over again, therefore

code:
$result = mysql_query($query_string, $conn);

should actually be
code:
$result = mysql_query($query_string);

and acutally even in conn.php remove $conn from
code:
mysql_select_db("dbname", $conn);

RE: PHP Help by Ezra on 11-21-2005 at 07:49 AM

connection.inc.php

code:
<?php
function opendb(){
    mysql_connect("localhost", "username", "password") or die("Can't Connect to database, try again later");
    mysql_select_db("database");
}
?>


index.php
code:
<?
include("connection.inc.php");
opendb();

$sql = "INSERT INTO `table` (`id`, `name`) VALUES ('".$id."', '".$name."')";
mysql_query($sql) or die (mysql_error());
?>

That's how I always do it with my websites.

EDIT: Added the mysql_query bit.
RE: PHP Help by J-Thread on 11-21-2005 at 09:15 AM

A better way to do it:

code:
<?php
// Connection info
$database['username'] = "username";
$database['password'] = "pass";
$database['database'] = "database";
$database['host'] = "localhost";


// Database connect code

$database['link'] = false;
function query($query) {
   global $database;
   
   if(!$database['link']) {
      make_connection();
   }
   return mysql_query($query, $database['link']);
}

function make_connection() {
   global $database, $talen;

   $database['link'] = mysql_connect($database['host'], $database['username'], $database['password']);
   
   if(!$database['link']) {
      die("Could not connect to server " . $database['host']);
   }
   if(!mysql_select_db($database['database'], $database['link'])) {
      die("Database \"" . $database['database'] . "\" wasn't found!");
   }
}
?>

Include this in all your scripts (I always make an include file in which I include everything) and when you need to acces the database just do:
code:
$result = query("INSERT INTO ...");

The code will never make more then 1 database connection, wil never make a connection when you don't need it, AND it works with multiple connections.

Just be sure you doesn't use $database somewhere else in the script, and off course use query instead of mysql_query everytime.

Besides that, you can better use require instead of include, because you're sure your scripts need the database... So if the file doesn't exist your script doesn't need to be executed.
RE: PHP Help by DragonX on 11-21-2005 at 09:15 AM

Ah ha, it works :)

Thx a bunch ppl :D


RE: PHP Help by ipab on 11-21-2005 at 04:37 PM

thats some handy code there J-Thread :)

i learnt something new today


RE: PHP Help by J-Thread on 11-21-2005 at 05:29 PM

That's why we're here, to learn things(Y).

I also always learn when I read here, even with PHP:D