Trying to get php and mySQL talking!!
Posted by:
ZAKI SEMAR
Date: December 08, 2005 04:45PM
Hi ! I am new to mySQL and need some help. I followed some online instructions to try and get php and mySQL talking. I entered the code shown below in mySQL and then created the 2 files "test_insert_sql.php" and "test_select_sql.php" and put them in htdocs in Apache. But, when I traversed to the file via localhost/"test_insert_sql.php", I got the following error:
Fatal error: Call to undefined function mysql_connect() in C:\Program Files\Apache Group\Apache2\htdocs\test_insert_sql.php on line 7
and when I traversed to localhost/"test_select_sql.php" I got the following:
Parse error: parse error, unexpected '>' in C:\Program Files\Apache Group\Apache2\htdocs\test_select_sql.php on line 22
Line 22 is the line "echo $row->text . ‘<br>’;" as you can see if you scroll down.
I was just wondering if some1 can help me. I am new to php, SQL and Apache. I installed all of this in the last 24 hrs. So, please use English. Not Geek! :)
BTW, I use Windows XP!
Thanx so much!
########################################################
CREATE DATABASE simple;
USE simple;
CREATE TABLE simple_table (
id INT AUTO_INCREMENT,
text MEDIUMTEXT,
PRIMARY KEY (id)
);
Response: OK …
Enter: GRANT ALL
Enter: ON simple.*
Enter: TO testuser@localhost
Enter: IDENTIFIED BY 'testpassword';
Response: OK …
########################################################
The following is "test_insert_sql.php":
<?php
// Connect to the database
$dbhost = 'localhost';
$dbusername = 'testuser';
$dbpasswd = 'testpassword';
$database_name = 'simple';
$connection = mysql_connect("$dbhost","$dbusername","$dbpasswd")
or die ('Couldn\'t connect to server.');
$db = mysql_select_db("$database_name", $connection)
or die('Couldn\'t select database.');
// Generate SQL code to store data on database.
$insert_sql = 'INSERT INTO simple_table (text) VALUES (\'test text, 1,2,3\')';
// Execute SQL code.
mysql_query( $insert_sql )
or die ( 'It Didn\’t Work: ' . mysql_error() );
// Tell User we are done.
echo 'Code Inserted';
?>
########################################################
The following is "test_select_sql.php":
<?php
// Connect to the database
$dbhost = 'localhost';
$dbusername = 'testuser';
$dbpasswd = 'testpassword';
$database_name = 'simple';
$connection = mysql_connect("$dbhost","$dbusername","$dbpasswd")
or die ('Couldn\'t connect to server.');
$db = mysql_select_db("$database_name", $connection)
or die('Couldn\'t select database.');
// Generate code to retrieve data from database.
$select_sql = 'SELECT text FROM simple_table';
// Retrieve code from database.
$result = mysql_query( $select_sql )
or die ( 'It Didn\’t Work: ' . mysql_error() );
// Display results to user.
while ( $row = mysql_fetch_object ( $result ) )
{
echo $row->text . ‘<br>’;
}
?>
#########################################################