473,395 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

mysql_connect function isn't working

4
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 4883
jamwil
13
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 Expert Mod 8TB
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 Expert 100+
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 Expert 4TB
@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
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 Expert 4TB
@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
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 Expert 4TB
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 Expert 4TB
@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
Hendor
4
@Atli
I think that was just an extraordinarily unlikely typo. I had heard of both mysql and mysqli, and I knew I was looking for mysql. Or maybe I just copied and pasted it from somewhere without noticing the 'i'.

Thanks for the tips, I'm sure they would have worked, but after changing so many settings over the past few days, I figured that even if I had fixed the problem, one of my other changes would have messed it up and I wouldn't know that I had fixed it. An hour ago, I uninstalled everything in order to start over and then saw Markus' comment. I've now got easyPHP installed and all my earlier problems are fixed. Now I just wish that I had saved my site and database before I deleted the Apache and MySQL folders. *facepalm*

Thanks all for your help, much appreciated.
May 29 '09 #11
Markus
6,050 Expert 4TB
@Hendor
Yeah, I've been there before. You change so much in blind hope that it'll fix something. And then, when you do fix the error, you've caused other errors, and then you decide to walk away from the computer before beating it.

@Hendor
Been there too. I've lost lots of stuff doing that; thankfully, it wasn't important stuff.

Mark.
May 30 '09 #12
Atli
5,058 Expert 4TB
@Hendor
I did the same thing yesterday :)
Lost and entire MSSQL database I spent the last 2 days building.

Anyways, I'm glad you've got your sever working now.

P.S.
You should try using Linux as a server someday.
On my Ubuntu test server this entire problem would have been solved by a single command:
Expand|Select|Wrap|Line Numbers
  1. $sudo apt-get install php5-mysql
Easy, right? :P
May 30 '09 #13

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

Similar topics

1
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 =...
10
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....
5
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...
1
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...
7
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...
6
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...
1
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...
7
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...
8
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.