Help in connecting to MySQL using PHP

chococatchococat BeginnerLink Clerk
I made my attempt in PHP programming and found it very frustrating to know that my first PHP code already encountered an error.

Here's my code:
<?php

// set your infomation.
$dbhost='localhost';
$dbusername='username';
$dbuserpass='password';
$dbname='database';

// connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);

//select the database
mysql_select_db($dbname) or die('Cannot select database');

//create the table, customize it if you want
$query = 'CREATE TABLE users(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
username VARCHAR(30) NOT NULL,
password VARCHAR(20) NOT NULL,
email VARCHAR(40) NOT NULL)';
$result = mysql_query($query);

echo "Table Created!";
?>

After running the debug feature of the php software I am using, it showed this error:

PHP Fatal error: Call to undefined function mysql_connect() in C:\Program Files\Apache Group\Apache2\htdocs\samples\_debug_tmp.php on line 9

I am using Apache 2.x, MySQL 4.1.3 and PHP 5.0.2. What is wrong with my code?

Comments

  • ChroderChroder Senior Member The Royal RAM
    You don't have the mysql module installed/enabled. PHP5 by default does not come bundled with the mysql library. See the FAQ entry for more info.

    Note that you must use the MySQL Improved module to connect to MySQL 4.1+. To use the older MySQL module, you have to reset your user passwords using the OLD_PASSWORD function (more info on this). But you will loose all of the interesting new features 4.1 brings if you don't use MySQLI.
  • ne0ne0 Beginner Link Clerk
    Or just use Vetrigo server,which include mysql,php,apache and all you need to run your scripts.That is one of the best programs that I ever seen.
    www.bgliga.com-There is an english version,too!Manage your football team in one of the greatest online games!
Sign In or Register to comment.