473,569 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mysql_connect function isn't working

4 New Member
Hi all. I've recently set up Apache 2.2 with PHP 5.2 and MySQL 5.1. I played around with SQL a bit, and now I'm trying to access it with PHP. I currently have the code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. # error_reporting(E_ALL);
  4. # ini_set('display_errors', true);
  5.  
  6. echo "test";
  7.  
  8. $link = mysql_connect('localhost', 'root', '[mypassword]');
  9. if (!$link) {
  10.     die('Could not connect: ' . mysql_error());
  11. }
  12. echo 'Connected successfully';
  13.  
  14. ?>
  15.  
When I run the above in Firefox with my correct password, all I get is "test", verifying to myself that PHP was working when the script ran. I've also verified that the username and password correctly start MySQL though the CLI, but I don't think that's the issue here. If they were incorrect, I should get the "Could not connect" message and the MySQL error, but I get no errors at all. This makes me think the mysql_connect function itself isn't working right.

What possible mistakes could I be making? Thanks in advance.
May 28 '09 #1
12 4907
jamwil
13 New Member
I'm certainly no expert, but it looks to me like your code is sound.
Seems to me like php is not communicating with mySql... Which I'm not the person to ask how to fix.
May 28 '09 #2
Dormilich
8,658 Recognized Expert Moderator Expert
still the question remains, why there is no further output. you could try using a database abstraction layer (e.g. PDO). they are usually more responsive/informative when it comes to errors.
May 28 '09 #3
Ciary
247 Recognized Expert New Member
try this and see what it outputs (if it outputs something). whats wrong is, if an sql error occurs, your phpcode wil stop. therefor you need to put the error-output on the same line as where you get the error.

Expand|Select|Wrap|Line Numbers
  1. $link = mysql_connect('localhost', 'root', '[mypassword]') or die(mysql_error($link));
warnings are different, they wont stop the executing code
May 28 '09 #4
Atli
5,058 Recognized Expert Expert
@Ciary
I'm not sure you are right there.
Using "or die()" or a if statement, like he did, is pretty much the same thing.

SQL errors don't affect the execution of the PHP code, other than the mysql_connect function returning FALSE rather than returning a resource.

Could it be that the mysql extension isn't installed, and the mysql functions simply aren't there?
Try using the function_exists function to see if the mysql_connect function really exists.
May 28 '09 #5
Hendor
4 New Member
Thanks for your responses; I think Atli got it. I ran this script:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (function_exists('mysql_connect')) {
  3.     echo "Good<br />\n";
  4. } else {
  5.     echo "Bad.<br />\n";
  6. }
  7. ?>
It returned "Bad", so the MySQL extension for PHP is not installed. Is this the kind of thing that PHP would exclude when I choose the Recommended settings during its installation? Seems like a pretty major component to be considered unimportant... I'll reinstall PHP fully and try again.
May 28 '09 #6
Atli
5,058 Recognized Expert Expert
@Hendor
The MySQL extension was compiled into PHP4, but they detached it in PHP5 and made it an optional extension.
Not sure exactly why, but I bet they had their reasons :)

If you are using the Windows installer, you don't have to completely reinstall it.
Just go to the Add/Remove Programs, find PHP in the list and click modify. Then you should be able to go through the installation wizard again and check the extensions you want.
May 29 '09 #7
Hendor
4 New Member
Ok, thanks. I've done that, restarted Apache and computer, but it's still not working. PHP seems to work just as before, but still returns "Bad" when I run that script.



I've read a few things and looked through my php.ini. I found this:

extension_dir=" C:\Program Files\PHP\ext"

The phpinfo() function also returns that directory as the extension location. When I look in that PHP directory, there is no "ext" sub-directory, only ones called dev and PEAR. So PHP is looking for the mysql (and other) extensions in a non-existent folder...

Two questions: Do you know where the PHP installer would have put the extensions during installation, if not where it says in the ini file. And, is it just php_mysqli.dll and libmysql.dll that is needed, or is there more? Thanks.
May 29 '09 #8
Markus
6,050 Recognized Expert Expert
Not a solution to your current problem, but try out one of the nifty installers that have most web-development components bundled with them.

XAMPP

EasyPHP

Both very easy to set up, and they do most of the work for you.
May 29 '09 #9
Atli
5,058 Recognized Expert Expert
@Hendor
The installer should have put php_mysql.dll into a ext directory under the PHP installation path.
The libmysql.dll should be directly under the PHP installation path.

There are only three things that you need to do to get the MySQL extension working.
  1. Put php_mysql.dll into the directory specified in the extension_dir directive.
    If the installer didn't provide the DLL you can download the ZIP package at the php.net download page and use the DLL from there.
  2. Add/uncomment "extension=php_ mysql.dll" in your php.ini file.
  3. Add the PHP installation dir to your path variable.
    (See the instructions here)
    Alternatively, you can just copy the libmysql.dll into the Windows directory, although I would at least try setting the path variable first.
Then restart the server and it should be working.

Edit:
I just realized you wrote php_mysqli.dll, rather than php_mysql.dll.

These are two different extensions: The old MySQL Extension and the new Improved MySQL Extension.

They are two very different things.
May 29 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1841
by: LRW | last post by:
I'm working on some scripts someone else made, and it's littered with repetitive mysql_connect's and mysql_select_db's. For example: // MAKE SURE IT IS A VALID PRODUCT $connection2 = mysql_connect("localhost", "root") or die ("Couldn't connect to the server."); $db2 = mysql_select_db("priceretail", $connection2) or die ("Couldn't select...
10
5098
by: Alvaro G Vicario | last post by:
I need that my script retrieves data from two different databases so I use mysql_connect() to open two connections. I have only one server, user and password, just two different databases. Simplified, it'd be: $con1=mysql_connect($server, $user, $pass); mysql_select_db($database1, $con1); $res1=mysql_query($sql1, $con1); ...
5
2771
by: awdsites | last post by:
Hi, I'm a noob with a server so keep that in mind :) Been trying to connect with php to mysql and all I get is: "Fatal error: Call to undefined function: mysql_connect()" I've read and read and can't figure out what is wrong. My current set up is:
1
5630
by: ezmiller | last post by:
Hi, I am very new to php, and I am trying to use it to access a mysql database. But I'm trying to use mysql_connect but the browser returns the following error sayin that the mysql_connect function is undefined. Why? Can anyone help? Fatal error: Call to undefined function mysql_connect() in...
7
5406
by: avenpace | last post by:
Hi, I got error when using mysql_connect function in my php script. If i set the db host to localhost, it give me that error altough all the user and password that I wrote is true(I can login using that username/passwd using mysql console), but when I change the db host into 127.0.0.1 mysql_connect will work. Beside of that I can't make my...
6
3354
by: GD | last post by:
Hi All, I've got MySQL 5.0.21 running on Windows Server 2003, and php running on Apache on a Linux box (Fedora Core 4). Previously when the pages were running on an IIS server the connection was succesful, now I get: Can't connect to MySQL server on 'SERVER' (13)
1
4353
by: hugo | last post by:
Hello people, There is a problem is that mysql_connect() somehow caches last sessions IP and is not using the one which you put into host place. Has anyone made mysql_connect() from php to multiple SQL servers so that script tries to connect to resolved IPs until it finds one that works? I have set up DNS so that on request...
7
2841
by: criveraf | last post by:
Hi there, I wasn't sure where to put this question, since it deals with both PHP and MySQL. I apologize if this is not the correct forum for this. I am working with a simple PHP application using MySQL at the data layer. This is on a Windows XP machine, using IIS 6.0, MySQL 5.0, PHP 5. The problem I am having is that a call to...
8
8245
by: Kjell Pettersen | last post by:
Hello! I have installed PHP and MySql 5.0 on Win XP. Database created ok. Installation ok. PHP scripts runs ok. But when I am trying some mysql calls in the script I get an "Error 500" from explorer. // Connect to the database server echo ('Trying to connect to db');
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.