Lunarpages Internet Solutions Blog
  • Home
  • About Us
  • LunarForums
  • Free Web Tools
  • LunarMods
4

PHP Script to Test Database Connections

6 August, 2009 - Web Host Support

Need to test if a mySQL database has been setup, associated with a user and working properly?  In most cases where you get a “can not connect to database” issue when installing a script, it is caused by something in your configuration files being wrong. To help you test, create a .php file (could be named anything, such as dbtest.php) and then put this inside of it:

<?php

$host = ‘localhost’;

$user = ‘mysqluser’;

$pass = ‘mypass’;

$db = ‘databasename’;

//Don’t change below here

$conn = mysql_connect($host, $user, $pass);

mysql_select_db($db, $conn);

echo ‘<hr />anything above this linebreak is BAD!’;

Make sure you change mysqluser, mypass, and databasename to the correct variables as you set them up in your hosting account control panel.  Then, upload the test .php file into your public_html directory, and try to go to it via your browser.  If you see no errors above the line break, it is connecting successfully.  If you do see errors, then there is something wrong with the database or how you typed the information into the test script.

Remember also to use the

hostingusername_databaseusername

or

hostingusername_databasename

format (as it was told to you after creating the database or user for the database) when typing in your information.  Kudos goes to the Joomla forum where I found this script.

Either way it should be a helpful tool when it comes to troubleshooting your MySQL database issues. 

  • Billgates

    I had to use double quotes instead of single to get this to work.

  • http://www.WebHostReview1.com WebMaster Nate

    Great post and explanation! Connection strings are a huge pain point for a lot of people.

  • Shawn

    Thanks for this, going to save it for later use. :D

  • http://www.rahulnimawat.com Rahul Nimawat

    Thanks for such a useful and timely post.

Archives

Categories

View more:

  • About the Lunarpages blog
  • Best Blog Hosting Guide
  • BOTW.org 40% Discount
  • Contact Lunarpages
  • Free Online Tools
  • Free Webmaster Downloads
  • Free Wordpress Themes

Best Web Hosting

Lunarpages.com Web Hosting

Recent Posts

  • Enjoy Backup and File Sync with Sync TOGO
  • JugnooMe: Making Your Business Social Is Our Business
  • An Explanation of HTML5 for Beginners
  • How to Ensure Your Website is Optimized for Top Speed
  • Tips Every Webmaster Should Keep in Mind to Improve On-Page SEO

Recent Comments

  • jocuri kizi on Affiliate Marketing – Part 1 – Why the heck are you not doing this?
  • Kurt on Choosing the Best Ecommerce Shopping Cart
  • Vmelissap123 on Are You Smarter than a 5th Grader?
  • Brennan Scurlock | Review of top 10 web hosting providers. on Site of the Month – Team Brennan
  • iyinet on 10 Best Bookmark and Share WordPress Plugins

@Lunarpages Feed

Who We Are

  • Web Hosting Services
  • About us
  • Partners
  • Affiliate Program
  • Specials
  • 24/7/365 Support Desk
  • Community Forum
  • WIKI

Our Products

  • Colocation Services
  • Fully Managed Services
  • Dedicated Hosting
  • VPS Hosting
  • Microsoft Exchange
  • Open-Xchange
  • Fax To Email
  • Linux Hosting
  • Windows Hosting
  • Reseller Hosting
  • Quicksite Website Builder
  • SEO & SEM Services
  • Payment Processing
  • SecureLive Website Security
  • Tremendesk - Help Desk Solution

Connect With Us

Stay in touch with us!

Sign up for our monthly newsletter to receive updates, news, and current promotions!

Twitter
Facebook Google+ RSS Feed


© 2000 - 2011 Add2Net, Inc. "Lunarpages", "Add2Net" and the "Lunar Symbol" are the trademarks or the Registered trademarks of Add2Net, Inc.
All rights reserved. Unauthorized use is prohibited.