please guide me how to acess database for mysql thr' php, i tried a lot but getting error like cannot open the page or c:/program files/ apache/htdocs/dbconnect.php Could not connect to database


why these error, i am using mysql 5.2.0, php 5.2.0 and apache 2.2.3, are there any setting needed?? please reply quickly

Are you using the following statement;

mysql_connect('hostname', 'user', 'password');

If you are and you still get an error create a php file and have the following script in the file;

<?php
php_info();
?>

If MYSQL does not appear then your not configured correctly. Post back to let me know how you got on!!!

This is the connection im using.....

//connection settings (Open)
$conn = mysql_connect("localhost", "root", "");
if (!$conn)
{
die('Could not connect: ' . mysql_error());
exit;
}
//select database
mysql_select_db("vssdb", $conn);
//check if database does not exist
if(mysql_errno($conn) == 1049)
{
//creating the database
$sql = 'CREATE DATABASE vssdb';
if (!mysql_query($sql, $conn))
{
echo 'Error creating database: ' . mysql_error();
exit;
}

createtables();
}
//close connection

You don't need those exit; statements. Try removing them and see what happens.

When posting code, it's a good idea to use [ code ] and [ /code ] tags. It helps everyone involved, giving us color highlighting (many problems have been solved with color highlighting) and preserving indention.

After looking at your script you do not have a password to connect to the database. Is this being inserted in the actual script?

This is normally what I would do,
I write a config.php file with the following script

<?
// MySQL Settings
$DbServer = "localhost";
$DbUser = "username";
$DbPass = "password";
$DbName = "database";
// making the database connection
$dblink=mysql_connect($DbServer, $DbUser, $DbPass) OR DIE("Unable to connect to database");
mysql_select_db("$DbName") or die( "Unable to select database");
?>

If this doesn't work, check if Apache and Mysql are running, also check with your phpinfo() if mysql is configured.

This article has been dead for over six months. Start a new discussion instead.