msgbartop
Gossimer’s Premier Web Hosting and Domain Registration Knowledgebase.
msgbarbottom

20 Aug 10 Testing MySQL database connectivity through a script

You can test connectivity to a MySQL database using the below script -

Sample Script

<?php
$dbname = ‘mysql_dbname’;

if (!mysql_connect(‘mysql_host’, ‘mysql_user’, ‘mysql_password’)) { echo ‘Could not connect to mysql’; exit; }

$sql = “SHOW TABLES FROM $dbname”;
$result = mysql_query($sql);

if (!$result) { echo “DB Error, could not list tablesn”; echo ‘MySQL Error: ‘ . mysql_error(); exit; }

while ($row = mysql_fetch_row($result)) {
echo “Table: {$row[0]}n”;
}

mysql_free_result($result);
?>

The variables used in the script are –

  • mysql_dbname – the MySQL database name
     
  • mysql_host – this needs to be specified as localhost
     
  • mysql_user – the username associated with the above MySQL database
     
  • mysql_password – the password corresponding to the above user

This script, on a successful connection, will fetch and display the names of all the tables in the specified database.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BlinkList
  • Fark
  • Faves
  • LinkedIn
  • Live
  • MySpace
  • NewsVine
  • Propeller
  • Segnalo
  • SphereIt
  • StumbleUpon
  • Yahoo! Buzz
  • Reddit
  • Simpy
  • Twitter
  • Yahoo! Bookmarks
  • RSS

Tags: , , , , , , , ,

Leave a Comment


SEO Powered By SEOPressor