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

Reply

Join Date: Aug 2009
Posts: 12
Reputation: Newbi is an unknown quantity at this point 
Solved Threads: 0
Newbi Newbi is offline Offline
Newbie Poster

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

 
0
  #1
Aug 6th, 2009
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 263
Reputation: Dukane is an unknown quantity at this point 
Solved Threads: 22
Dukane's Avatar
Dukane Dukane is offline Offline
Posting Whiz in Training

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

 
0
  #2
Aug 6th, 2009
I don't know if this will solve your whole problem but one thing is staring me in the face:

  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:
  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());
It is very important to read this: http://www.catb.org/~esr/faqs/smart-questions.html
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 12
Reputation: Newbi is an unknown quantity at this point 
Solved Threads: 0
Newbi Newbi is offline Offline
Newbie Poster

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

 
0
  #3
Aug 7th, 2009
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 12
Reputation: Newbi is an unknown quantity at this point 
Solved Threads: 0
Newbi Newbi is offline Offline
Newbie Poster

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

 
0
  #4
Aug 7th, 2009
it says it has a problem with this line,
while($info = mysql_fetch_array( $data ))
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 263
Reputation: Dukane is an unknown quantity at this point 
Solved Threads: 22
Dukane's Avatar
Dukane Dukane is offline Offline
Posting Whiz in Training

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

 
0
  #5
Aug 9th, 2009
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
  1. while($info = mysql_fetch_array( $data ))
It is very important to read this: http://www.catb.org/~esr/faqs/smart-questions.html
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 191
Reputation: FlashCreations is an unknown quantity at this point 
Solved Threads: 15
FlashCreations's Avatar
FlashCreations FlashCreations is offline Offline
Junior Poster

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

 
1
  #6
Aug 10th, 2009
@Dukane
You made the same error as Newbi..the correct code is below (Don't worry about it, I've made worse mistakes ):
  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.
FlashCreations
(aka PhpMyCoder)

About Me | My Blog | Contact Me
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 12
Reputation: Newbi is an unknown quantity at this point 
Solved Threads: 0
Newbi Newbi is offline Offline
Newbie Poster

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

 
0
  #7
Aug 12th, 2009
Thank you that solved the problem
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 191
Reputation: FlashCreations is an unknown quantity at this point 
Solved Threads: 15
FlashCreations's Avatar
FlashCreations FlashCreations is offline Offline
Junior Poster

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

 
0
  #8
Aug 12th, 2009
You're welcome! If your original problem is now solved, I ask that you mark this thread as solved.
FlashCreations
(aka PhpMyCoder)

About Me | My Blog | Contact Me
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC