Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connection script not working....

Status
Not open for further replies.

peterv12

Technical User
Dec 31, 2008
108
US
I'm not sure if this should be in the MySQL section, or the PHP, so if it should be elsewhere please let me know.

I have a small PHP/MySQL project. It has a PHP front end that connects to a MySQL database. It works perfectly on OS X, but when I attempt to run the script on Kubuntu Linux, it doesn't connect.

I extracted the connection code to test, and it seems that's where the problem is. This is the script:

Code:
<?php
// CONNECTDB.PHP - Updated: Monday July 8, 2009 - 6:23 PM
//$link  = mysql_connect('localhost', 'root', 'jclark', 'PROJECT1');
[COLOR=blue]echo "before link....";[/color]
//
[COLOR=red]$link = mysql_connect('127.0.1.1', 'peterv', 'password','PROJECT1');[/color]
//
[COLOR=blue]echo "after link....";[/color]
if (!$link) {
   die ('Connect failed: ' . mysql_error());
   exit();
} else {
// select the database
   $db_selected = mysql_select_db('PROJECT1', $link);
   if (!$db_selected) {
      die ('Database Selection failed: PROJECT1 ' . mysql_error());
   }
}
When the script runs, the first echo statement displays, but the second doesn't. It's almost as it stalls on the link statement.

If anyone has any ideas, I'd be grateful for the help!
Peter V.
 
So you probably get an error, but PHP might be configured not to show them (which is a good thing on a production server and a bad thing on your development PC). There are a lot of things that can be wrong, and the error message should hint you what the problem is.

You can switch error reporting on in php.ini or with the ini_set function if the settings in php.ini allow it.

Good luck!


+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
I'd take the 'PROJECT' parameter out on the mysql_connect() (it's used to create a new link. Look at the examples at no one uses it (well they might I didn't scroll all the way down) I'm not too sure what will happen to $link in this case.
If you still get the error put a call to mysql_error straight after the call to mysql_connect (i.e. before you test $link.
Interestng ot works on OSX.
Can you connet to the DB using the utilities (e.g. mysql) from the command line on kubuntu.
Also take Don's advice re error reporting.
 
Guys, I thought I had already posted a reply to your replies, but thank you both for the informaion. The initial problem was resolved by Don's suggestion to check the php.ini file. I've also removed the 'PROJECT' parameter per ingresman's suggestion, but I'm getting the following error now:

Code:
The script...
<?php
$mysql = mysql_connect('127.0.0.1', 'peterv', 'password');
if (mysql_connect_errno()) {
   printf("Connect failed: %s\n", mysql_connect_errno());
   exit();
} else {
   printf("Host information: %s\n", mysql_get_host_information($mysql));
}
?>
The error message:
"Fatal error: Call to undefined function mysql_connect() in..."

I'm assuming there's something missing in my PHP or MySQL setup, but I have no idea what. I'd appreciate any suggestions.
Peter V.
 
What version of PHP are you running? Judging by the error I'd say something below 5.3 as it looks like the MYSQL library is not enabled.

run phpinfo(); in a blank php page and see if the mysql extension is loaded.

If its not you'll have to go into the php.ini and enable it there, making sure the library file is present.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
i have not come across the function mysql_connect_errno.

the traditional way of connecting is advisable

Code:
mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select($db) or die(mysql_error());

when you get to production consider removing the die and replacing with more graceful error handlers.

I agree with vacunita that it appears that the mysql library is not loaded.

perhaps this thread would be better located in the php forum.
 
Thanks to all. What I did was reinstall PHP. It seems that the original installation must have been missing something, because it all works now. I do, however have two new questions:

jpadie, I'm new to PHP, so I'm learning it as I go along by searching web tutorials. die is what I've found to handle errors. Can you point me to some examples of what you call "more graceful error handlers"?

Also, I'm having a strange time connecting to MySQL. If I use "localhost" in my script, I get the following errors:
Code:
Warning: mysql_connect() [function.mysql-connect]: [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Users/peterv/Sites/LIBRARY_PROJECT/INCLUDES/dbconnect.php  on line 5

Warning: mysql_connect() [function.mysql-connect]: No such file or directory in /Users/peterv/Sites/LIBRARY_PROJECT/INCLUDES/dbconnect.php on line 5
Connect failed: No such file or directory

However, if I use "127.0.0.1", it connects properly. I've always thought that the two were synonymous. If anyone can explain this to me (or point me to some explanation), I'd be grateful.
Peter V.
 
Hey Peter,
Usually the way a computer maps the string "localhost" to an IP address (generally 127.0.0.1) is by looking in a local hosts file... on most computers this would be in /etc/hosts... on a windows box it's normally $WINDOWS_ROOT/system32/drivers/etc/hosts (no idea why). Most computers are configured to look at a hosts file first and then either use DNS or other naming services. You can often look at /etc/resolv.conf (not windows) to see what the configured order for resolving names into IP addresses.
 
ramam1, thanks for the info.

PETERV
17" MacBook Pro
Snow Leopard 10.6.7
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top