Jump to content

Archived

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

jparker

Can't connect to a database through php

Recommended Posts

Hello

I'm trying to get php to work with mysql. If I log onto mysql with my credentials I can look at the database and so some commands.

If I attempt to do the same in php via a script : -

<?php

$link = mysql_connect('localhost', 'james', 'password');

if (!$link) {

print('Could not connect: ' . mysql_error());
exit;

}

echo 'Connected successfully';

mysql_close($link);

?>

I don't get to see the success message but neither do I get the error message. Could someone please spread some light on this as I have a long way to go on my php/mysql journey and this is depressingly early roadblock.

James

Share this post


Link to post
Share on other sites
Use the "or die" clause instead of the "if" statement:
[code]<?php
$link = mysql_connect('localhost', 'james', 'password') or die('Could nit connect: ' . mysql_error());
echo 'Connected successfully';
mysql_close($link);
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

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.