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