How to access MySQL database in PHP?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
hanxiao
Forum Newbie
Posts: 12
Joined: Sun Apr 16, 2006 10:58 pm
Location: Auckland, New Zealand
Contact:

How to access MySQL database in PHP?

Post by hanxiao »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I was trying to make a trial in Dreamweaver MX 2004 to access the database in MySQL 5.0 under Windows XP. 


I used the code from http://www.php.net like:

Code: Select all

<?php
	$link = mysql_connect('localhost', 'root', '12345678');
	if (!$link) 
                {
	   die('Could not connect: ' . mysql_error());
	}
	echo 'Connected successfully';
	mysql_close($link);
?>

But gives me a error says:"Fatal error: Call to undefined function mysql_connect() in D:\Apache Group\Apache2\htdocs\Learn\Welcome.php on line 10", which is saying the line with "$link = mysql_connect('localhost', 'root', '12345678');" something is wrong.

Because in my previous post, I got problem with setting up MySQL connection in Dreamweaver MX 2004, so I just want to have a try to see if PHP can connect to MySQL directly. It seems it doesn't work. Anyone has any idea about this?

Thank you.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

As long as you don't configure your php.ini in order to load the extension the function will not exist.
It's all mentionned in the INSTALL.TXT file and in the fine manual...

Have you searched the web for 'mysql_connect function does not exist'?
hanxiao
Forum Newbie
Posts: 12
Joined: Sun Apr 16, 2006 10:58 pm
Location: Auckland, New Zealand
Contact:

Post by hanxiao »

Thanks for your reply.

So now I know I need to modify the php.ini file again for extension.

I just uncommented the extension part for mysql. "extension=php_mysql.dll".

But it still gives me the same error message at the same line. Any idea?

Thank you.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Did you restart Apache?
hanxiao
Forum Newbie
Posts: 12
Joined: Sun Apr 16, 2006 10:58 pm
Location: Auckland, New Zealand
Contact:

Post by hanxiao »

Sorry about that code thing for my post. I am new here and new for php, mysql, apache etc.


I think I found the problems. But I am not sure which one is the vital one.

I did some changes in the setting files:
In httpd.conf, I added this line, which was in the php.ini file, which I followed the instruction on http://www.php.net, maybe it's my misunderstanding from the instruction, but I think it is really misleading me to do the wrong thing:
PHPIniDir "D:/PHP/php-5.1.2-Win32"

In php.ini, I added one more line for the extension, which was not in the recommended php.ini file.
extension=php_mysqli.dll

I also replaced the libmysql.dll file which was contained within the zip file of 5.0.18 version PHP from the http://www.php.net with the new file from http://dev.mysql.com/downloads/connector/php/
They have different size, so I am confused is it a mistake from php or it is just because mysql updated the dll file?

But everything is working, thank you.

If anyone has similiar problem with setting up extensions etc. can go to read this thread: http://forums.mysql.com/read.php?52,698 ... #msg-69846
It's really useful.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Do not cross post. Choose a thread and stay there.
Post Reply