Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Retrieving Data

Sending your query to MySQL in 3 lines by Glowball
Posted: 14 Oct 01

This involves one script to be used throughout your site (and included on pages that need it).  You'll have three lines on your page to send the SQL and get the recordset.


define("SQL_HOSTNAME", "myhost");
define("SQL_USERNAME", "myusername");
define("SQL_PASSWORD", "mypassword");
define("SQL_DATABASE", "mydatabase");
$logpath = "/path/to/a/writable/directory/errorlog.txt";
$exitMsg = "<p>Sorry, this utility is currently unavailable.  Please <a href='mailto:webmaster@yoursite.com'>email the Webmaster</a> for more information.</p>";

function exitOut($logMsg,$logpath,$exitMsg) {
    error_log($logMsg . chr(13) . chr(10), 3, $logpath);
    echo $exitMsg;

if (isset($query)) {    
    if (!mysql_pconnect(SQL_HOSTNAME,SQL_USERNAME,SQL_PASSWORD)) exitOut(chr(10) . "Unable to connect to " . SQL_HOSTNAME,$logpath,$exitMsg);
    if (!mysql_select_db(SQL_DATABASE)) exitOut(chr(10) . "Unable to select database '" . SQL_DATABASE,$logpath,$exitMsg);
    if (!isset($queryname)) $queryname = "recordset";
    if (!$$queryname = mysql_query($query)) exitOut(chr(10) . "Database query failed: " . $query,$logpath,$exitMsg);
else {
    exitOut(chr(10) . "The 'query' variable was not passed to sendquery.php",$logpath,$exitMsg);


$query = "SELECT * FROM mytable";
$queryname = "getInfo";
include "/your/directory/structure/sendquery.php";

while ($info = mysql_fetch_array($getInfo)) {

You can call that three-line block repeatedly in the same file without issues.  It has very basic error message display but it wouldn't be difficult to add some Look & feel to it.

Back to PHP FAQ Index
Back to PHP Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close