What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » PHP Help

PHP Help
Author: Message:
DragonX
Full Member
***

Avatar

Posts: 226
Reputation: 10
40 / Male / –
Joined: Aug 2005
O.P. PHP Help
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 :)
[Image: dsd-mi_616175.png]
11-21-2005 05:41 AM
Profile E-Mail PM Web Find Quote Report
ipab
Veteran Member
*****

Avatar
Design Evolved

Posts: 1017
Reputation: 32
34 / Male / Flag
Joined: May 2004
RE: PHP Help
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);

This post was edited on 11-21-2005 at 07:01 AM by ipab.
56656E692C 20566964692C 2056696369
11-21-2005 06:57 AM
Profile E-Mail PM Web Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
RE: PHP Help
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.

This post was edited on 11-21-2005 at 08:02 AM by Ezra.
[Image: 1-0.png]
             
11-21-2005 07:49 AM
Profile PM Web Find Quote Report
J-Thread
Full Member
***

Avatar

Posts: 467
Reputation: 8
– / Male / –
Joined: Jul 2004
RE: PHP Help
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.
11-21-2005 09:15 AM
Profile E-Mail PM Find Quote Report
DragonX
Full Member
***

Avatar

Posts: 226
Reputation: 10
40 / Male / –
Joined: Aug 2005
O.P. RE: PHP Help
Ah ha, it works :)

Thx a bunch ppl :D
[Image: dsd-mi_616175.png]
11-21-2005 09:15 AM
Profile E-Mail PM Web Find Quote Report
ipab
Veteran Member
*****

Avatar
Design Evolved

Posts: 1017
Reputation: 32
34 / Male / Flag
Joined: May 2004
RE: PHP Help
thats some handy code there J-Thread :)

i learnt something new today
56656E692C 20566964692C 2056696369
11-21-2005 04:37 PM
Profile E-Mail PM Web Find Quote Report
J-Thread
Full Member
***

Avatar

Posts: 467
Reputation: 8
– / Male / –
Joined: Jul 2004
RE: PHP Help
That's why we're here, to learn things(Y).

I also always learn when I read here, even with PHP:D
11-21-2005 05:29 PM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On