homepage Welcome to WebmasterWorld Guest from 54.235.61.62
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Database not wanting to connect
database not connecting
mades




msg:4547189
 5:51 pm on Feb 20, 2013 (gmt 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



 

coopster




msg:4547205
 6:59 pm on Feb 20, 2013 (gmt 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...]

Andy Langton




msg:4547210
 7:04 pm on Feb 20, 2013 (gmt 0)

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());
mades




msg:4547214
 7:32 pm on Feb 20, 2013 (gmt 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)

brotherhood of LAN




msg:4547240
 9:19 pm on Feb 20, 2013 (gmt 0)

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.

swa66




msg:4547246
 9:31 pm on Feb 20, 2013 (gmt 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...]

Skorpios




msg:4552305
 9:39 pm on Mar 7, 2013 (gmt 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

phranque




msg:4552526
 11:29 am on Mar 8, 2013 (gmt 0)

welcome to WebmasterWorld, Skorpios!

Skorpios




msg:4552662
 5:15 pm on Mar 8, 2013 (gmt 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.

phranque




msg:4552769
 2:57 am on Mar 9, 2013 (gmt 0)

you found one of the better php forums to move up the scale.
=8)

Skorpios




msg:4552770
 3:09 am on Mar 9, 2013 (gmt 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 ]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved