WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Newbie Notes #2 - Don't keep on writing the same code to connect to MySQL
Categories : PHP, Databases, MySQL, Beginner Guides Click here to Update Your Picture
Simon Booth
Date : Apr 21st 2004
Grade : 3 of 5 (graded 2 times)
Viewed : 7096
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Simon Booth
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

NewbieNotes is a little series of tips for people who are new to PHP to give them a few handy tips that the more experienced of us use often

I use this one all the time...

A little function saves you lots of lines and time in a database-heavy script.

When you're new at this you're quite likely to keep on writing the same thing over and over again. Wrap it up in one nice little function and call the fucntion instead.

<?php
// alter the following four lines to your liking

$sql_host = "localhost";
$sql_user = "username";
$sql_pass = "password";
$sql_data = "database";

function
open_mysql_link()
  {
  if(
$link = mysql_pconnect($GLOBALS["sql_host",
           
$GLOBALS["sql_user"], $GLOBALS["sql_pass"]))
    {
    if(!
mysql_select_db($GLOBALS["sql_data"], $link))
      {
     
$link = 0;
      }
    }
  return
$link;
  }
?>


Armed with this trifle you can open a link to MySQL, connect to your database and get on with the rest of it without introducing hard to find bugs from the odd typo.

Usage Example:

<?php
if($link = open_mysql_link())
  {
 
$sql = "select program, time, channel from tv";
  if(
$res = mysql_query($sql, $link))
    {
    while(
$row = mysql_fetch_object($res))
      {
     
// Print tonight's TV listing
     
}
    else echo
mysql_error()."<br>\n$sql<br>\n";
    }
  else echo
mysql_error()."<br>\n";
  }
?>


Note that it's important to check what you got back from a call to MySQL. You often see code that never checks if things went as expected. Your database might have stopped running for some reason, you might have an error in your SQL - a multitude of things can stops something as simple as the above functioning.

CHECK what you get back is what you should have back! It's all detailed in the manual.



email new items in db
Categories : PHP, Email, Databases, MySQL, Beginner Guides
Specify your connection settings and create a link to a MySQL database.
Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides
Convert a File database into MySQL
Categories : PHP, Filesystem, Databases, MySQL, Beginner Guides
mySQL/PHP/search with multientry form and table output with colored rows
Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases
Newbie Notes #4 - Trapping dumb MySQL query errors
Categories : PHP, Databases, MySQL, Debugging, Beginner Guides
This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
for each record, do this to the first record, and do that to any subsequent record
Categories : PHP, Databases, MySQL, Beginner Guides
bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
How to Insert a Date Format Into MySQL from PHP
Categories : PHP, Databases, MySQL, Date Time, Beginner Guides
Newbie Notes #10 - Generating drop downs
Categories : PHP, MySQL, HTML, Beginner Guides, Databases
Making a simple Hit-Log using PHP and MySql
Categories : PHP, Log Files, Beginner Guides, Databases, MySQL
Cut your MySQL Connections to 1 line of code
Categories : PHP, Beginner Guides, Databases, MySQL
Point and Click Interface ala MS Access for creating SQL statements.
Categories : MySQL, Complete Programs, General SQL, PHP, Databases
Message of the Day - Random Message (Needs MySQL!)
Categories : Databases, HTML and PHP, PHP, MySQL
Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs.
Categories : Databases, PHP, MySQL, Complete Programs
 Top Up wrote : 1102
Thats really cool I just got to know mysql_fetch_object ;p
I liked your code (though I would use mysql_connect instead of mysql_pconnect since the second kills the server after many requests).. and here is your code after my editings:
function open_mysql_link(){
    global $db;
    if($link = mysql_connect($db[`server`], $db[`username`], $db[`password`]))
        if(!mysql_select_db($db[`database`], $link)) $link = 0;
    return $link; 
}

$sql = "select * from page";
if($res = mysql_query($sql, $link_id))
    while($row = mysql_fetch_object($res)){
        echo $row-&gt;title."&lt;br&gt;";
    }
    else echo mysql_error()."&lt;br&gt;\n$sql&lt;br&gt;\n";
 
 Simon Booth wrote :1105
Using connect rather than pconnect you should mysql_close($link) at the end

Actually, although not illustrated above you should also free the result when you`re done with it