|
|
|
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->title."<br>";
}
else echo mysql_error()."<br>\n$sql<br>\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
| |
|
|
|