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
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")
?>

Link to comment
Share on other sites

$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() );

Link to comment
Share on other sites

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!";
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.