943,923 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Marked Solved
  • Views: 979
  • MySQL RSS
Jul 15th, 2009
0

Connecting to MySQL with PHP

Expand Post »
I have MySQL running such that I can open a client command line and log on and make databases, tables, etc.

I wanted to do some AJAX. Doing AJAX with ASP, SQL Server, etc is not advisable since both places where I am hosting my websites do not use the Microsoft products. So I am forced to do my development with PHP and MySQL.

I just about have everything set up. I have set up IIS so that I can go to my localhost and I can test out web pages. I have PHP installed so that I can pull up the PHP setting pages.

The problem occurs when I try to bring up the MySQL database in PHP. I use this very simple PHP command:

<?php

$con = mysql_connect('localhost', 'testuser', 'testpassword');

?>


When I try to connect to the mysql database through PHP, I get this error:
Click Here

I figure the problem must be in the settings that are in the php.ini. But it seems that my php.ini is set up for MySQL although it is not mentioned in the output when I query the phpinfo page with this code

<?php phpinfo(); ?>

Here is the result from this:

Click Here
Similar Threads
Reputation Points: 17
Solved Threads: 0
Junior Poster
complete is offline Offline
147 posts
since Dec 2005
Jul 15th, 2009
0

Re: Connecting to MySQL with PHP

Why don't you install xampp. Installing this software does not require any updation in any of the config files.
But in case of XAMPP you'll be using apache server and not IIS.
I recommend you using XAMPP.
Reputation Points: 8
Solved Threads: 13
Junior Poster
anuj576 is offline Offline
180 posts
since Jul 2008
Jul 15th, 2009
0

Re: Connecting to MySQL with PHP

mysql is not enabled. check your php.ini file.

http://nl.php.net/manual/en/mysql.installation.php
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 731
Bite my shiny metal ass!
pritaeas is offline Offline
4,177 posts
since Jul 2006
Jul 16th, 2009
0

Re: Connecting to MySQL with PHP

So, MySQL is no longer enabled by default, so the php_mysql.dll DLL must be enabled inside of php.ini. How is this done? I searched php.ini and I did not find php_mysql.dll.

In my php.ini file I found this section under [MySQL]

[MySQL]
; Allow or prevent persistent links.
mysql.allow_persistent = On

; Maximum number of persistent links. -1 means no limit.
mysql.max_persistent = -1

; Maximum number of links (persistent + non-persistent). -1 means no limit.
mysql.max_links = -1

; Default port number for mysql_connect(). If unset, mysql_connect() will use
; the $MYSQL_TCP_PORT or the mysql-tcp entry in /etc/services or the
; compile-time value defined MYSQL_PORT (in that order). Win32 will only look
; at MYSQL_PORT.
mysql.default_port =

; Default socket name for local MySQL connects. If empty, uses the built-in
; MySQL defaults.
mysql.default_socket =

; Default host for mysql_connect() (doesn't apply in safe mode).
mysql.default_host =

; Default user for mysql_connect() (doesn't apply in safe mode).
mysql.default_user =

; Default password for mysql_connect() (doesn't apply in safe mode).
; Note that this is generally a *bad* idea to store passwords in this file.
; *Any* user with PHP access can run 'echo get_cfg_var("mysql.default_password")
; and reveal this password! And of course, any users with read access to this
; file will be able to reveal the password as well.
mysql.default_password =

; Maximum time (in seconds) for connect timeout. -1 means no limit
mysql.connect_timeout = 60

; Trace mode. When trace_mode is active (=On), warnings for table/index scans and
; SQL-Errors will be displayed.
mysql.trace_mode = Off
Reputation Points: 17
Solved Threads: 0
Junior Poster
complete is offline Offline
147 posts
since Dec 2005
Jul 16th, 2009
0

Re: Connecting to MySQL with PHP

add

extension=php_mysql.dll

to the php.ini

http://www.bicubica.com/apache-php-mysql/index.php
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 731
Bite my shiny metal ass!
pritaeas is offline Offline
4,177 posts
since Jul 2006
Jul 16th, 2009
0

Re: Connecting to MySQL with PHP

I went back and redid the php installation from scratch and did the extra steps you mentioned. Now it works.
Reputation Points: 17
Solved Threads: 0
Junior Poster
complete is offline Offline
147 posts
since Dec 2005
Jul 17th, 2009
0

Re: Connecting to MySQL with PHP

Well, I spoke to soon. Now I get a different kind of error.
pritaeas, I have added these two lines to PHP.ini :
extension=php_mysql.dll
extension_dir="/ext"

I have done all the steps in http://nl.php.net/manual/en/mysql.installation.php
to enable mysql with php. I have included c:\php in the system path. I have added the removed the semi colon on extension=php_mysql.dll in php.ini I have even copied the libraries into the c:\windows\system32 directories just to be sure.
But when I run a php script with this <?php phpinfo(); ?> from a php file, the result does not mention mysql.

The new problem I have is when I run a php file with
$con = mysql_connect('localhost', 'testuser', 'testpassword') or die("Could not connect");

The error I get in my browser says:

Fatal error: Call to undefined function mysql_connect() in C:\inetpub\wwwroot\getuser.php
Last edited by complete; Jul 17th, 2009 at 7:13 pm.
Reputation Points: 17
Solved Threads: 0
Junior Poster
complete is offline Offline
147 posts
since Dec 2005
Jul 18th, 2009
0

Re: Connecting to MySQL with PHP

extension_dir="/ext"

use the full path to the folder, e.g.

extension_dir="c:/php/ext"
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 731
Bite my shiny metal ass!
pritaeas is offline Offline
4,177 posts
since Jul 2006
Aug 17th, 2009
0

Re: Error Connecting to MySQL with PHP

Hi Experts,

I am getting this error message, "Error connecting to mysql"
whenever i am running the below scripts:

php Syntax (Toggle Plain Text)
  1. <?php
  2. $dbhost = 'localhost';
  3. $dbuser = 'root';
  4. $dbpass = 'password';
  5.  
  6. $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
  7.  
  8. $dbname = 'petstore';
  9. mysql_select_db($dbname);
  10. ?>

I've just set-up everything and still getting such error...

By the way, I am using Windows Vista ans IIS 7.0, Could you please help me? suggestions please, recommendations...
Last edited by peter_budo; Aug 17th, 2009 at 4:45 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Robert Plant is offline Offline
2 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: mysql connection limit reached?
Next Thread in MySQL Forum Timeline: mysql Installation Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC