Jump to content


Photo

Can't connect to a database through php


  • Please log in to reply
1 reply to this topic

#1 jparker

jparker
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 24 April 2006 - 02:22 PM

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

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 April 2006 - 02:49 PM

Use the "or die" clause instead of the "if" statement:
<?php
$link = mysql_connect('localhost', 'james', 'password') or die('Could nit connect: ' . mysql_error());
echo 'Connected successfully';
mysql_close($link);
?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users