Search Help Board

PHP FAQ
PHP Articles
PHP Help
Bulletin Board

PHP Manual (NEW!)
First Time PHP'ers
Help with programming
Sql assignment help
PHP Homework Help


 
 php/mysql problem
Author:  (---.cust.uslec.net)
Date:   02-24-04 12:09

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';
?>


QUERY 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 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>';
}
?>



All help is GREATLY appreciated!

 Re: php/mysql problem
Author:  (---.scpe.powergate.ca)
Date:   02-24-04 12:35

I believe your problem is that TEXT is a keyword and should not be a column name..try changing it to vText or something else and try again

 Re: php/mysql problem
Author:  (---.cust.uslec.net)
Date:   02-25-04 04:17

Then why did the INSERT file work? I understand your reasoning, but why would reading be harder to perform than inserting data?

Thanks!

 Re: php/mysql problem
Author:  (---.scpe.powergate.ca)
Date:   02-25-04 05:25

try
// Display results to user.
while ( $row = mysql_fetch_array ( $result ) )

$myText= $row['text'];

echo $myText;
}

 Re: php/mysql problem
Author:  (---.cust.uslec.net)
Date:   02-25-04 05:43

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.

 Re: php/mysql problem
Author:  (---.oakland-elevators.co.uk)
Date:   02-25-04 08:05

Hi,

You could try replacing

while ( $row = mysql_fetch_object ( $result ) )
{
echo $row->text . <br>;
echo 'PHP is working.<br>';


while ($row = mysql_fetch_array($result))
{
echo $row['text'];

Matt

 Re: php/mysql problemSTILL NOT WORKING
Author:  (---.cust.uslec.net)
Date:   02-25-04 09:30

That didn't work either. I'm losing my hair now.

 Re: php/mysql problem
Author:  (203.106.219.---)
Date:   02-25-04 16:42

Hm..I change a bit--it works fine.

INSERT 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 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>";
}
?>

Go to Top  |  Go to Topic  |  Threaded View  |  Search 
  |  
New Topic
 Reply To This Message
 Your Name:
 Your Email:
 Subject:
Email replies to this thread, to the address above.
  

Provided By
Phorum