Configuring MySQL 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
timbo

Configuring MySQL in PHP

Post by timbo »

I have downloaded and installed PHP successfully on my PC running Windows NT using the Windows installer version rather than compiling source. The basic PHP commands seem to be working. However MySQL does not seem to be working from within PHP...

The symptoms are that the mysql_connect( ) and mysql_select_db( ) are completing OK but actual SQL done via mysql_query( ) is returning false result (no error though).

I downloaded MySQL separately and can run mysqladmin and the DOS interface where I can create tables and query them.

I notice in the PHP documentation that from PHP4 has MySQL "built into PHP". There are also comments about having to tell PHP where the MySQL implementation is if you do not want to use the built in version. But how do you do this? I can't work out where to put the directive to tell PHP to use MySQL in the c:/mysql directory or even if I need to do this...

Help! Anyone got any ideas/answers?
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post by sam »

Well because the mysql_connect() and mysql_select_db() are working I would venture to say that you most likely have an error in your SQL statement. If you post it we might be able to give you some help.

Cheers Sam
timbo
Forum Newbie
Posts: 1
Joined: Fri Apr 19, 2002 6:16 am

Example of SQL failing

Post by timbo »

As requested here is an example of the PHP code that is not working. It prints allthe connection stuff but then prints the "No MySQL result"

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Do MySQL 2</title>
</head>

<body>

<?PHP
	$DB_HOST = "localhost";
	$DB_USER = "root";
	$DB_PASSWORD = "fenton";
	$db_link = mysql_connect( $DB_HOST, $DB_USER, $DB_PASSWORD ) or die("cannot connect to MySQL");
	print("Connected to MySQL
");

	$DB_NAME = "mysql";
	mysql_select_db($DB_NAME, $db_link) or die("cannot select 'mysql' database"); 
	print("<br>'mysql' selected");

	$sqlstring = "CREATE TABLE test (test_id smallint(6) DEFAULT '0' NOT NULL auto_increment, test_text tinytext NOT NULL, PRIMARY KEY (test_id))";

	$result = mysql_query( $sqlstring, $db_link );

	if ( $result ) &#123;
		print("<br>MySQL instruction executed:<br>$sqlstring");
	&#125; else &#123;
		print("<br>No MySQL result... <br>$sqlstring
");
	&#125;

?>
</body>
</html>
Could it be an authority issue? How do I look at the MySQL settings for the default PHP MySQL settings?
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post by sam »

SELECT * FROM mysql.user; will show you the list of users, if you run a GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY 'fenton' WITH GRANT OPTION; It will enable you to set the access limitations of your user account.

Cheers Sam...
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

You might also try printing out what mysql_error() returns.
User avatar
hex
Forum Commoner
Posts: 92
Joined: Sat Apr 20, 2002 3:20 am
Location: UK

Post by hex »

Also, why not use the command line interface to add tables and stuff? Or even better than that, get the glorious phpMyAdmin:
http://www.phpwizard.net/projects/phpMyAdmin/index.html
http://phpmyadmin.sourceforge.net/
http://www.phpmyadmin.net/
Post Reply