FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


For PHP & MySql newbies





BlueVD
The following script is useful for people that want to have a little bit of db functionality. It will be expanded in the near future with a few more functions. Hope you find them useful.
Code:

<?php

// Version Info: DB Info File v 0.0.1A
// Original filename: main_db.php
// Author: BlueVD (bluevd@gmail.com)
// Functions List:
//               |_> db_conn_exists([bool auto_connect]) -> returns true if a mysql connection exists
//               |                                          if the connection doesn't exists, but the
//               |                                          auto_connect param is set to true, it will
//               |                                          automatically create a connection and it will
//               |                                          return the link id; otherwise it will return false;
//               |                                          Note: the auto_connect param is *optional*
//               |_> db_connect([bool force_new]) -> If no param is specified, it will try to connect to
//               |                                   the mysql database; If a connection already exists, it will
//               |                                   return true; Otherwise, it will connect and return the link
//               |                                   id. If the force_new param is set to true, it will create
//               |                                   a new connection even if one already exists and it will
//               |                                   return it's link id. On failure, it will return false;
//               |                                   Note: the force_new param is *optional*
//
// Details: Database definitions and functions;
// Comments: none

define("MYSQLH","localhost");  //MySQL Host
define("MYSQLU","user");       //MySQL Username
define("MYSQLP","pass"); //MySQL Password
define("MYSQLD","database");     //MySQL Database


//define("USER_TABLE","users");
//require_once("../security/secure.php"); ignore this line or erase it.
//it's part of my security engine, soon to be published.

function db_conn_exists(){
   @$tmpres=mysql_query("status");
   if(!$tmpres){
      if(func_num_args()==0){
         return false;
      }else{
         $auto_conn=func_get_arg(0);
         if($auto_conn){
            $db=mysql_connect(MYSQLH,MYSQLU,MYSQLP);
            if(!$db){
               return false;
            }else{
               $sdb=mysql_select_db(MYSQLD);
               if(!$sdb){
                  mysql_close($db);
                  return false;
               }else{
                  return $db;
               }
            }
         }
      }
   }else{
      return true;
   }
}

function db_connect(){
   if(func_num_args()==0){
      $db=db_conn_exists(true);
      return $db;
   }else{
      $force_conn=func_get_arg(0);
      if($force_conn){
         $db=mysql_connect(MYSQLH,MYSQLU,MYSQLP);
         if(!$db){
            return false;
         }else{
            $sdb=mysql_select_db(MYSQLD);
            if(!$sdb){
               mysql_close($sdb);
               return false;
            }else{
               return $db;
            }
         }
      }
   }
}
?>
smartpandian
Its good start... and always, reinventing the wheel..
check out Justin Vincent's ez sql
http://www.woyano.com/jv/ezsql
BlueVD
I've used ezSql... and I'm not reinventing the wheel. These are functions that are a part of my WDK. And ezSql presumes you have medium knowledge about sql in the first place. this file make no presumption of that, nor does it tend to replace the major mysql functions. It just makes it easy to handle the mysql links.
roboguyspacedude
that's good for checking if the db is connnected and a good introduction. I just use
Code:
<?
$user="";
$host="";
$password="";
$database="";
#connect to MySQL
#connect to MySQL
$rs = @mysql_connect( "$host", "$user", "$password" )
                  or die( mysql_error() );

#select the database
$rs = @mysql_select_db( "$database")
      or die( "Could not select database" );

?>


because it takes less lines and is simpler, plus I know that it will connect, there is no need for me to check.
RootRabbit
I do it the same way as roboguyspacedude. Just to keep it simply
Related topics
E-Cards with PHP & MySQL
HTTP AUTH with PHP and mySQL
Working with Excel, PHP & MySQL. Any Ideas
Installing Apache Php and MySQL [newbie tutorial]
creating guestbooks (form) with php & mysql
A very good PHP MySQL Tutorial
PHP and MYSQL on the computer
Simple php & mysql script
PHP & MySQL auth system
PHP&MySql - Is there any server side caching of queries?
Help regarding Php & MySQL
which host do u like put php&mysql?
php and mysql form
PHP-Fusion | An Open Source CMS | PHP & MySQL Based
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.