I am having a strange problem that hopefully one of you guru's can help me with. I am new to this, just installed PHP, Apache, and MySQL. I followed the instructions for installing PHP under Windows and am in the process of Testing it all. I have my host up and running. I created the database. I created 2 php files, one for inserting and one for querying.
The insert file works fine. The query doesn't work at all, and I don't know why. Here are the two files:
INSERT FILE
<?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';
?>
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';
echo $select_sql;
// 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>’;
echo 'PHP is working.<br>';
}
?>
I tried both of your suggestions, neither worked. If it helps, these files were cut and pasted from the "Installing PHP under Windows" manual from this site.
<?php
// Connect to the database
$dbhost = 'localhost';
$dbusername = 'testuser';
$dbpasswd = 'testpassword';
$database_name = 'simple';
$connection = mysql_connect($dbhost,$dbusername,$dbpasswd)
or die ("Could not connect to server.");
$db = mysql_select_db($database_name, $connection)
or die("Could not 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 Did not Work: ".mysql_error() );
// Tell User we are done.
echo "Data Inserted";
?>
QUERY FILE
<?php
// Connect to the database
$dbhost = 'localhost';
$dbusername = 'testuser';
$dbpasswd = 'testpassword';
$database_name = 'simple';
$connection = mysql_connect($dbhost,$dbusername,$dbpasswd)
or die ("Could not connect to server.");
$db = mysql_select_db($database_name, $connection)
or die("Could not select database.");
// Generate code to retrieve data from database.
$select_sql = "SELECT text FROM simple_table ";
echo $select_sql;
// Retrieve code from database.
$result = mysql_query( $select_sql ) or die ( "It Did not Work: " . mysql_error() );
// Display results to user.
while ( $row = mysql_fetch_object ( $result ) )
{
echo $row->text . "<br>" ;
echo "PHP is working.<br>";
}
?>