rdkd1970 Posted May 30, 2011 Share Posted May 30, 2011 I am not getting any error messages as I just think the codes are wrong. I am trying to get the $sql(s) to choose and print out the the looking_for in the database. However it is not printing anything. Any ideas of where I am need to fix this. I have tried a few ways with no luck. $sql = mysql_query("SELECT id,gender,looking_for FROM Members"); $nr = mysql_num_rows($sql); $sql2 = mysql_query("SELECT * FROM Members WHERE id='".($_POST['$looking_for'])."' LIMIT $pn, $itemsPerPage"); $outputList = ''; while($row = mysql_fetch_array($sql2)) { $id = $row["id"]; $username = $row["username"]; $firstname = $row["firstname"]; if (!$firstname) { $firstname = $username; } $gender=''; $looking_for=''; Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/ Share on other sites More sharing options...
Pikachu2000 Posted May 30, 2011 Share Posted May 30, 2011 Separate the query string from the query execution, store it in a variable, and use the variable in the execution. Then echo the query string so you can see if it contains the values you'd expect it to contain. If it does, paste it into phpMyAdmin, and see what results are returned. $query = "SELECT field FROM table WHERE pk = $value"; $result = mysql_query($query); // ETC. echo "<br>$query<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222356 Share on other sites More sharing options...
chintansshah Posted May 30, 2011 Share Posted May 30, 2011 $sql2 = mysql_query("SELECT * FROM Members WHERE id='".$_POST['$looking_for']."' LIMIT $pn, $itemsPerPage"); Remove () brackets, you have write $ sign above "looking_for", please remove it and try again. Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222371 Share on other sites More sharing options...
rdkd1970 Posted May 30, 2011 Author Share Posted May 30, 2011 I removed the $ and also placed the codes into the database to see the results and it actually picked the only ones where I do not have anything in the gender or looking_for row. Were I need it to pick up on the person doing the looking_for their gender. $sql = mysql_query("SELECT * FROM Members WHERE gender='".($_POST['looking_for'])."'"); $nr = mysql_num_rows($sql) $sql2 = mysql_query("SELECT * FROM Members WHERE gender='".($_POST['looking_for'])."' LIMIT $pn, $itemsPerPage"); while($row = mysql_fetch_array($sql2)) Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222391 Share on other sites More sharing options...
mikesta707 Posted May 30, 2011 Share Posted May 30, 2011 try echoing your $_POST['looking_for'] variable and see what it has in it. I suspect it is empty Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222409 Share on other sites More sharing options...
rdkd1970 Posted May 30, 2011 Author Share Posted May 30, 2011 I have this is my while loop and it is not echoing anything you are right. while($row = mysql_fetch_array($sql2)) { $id = $row["id"]; $gender = $row["gender"]; $looking_for=$row["looking_for"]; $firstname = $row["firstname"]; if (!$firstname) { $firstname = $username; } Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222416 Share on other sites More sharing options...
mikesta707 Posted May 30, 2011 Share Posted May 30, 2011 So now the problem will be to see why your post variable is empty. Are you sure it has the right name? Can you post the code for the form that is sumbitting that post data? Try doing a print_r on $_POST and see if it is structured like you expect it to be Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222420 Share on other sites More sharing options...
rdkd1970 Posted May 30, 2011 Author Share Posted May 30, 2011 This is my registration info I just put the gender and looking_for in the values for this question. I also did the print and got nothing.??? $results = mysql_query("INSERT INTO Members (username,password,firstname,lastname,email,gender,looking_for,full_birthday, maritalStatus,country,sign_up_date) VALUES '".mysql_real_escape_string($_POST["gender"])."', '".mysql_real_escape_string($_POST["looking_for"])."', Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222427 Share on other sites More sharing options...
mikesta707 Posted May 30, 2011 Share Posted May 30, 2011 No i mean the form that is supposed to be posting the $_POST information Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222443 Share on other sites More sharing options...
Pikachu2000 Posted May 30, 2011 Share Posted May 30, 2011 Separate the query string from the query execution, store it in a variable, and use the variable in the execution. Then echo the query string so you can see if it contains the values you'd expect it to contain. If it does, paste it into phpMyAdmin, and see what results are returned. $query = "SELECT field FROM table WHERE pk = $value"; $result = mysql_query($query); // ETC. echo "<br>$query<br>"; Did you bother to do this? ^^^ Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222459 Share on other sites More sharing options...
rdkd1970 Posted May 30, 2011 Author Share Posted May 30, 2011 yes I got the names in the db that did not have any information by the gender and the looking_for. Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222472 Share on other sites More sharing options...
Pikachu2000 Posted May 30, 2011 Share Posted May 30, 2011 OK. Please post the markup for the form, and in your script that processes the form, add this at the top then post the output it generates after the form has been submitted: echo '<br>///////////// POST ARRAY //////////////<br><pre>'; print_r($_POST); echo '</pre><br>///////////// END POST ARRAY //////////////<br>'; Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222482 Share on other sites More sharing options...
rdkd1970 Posted May 30, 2011 Author Share Posted May 30, 2011 Here is the results from the adding this to the top of my form ///////////// POST ARRAY ////////////// Array ( [username] => nathalie [password] => nathalie1 [firstname] => Nathalie [lastname] => Davis [email] => nathalie@davis.com [gender] => Female [looking_for] => Male [birth_month] => 06 [birth_day] => 24 [birth_year] => 1940 [maritalStatus] => Divorced [country] => Falkland Islands [humanCheck] => [submit] => go to next page ) ///////////// END POST ARRAY ////////////// Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222490 Share on other sites More sharing options...
Pikachu2000 Posted May 30, 2011 Share Posted May 30, 2011 I think I just spotted the issue. The parentheses are within the single quotes in the query string, thus the value in the table would need to literally be "(Male)" to match. Change the query strings to eliminate the parentheses and string concatenation. You should also be validating and sanitizing the value before using it in a db query string. // VALIDATE THE VALUE, SANITIZE AND ASSIGN IF VALID $valid = array( 'Male', 'Female'); if( in_array($valid, $_POST['looking_for']) ) { $looking_for = mysql_real_escape_string($_POST['looking_for']); } else { // an invalid value was submitted, so throw an error, or whatever. } $sql2 = mysql_query("SELECT * FROM Members WHERE id = '$looking_for' LIMIT $pn, $itemsPerPage"); $outputList = ''; Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222494 Share on other sites More sharing options...
rdkd1970 Posted May 30, 2011 Author Share Posted May 30, 2011 I tried this put the if statement at the beginning of the script with the echo to be need looking_for in case the if statement did not work. the results is the page shows that I have 10 names in the db but does not show the names. Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222505 Share on other sites More sharing options...
Pikachu2000 Posted May 30, 2011 Share Posted May 30, 2011 Sorry, I have a typo in the in_array() function. The two arguments should be reversed. if( in_array($_POST['looking_for'], $valid) ) { Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222527 Share on other sites More sharing options...
rdkd1970 Posted May 30, 2011 Author Share Posted May 30, 2011 Results live were blank no output Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222546 Share on other sites More sharing options...
Pikachu2000 Posted May 30, 2011 Share Posted May 30, 2011 Post your current code. Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222548 Share on other sites More sharing options...
rdkd1970 Posted May 30, 2011 Author Share Posted May 30, 2011 This is code presently. I was not sure to leave out the pagination codes as they are working fine. // VALIDATE THE VALUE, SANITIZE AND ASSIGN IF VALID $valid = array( 'Male', 'Female'); if( in_array($_POST['looking_for'],$valid) ) { $looking_for = mysql_real_escape_string($_POST['looking_for']); } else { // an invalid value was submitted, so throw an error, or whatever. } // DEAFAULT QUERY STRING $queryString = "WHERE gender= '$looking_for' ORDER BY looking_for ASC"; // DEFAULT MESSAGE ON TOP OF RESULT DISPLAY $queryMsg = "Showing looking_for"; $sql = mysql_query("SELECT * FROM Members ORDER BY gender = '$looking_for'"); $nr = mysql_num_rows($sql); $sql2 = mysql_query("SELECT * FROM Members ORDER BY gender = '$looking_for' LIMIT $pn, $itemsPerPage"); $outputList = ''; while($row = mysql_fetch_array($sql2)) { $id = $row["id"]; $gender = $row["gender"]; $looking_for=$row["looking_for"]; $firstname = $row["firstname"]; if (!$firstname) { $firstname = $username; } Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222554 Share on other sites More sharing options...
rdkd1970 Posted May 30, 2011 Author Share Posted May 30, 2011 The code I just sent you produces all the names in the db. it did not pull out the users as gender and display all they were looking_for Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222557 Share on other sites More sharing options...
Pikachu2000 Posted May 30, 2011 Share Posted May 30, 2011 How did WHERE get changed to ORDER BY in the $sql1 and $sql2 query strings? Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222567 Share on other sites More sharing options...
rdkd1970 Posted May 30, 2011 Author Share Posted May 30, 2011 The WHERE clause was not producing anything it left the page blank. The order by which may be wrong printed all the names. So that is wrong too. Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222572 Share on other sites More sharing options...
Pikachu2000 Posted May 30, 2011 Share Posted May 30, 2011 Obviously we're not making a lot of progress with this, so let's do some debugging. Paste this code into a new script and post the output so we can start to figure out where the root of the problem is. <?php // include your database connection credentials here $query = "SELECT gender FROM Members"; if( !result = mysql_query($query) ) { echo "Query $query<br>Failed with error: " . mysql_error()); } else { while( $array = mysql_fetch_assoc($result) ) { var_dump($array['gender']); echo '<br>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222581 Share on other sites More sharing options...
rdkd1970 Posted May 30, 2011 Author Share Posted May 30, 2011 I am getting an error in the codes from Dreamweaver on the lines if(!result...) and echo Query. I will try to see where it is going Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222585 Share on other sites More sharing options...
Pikachu2000 Posted May 30, 2011 Share Posted May 30, 2011 Don't run it through dreamweaver; simply paste it in to a new script, and call it into your browser. Quote Link to comment https://forums.phpfreaks.com/topic/237872-while-loop-to-pick-a-certain-type-from-db/#findComment-1222588 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.