Welcome to WebmasterWorld Guest from 50.17.117.221

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Database not wanting to connect

database not connecting

     
5:51 pm on Feb 20, 2013 (gmt 0)

New User

joined:Feb 20, 2013
posts: 2
votes: 0


I've done the same thing before (not with godaddy though - and it was our teacher at the time who helped us). Anyhow, I do not understand how this code is wrong.

<?php 
//Connection settings for MySQL
//Database name
$db_name = 'dbname';
$host = 'localhost'; //IP or localhost

//user info
$db_user = 'dbname';
$db_password = 'mypassword';

$connection = mysql_connect($host,$db_user,$db_password);
mysql_select_db($db_name,$connection);

?>


Also, for the localhost, I tried 127.0.0.1 but to no avail.

Unless I am completely wrong, shouldn't THIS work? On godaddy, it says that the name and the user is the same (they grouped them as one, I dunno why).

After typing in the url and pressing enter, it gives me THIS error:

Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/21/10535121/html/connection.php on line 11

Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/content/21/10535121/html/connection.php on line 12


6:59 pm on Feb 20, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12533
votes: 0


Welcome to WebmasterWorld, mades.

the function you are using returns false on failure and that is why you are getting the second boolean message there. You are going to need to look a little closer at the connection attempt in your code, particularly the host/user/password being used. Also, you likely learned about mysql functions in school but you may consider using the new MySQL functions (mysqli) since in PHP 5.5 mysql functions will be deprecated.

[php.net...]
7:04 pm on Feb 20, 2013 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member andy_langton is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 27, 2003
posts:2986
votes: 37


You could also try getting a more useful error message via mysql_error(), i.e.

$connection = mysql_connect($host,$db_user,$db_password) or die(mysql_error());
7:32 pm on Feb 20, 2013 (gmt 0)

New User

joined:Feb 20, 2013
posts: 2
votes: 0


@ Coopster: Thank you for the welcome :)

I have entered here the wrong users and pass on purpose. For obvious reasons.

You are correct, I have learned this in school.


you may consider using the new MySQL functions (mysqli)


I have no clue what you're referring to or even how to implement it. To say the least, back end is not my strong point.




@Andy, the second message isn't very useful for me as I don't understand what it means at all :( (I'm much more of a front end designer, not a back end one)
It slightly changes the message to this:


Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/content/21/10535121/html/connection.php on line 12
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
9:19 pm on Feb 20, 2013 (gmt 0)

Moderator from GB 

WebmasterWorld Administrator brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4842
votes: 1


I believe GoDaddy's MySQL offerings for shared hosting are on a different server, so localhost would be the wrong server to connect to. If you login to your GoDaddy account area there's a page that will show you the correct MySQL server.
9:31 pm on Feb 20, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 7, 2003
posts:4783
votes: 0


127.0.0.1 vs localhost
localhost and 127.0.0.1 are NOT the same when it comes to PHP talking to mysql.
When given "localhost" it will use a unix domain socket
When given "127.0.0.1" it will use a TCP connection.

You can compile mysql to not use the unix domain socket and/or when doing a chroot thing it might be a lot harder to get the unix domain socket to work.

Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

is interesting. In essence it's PHP telling you that where you pointed it (localhost), from the configuration it has, there is no mysql listening.
Do you have control over the mysql database being running or not ?
-> I'd take it up with those running the server - they should be able to tell you where it is listening.

mysql vs mysqli
You have a number of methods for PHP to connect to a mysql database. The most common you see in tutorials is obsolete (mysql). It is replaced with mysqli (note the i). The main difference is that is is not obsolete and that it does support things like prepared statements (which are IMHO essential to good and secure programming).
Anything new or anybody learning really should not use the obsolete mysql way of doing things, and focus on e.g. mysqli instead.

mysql: [php.net...] [OBSOLETE]
mysqli: [php.net...] [CURRENT]
The third option is "PDO": [php.net...]
9:39 pm on Mar 7, 2013 (gmt 0)

New User

joined:Mar 7, 2013
posts: 30
votes: 0


Try this as your full connect script, just change the fields as appropriate


<?php

$config['db'] = array(
'host' => 'localhost',
'username' => 'root',
'password' => 'Your_Password',
'dbname' => 'Your_DB_Name'
);

$db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'], $config['db']['username'], $config['db']['password']);

?>


save as connect.php or whatever then include on pages that need it. Hope this helps
11:29 am on Mar 8, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10542
votes: 8


welcome to WebmasterWorld, Skorpios!
5:15 pm on Mar 8, 2013 (gmt 0)

New User

joined:Mar 7, 2013
posts: 30
votes: 0


Thanks. Let me just add I did not write that connection script, I am at the beginner end of the scale as far as PHP, and I do mean beginner.
2:57 am on Mar 9, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10542
votes: 8


you found one of the better php forums to move up the scale.
=8)
3:09 am on Mar 9, 2013 (gmt 0)

New User

joined:Mar 7, 2013
posts: 30
votes: 0


Well I was in another forum, was helped by 2 people but a third was decidedly sarcy and just played the superiority card so thanked the ones that helped and left. I do have a question posted if anyone is in the mood? [webmasterworld.com ]