Jump to content

Can't connect to a database through php


jparker

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
Link to comment
https://forums.phpfreaks.com/topic/8257-cant-connect-to-a-database-through-php/
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

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.