943,917 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1025
  • PHP RSS
Aug 6th, 2009
0

Query failed: You have an error in your SQL syntax; check the manual that corresponds

Expand Post »
hi i need some help someone please. i tried so many combinations of code with but not getting anywhere. I need a user to enter an id through a form, then the code should retrieve certain fields of the table and display them in a table. here is the code.

php Syntax (Toggle Plain Text)
  1. <form method="POST" action="actioned.php">
  2. <table>
  3. <col span="1" align="right">
  4. <tr>
  5. <td><font color="blue">Membership No:</font></td>
  6. <td><input type="text" name="id" id="id" size=50></td>
  7. </tr>
  8. <tr>
  9. <td><input type="submit" value="Submit"></td>
  10. </tr>
  11. </table>
  12. </form>
  13.  
  14.  
  15. <?php
  16. // Connects to your Database
  17. $dbhost = "localhost"; // variable holding host information
  18. $dbname = "sportscentre"; // variable holding the name of the database
  19. $dbuser = "*****"; // variable holding the username of the person using database account
  20. $dbpass = "*****"; // variable holding password of the person using database account
  21.  
  22. $connection = mysql_connect($dbhost, $dbuser, $dbpass)or die("Cannot connect");
  23.  
  24. $connection = mysql_select_db($dbname)or die("cannot select DB");
  25.  
  26. $id = $_POST['id'];
  27.  
  28. $data = mysql_query("SELECT title, first_name, surname, addr_line_1, addr_line_2, city, postcode, tel_number, mob_number, email FROM members where id = '$_post[id]'")or die(mysql_error());
  29. $result = mysql_query($data)or die ("Query failed: " . mysql_error());
  30.  
  31. Print "<table border cellpadding=3>";
  32. while($info = mysql_fetch_array( $data ))
  33. {
  34. Print "<tr>";
  35. Print "<th>Title:</th><td>".$info['title'] . "</td></tr>";
  36. Print "<tr>";
  37. Print "<th>First Name:</th> <td>".$info['first_name'] . " </td></tr>";
  38. Print "<tr>";
  39. Print "<th>Surname:</th> <td>".$info['surname'] . " </td></tr>";
  40. Print "<tr>";
  41. Print "<th>Address Line 1:</th> <td>".$info['addr_line_1'] . " </td></tr>";
  42. Print "<tr>";
  43. Print "<th>Address Line 2:</th> <td>".$info['addr_line_1'] . " </td></tr>";
  44. Print "<tr>";
  45. Print "<th>City:</th> <td>".$info['city'] . " </td></tr>";
  46. Print "<tr>";
  47. Print "<th>Postcode:</th> <td>".$info['postcode'] . " </td></tr>";
  48. Print "<tr>";
  49. Print "<th>Tel Number:</th> <td>".$info['tel_number'] . " </td></tr>";
  50. Print "<tr>";
  51. Print "<th>Mob Number:</th> <td>".$info['mob_number'] . " </td></tr>";
  52. Print "<tr>";
  53. Print "<th>Email:</th> <td>".$info['email'] . " </td></tr>";
  54. //Print "<tr>";
  55. //Print "<th>Username:</th> <td>".$info['username'] . " </td></tr>";
  56. //Print "<tr>";
  57. //Print "<th>Password:</th> <td>".$info['password'] . " </td></tr>";
  58. }
  59. Print "</table>";
  60. ?>
Last edited by peter_budo; Aug 6th, 2009 at 8:01 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Similar Threads
Reputation Points: 13
Solved Threads: 0
Newbie Poster
Newbi is offline Offline
12 posts
since Aug 2009
Aug 6th, 2009
0

Re: Query failed: You have an error in your SQL syntax; check the manual that corresponds

I don't know if this will solve your whole problem but one thing is staring me in the face:

php Syntax (Toggle Plain Text)
  1. $data = mysql_query("SELECT title, first_name, surname, addr_line_1, addr_line_2, city, postcode, tel_number, mob_number, email FROM members where id = '$_post[id]'")or die(mysql_error());
  2. $result = mysql_query($data)or die ("Query failed: " . mysql_error());

There should be no mysql_query() on the first line. You can also delete the or die at the end of that line. Instead, try this:
php Syntax (Toggle Plain Text)
  1. $data = "SELECT title, first_name, surname, addr_line_1, addr_line_2, city, postcode, tel_number, mob_number, email FROM members WHERE id = '$_post[id]'";
  2. $result = mysql_query($data)or die ("Query failed: " . mysql_error());
Reputation Points: 45
Solved Threads: 28
Posting Whiz in Training
Dukane is offline Offline
282 posts
since Oct 2006
Aug 7th, 2009
0

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Thanks Dukane i tried what u said & i got over that part of the problem but now it is giving me the abv error and wont display the information that i am asking for. The revised code below.

