retriving data from mysql

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

juansoul
New php-forum User
New php-forum User
Posts: 29
Joined: Tue Oct 08, 2002 6:33 pm

retriving data from mysql

Postby juansoul » Tue Nov 05, 2002 6:25 am

Im having problem retrieving data from mysql database ,I succesfully created a table and enterd information via an html form but when i try to retrieve info from the database I get this error

Warning: Supplied argument is not a valid MySQL result resource in /home/www/bullzeyecreative/Display.php on line 28the error seem to be arround were the while function begins
can some please tell me whats wrong in here




Code: Select all

<?php
$Host = "localhost";
$User = "00000";
$Password = "00000";
$DBName = "00000";
$TableName = "Feedback";

$Link = mysql_connect ($Host, $User, $Password);

$Query = "SELECT * from $TableName";
$Result = mysql_db_query ($DBName, $Query, $Link);

print ("<table border=1 width=\"75%\" cellspacing=2 cellpadding=2>");
  print ("<tr>");
    print ("<td>Name</td>");
    print ("<td>Email</td>");
    print ("<td>Comments</td>");
  print ("</tr>");
 
while ($Row = mysql_fetch_array($Result)) {
  print ("<tr align=center valign=top>\n");
  print ("<td align=center valign=top>$Row[FirstName]</td>\n");
  print ("<td align=center valign=top>$Row[LastName]</td>\n");
  print ("<td align=center valign=top>$Row[Email]</td>\n");
  print ("<td align=center valign=top>$Row[Comments]</td>\n");
  print ("</tr>\n"); }
 
mysql_close ($Link);
  print ("</table>\n");
?>

DoppyNL

Postby DoppyNL » Tue Nov 05, 2002 8:22 am

The query you executed failed for some reason.
perhaps you made a typo somewhere.

print out the query in your php-script to see what is actually executed by PHP.

Greetz Daan

juansoul
New php-forum User
New php-forum User
Posts: 29
Joined: Tue Oct 08, 2002 6:33 pm

Postby juansoul » Tue Nov 05, 2002 8:31 am

well cant you tell me by looking at the script i posted here where the typo is because i cant find it , and i dont know how to print the query

DoppyNL

Postby DoppyNL » Tue Nov 05, 2002 8:46 am

you can print your query by simply using "print($Query);"

What actually is going wrong in your script is that the query ($Query) gets an error when executed so no values are returned.
after that you try to retrieve the data at the beginning of your loop php generates an error because $Result doesn't contain a correct query-result (the query failt).

You should check if the table "Feedback" exists in your database (case sensitive!) since this is probably the problem.


some other minor mistakes in your script:

Code: Select all

$Row[FirstName]
must be

Code: Select all

$Row['FirstName']

there must be quotes around strings, otherwise php thinks its a constant and generate a warning.

Code: Select all

print ("<td align=center valign=top>$Row['FirstName']</td>\n");
must be:

Code: Select all

print ("<td align=center valign=top>" . $Row['FirstName'] . "</td>\n");
you can't add a value from an array like you tried.

Code: Select all

mysql_close ($Link);
is not wrong, but also not necesary, php will do that for you when the script ends.

Greetz Daan

Jay

Postby Jay » Tue Nov 05, 2002 9:24 am

juansoul wrote:well cant you tell me by looking at the script i posted here where the typo is because i cant find it , and i dont know how to print the query

You've got
$Link = mysql_connect ($Host, $User, $Password);

$Query = "SELECT * from $TableName";
$Result = mysql_db_query ($DBName, $Query, $Link);

Try
$Link = mysql_connect ($Host, $User, $Password);
mysql_select_db($DBName); # This line is missing
$Query = "SELECT * from $TableName";
$Result = mysql_query ($Query);

That should work.

juansoul
New php-forum User
New php-forum User
Posts: 29
Joined: Tue Oct 08, 2002 6:33 pm

Postby juansoul » Tue Nov 05, 2002 11:23 am

Man Im still getting an error message on the line where the "while" function starts why????[/b]

Jay

Postby Jay » Tue Nov 05, 2002 11:33 am

It would help us to help you if you provided a bit more info. Did you try amending the code as I suggested?


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 2 guests