Jump to content

mySQL Connection problem


GreyPilgrim

Recommended Posts

Newbie etc.  Can you tell me what the problem is with this? I've installed mySQL 5 on my home pc and the command line interface seems to be working fine - I'm well away with creating tables and populating them etc

 

I'm trying to run some php code to connect to mySQL and I'm getting a blank screen:

 

===========================================

<?php

// Set the database access information as constants.

DEFINE ('DB_USER', 'root');

DEFINE ('DB_PASSWORD', 'password');

DEFINE ('DB_HOST', 'localhost');

DEFINE ('DB_NAME', 'sitename');

echo'Checkpoint1';

$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );

echo'Checkpoint2';

@mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );

?>

===========================================

 

I copied this from a textbook I'm reading but I put in the "checkpoint1" and "checkpoint2" echoes as simple debug pointers. 

 

All I'm getting is "checkpoint1" - nothing else at all!

 

I wasn't prompted for a username when installed mysql so I'm assuming its 'root'. I specified the password as 'password', and created the database as 'sitename'

 

Hope you can help>?

Link to comment
https://forums.phpfreaks.com/topic/39710-mysql-connection-problem/
Share on other sites

I have this code in a seperate file and I call it by using

 

include_once "connection.php"

 

There is a good site for installing php, apache and MySQL....http://www.puremango.co.uk/cm_wamp_97.php

<?php
$host="localhost";
$user="username";
$password="password";
$database="database_name";

mysql_connect($host,$user,$password);
@mysql_select_db($database) or die( "Unable to select database")
?>

$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );

Try not surpressing the error so the or die can actually be reached.

$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );

No.  It's done by the @ operator.  At the beginning of a function (and maybe some other things too... darn memory...)  it means 'If this returns any errors, pretend they don't happen.'

 

I made the same mistake for almost three months.  The way I learned to select databases was by @mysql_select_db();.  I saw that and just always assumed the @ had to be there until one day someone told me :P.

 

Anyway, if you remove the @, it won't suppress errors and you should be able to figure out what's wrong.

 

My best guess is that you don't have mysql installed in PHP.

 

Try making a blank file and seeing what this returns:

 

<?php
if(function_exists('mysql_connect')) {
echo "It exists!";
}
else {
echo "You need to install MySQL!";
}
?>

No.  It's done by the @ operator.  At the beginning of a function (and maybe some other things too... darn memory...)  it means 'If this returns any errors, pretend they don't happen.'

 

ARGH.  So easy when you have some help.  I didn't realise the @ did that. Anywoo, removing it led me to realise I had screwed up some stuff in the php.ini file

 

Thanks guys - lifesavers.

 

GP

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.