php Syntax (Toggle Plain Text)
  1. <?php
  2. ini_set('display_errors',1);
  3. error_reporting(E_ALL);
  4. // Connects to your Database
  5. $dbhost = "localhost"; // variable holding host information
  6. $dbname = "sportscentre"; // variable holding the name of the database
  7. $dbuser = "*****"; // variable holding the username of the person using database account
  8. $dbpass = "*****"; // variable holding password of the person using database account
  9.  
  10. $connection = mysql_connect($dbhost, $dbuser, $dbpass)or die("Cannot connect");
  11.  
  12. $connection = mysql_select_db($dbname)or die("cannot select DB");
  13.  
  14. $id = $_POST['id'];
  15.  
  16. $data = "SELECT title, first_name, surname, addr_line_1, addr_line_2, city, postcode, tel_number, mob_number, email FROM members where id = '$id'";
  17.  
  18. $result = mysql_query($data)or die ("Query failed: " . mysql_error());
  19.  
  20. Print "<table border cellpadding=3>";
  21. while($info = mysql_fetch_array( $data ))
  22. {
  23. Print "<tr>";
  24. Print "<th>Title:</th><td>".$info['title'] . "</td></tr>";
  25. Print "<tr>";
  26. Print "<th>First Name:</th> <td>".$info['first_name'] . " </td></tr>";
  27. Print "<tr>";
  28. Print "<th>Surname:</th> <td>".$info['surname'] . " </td></tr>";
  29. Print "<tr>";
  30. Print "<th>Address Line 1:</th> <td>".$info['addr_line_1'] . " </td></tr>";
  31. Print "<tr>";
  32. Print "<th>Address Line 2:</th> <td>".$info['addr_line_1'] . " </td></tr>";
  33. Print "<tr>";
  34. Print "<th>City:</th> <td>".$info['city'] . " </td></tr>";
  35. Print "<tr>";
  36. Print "<th>Postcode:</th> <td>".$info['postcode'] . " </td></tr>";
  37. Print "<tr>";
  38. Print "<th>Tel Number:</th> <td>".$info['tel_number'] . " </td></tr>";
  39. Print "<tr>";
  40. Print "<th>Mob Number:</th> <td>".$info['mob_number'] . " </td></tr>";
  41. Print "<tr>";
  42. Print "<th>Email:</th> <td>".$info['email'] . " </td></tr>";
  43. //Print "<tr>";
  44. //Print "<th>Username:</th> <td>".$info['username'] . " </td></tr>";
  45. //Print "<tr>";
  46. //Print "<th>Password:</th> <td>".$info['password'] . " </td></tr>";
  47. }
  48. Print "</table>";
  49. ?>
Last edited by Ezzaral; Aug 7th, 2009 at 12:22 pm. Reason: Added [code] [/code] tags. Please use them to format any code that you post.
Reputation Points: 13
Solved Threads: 0
Newbie Poster
Newbi is offline Offline
12 posts
since Aug 2009
Aug 7th, 2009
0

Re: Query failed: You have an error in your SQL syntax; check the manual that corresponds

it says it has a problem with this line,
while($info = mysql_fetch_array( $data ))
Reputation Points: 13
Solved Threads: 0
Newbie Poster
Newbi is offline Offline
12 posts
since Aug 2009
Aug 9th, 2009
0

Re: Query failed: You have an error in your SQL syntax; check the manual that corresponds

This is because the variable $data contains a string which is your SQL string. The actual data is in $result. So your line should be
php Syntax (Toggle Plain Text)
  1. while($info = mysql_fetch_array( $data ))
Reputation Points: 45
Solved Threads: 28
Posting Whiz in Training
Dukane is offline Offline
282 posts
since Oct 2006
Aug 10th, 2009
1

Re: Query failed: You have an error in your SQL syntax; check the manual that corresponds

@Dukane
You made the same error as Newbi..the correct code is below (Don't worry about it, I've made worse mistakes ):
PHP Syntax (Toggle Plain Text)
  1. while($info = mysql_fetch_array($result))
The reason for this is as Dukane explained...When you perform a query the query string is executed with the mysql_query() function. This function returns a special MySQL Result that you can then pass to mysql_fetch_array() or any other MySQL Result functions.
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008
Aug 12th, 2009
0

Re: Query failed: You have an error in your SQL syntax; check the manual that corresponds

Thank you that solved the problem
Reputation Points: 13
Solved Threads: 0
Newbie Poster
Newbi is offline Offline
12 posts
since Aug 2009
Aug 12th, 2009
0

Re: Query failed: You have an error in your SQL syntax; check the manual that corresponds

You're welcome! If your original problem is now solved, I ask that you mark this thread as solved.
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: can we use multipe and in the where clause
Next Thread in PHP Forum Timeline: Two different PHP aps for the same form data.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC