mackiever Posted November 14, 2013 Share Posted November 14, 2013 I'm successfully connecting to my database but cannot retrieve/pull data from my database. I'm using wamp. I figured I can echo any message I want but as soon as I enter the while loop and try to echo anything or in my case echo the rows from the database it simply does not work. I don't get any errors when running on wamp and I'm running out of ideas as of why this is happening. I'm using a form tag and extracting the users/people stored in the database with gender = "male/female", depending on which "radio/option" the user decides. Any help would be highly appreciated. <html> <head> <title>Enter title here</title> </head> <body> <?php if ($_GET) { $gender = $_GET[ 'gender']; $connect = mysql_connect("localhost", "root", "mypassword"); if ($connect) { mysql_select_db("ourDatabase", $connect); $query = "SELECT * FROM mytable WHERE Gender = ' " . $gender. " ' "; $result = mysql_query($query); while($row = mysql_fetch_array($result) ) { echo $row[ ' Name ' ] . "<br/>" . $row[ ' Surname ' ] . "<br/>" . $row[ ' Email ' ] ; } } else { die (mysql_error() ); } } ?> <form action="" method = "GET"> find entries that are: <br/> <input type= "radio" name = "gender" value = "Male">Male</input><br/> <input type = "radio" name = "gender" value = "Female">Female</input><br/> <input type ="submit" value = "Select"/> </form> </body> <html> Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted November 14, 2013 Share Posted November 14, 2013 your query statement contains a space character, both before and after the $gender value, so unless you actually have spaces stored in with your data, the query will never match anything. in a large amount of cases of a beginner using confrontation to build a string in php, they end up with errors due to the clutter. use the simplest, least mix of different contexts, to produce any statement - $query = "SELECT * FROM mytable WHERE Gender = '$gender'"; Quote Link to comment Share on other sites More sharing options...
KaiSheng Posted November 14, 2013 Share Posted November 14, 2013 (edited) 1) I am aware that form can only be post by method=post , not method=get, not sure if yours will work that way. 2) You called for Name and Surname to be echoed, since there is a while loop to fetch array results from db, i think you should do this $Name = $row['Name']; $SurName = $row['SurName']; then followed by echo '$Name' and echo '$SurName' , step by step instead of combining everything together. before you echo it out. 3) $query = "SELECT * FROM mytable WHERE Gender = ' " . $gender. " ' "; change to $query = "SELECT * FROM mytable WHERE Gender = '".$gender."' "; 4) I see an email echoing out from your while loop, while there is no email from the form. Edited November 14, 2013 by KaiSheng Quote Link to comment Share on other sites More sharing options...
mackiever Posted November 14, 2013 Author Share Posted November 14, 2013 Hey guys I'd like to thank you for your quick response it really helped see what was wrong. It all came down to this specific line: 1) WHERE Gender = ' ".$gender." ' "; it seems the above line will not work this way because the SPACE between '[single quote] "[double-quotes]. 2) WHERE Gender = '".$gender."' "; will work or its equivalent: WHERE Gender = '$gender' "; Both work. I guess my new question is what's the difference between the two working statements? Once again, thank you! Quote Link to comment Share on other sites More sharing options...
KaiSheng Posted November 14, 2013 Share Posted November 14, 2013 The difference is, you only use the first one when you need to concatenate a string contains with php variable.you only use second one when there is only a variable. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.