Meg Posted May 18, 2009 Report Share Posted May 18, 2009 (edited) I'm new to php/mySQL and stumbled across the phpkiller videos last week when researching resources that would help me to learn php and clarify things I do not understand in the books I have purchased. I watched the videos on conditional statements, functions and the SELECT command, and they helped to clarify those topics for me. I'm posting here (and I sincerely hope my post is not an example of a vampire post) because I can't find an example in my books (Powers, Ullman, Valade) that shows what I want to do: combine a while()loop with two blocks of if/then code. Below is the script I am writing. I would like it to take the name a user enters into a search box, check to see if the name is in the database, and echo one of two messages depending on whether or not the name is in the database. The script works for the first condition (the name is in the database), but it doesn't work when the name is not in the database. I've commented out the code that breaks the first script and have included a second script in which I rewrote the conditional statement part of the code, but the changes I made didn't get the result I want. What am I missing in the conditional block that will make the second message print? Thanks in advance for any help. if (isset($_POST['submit'])) { } include 'conf.inc.php'; $name_first = $_POST['name_first']; $name_last = $_POST['name_last']; $query = "SELECT * from tblparticipant WHERE name_first like '$name_first' AND name_last like '$name_last'"; if ($result = mysql_query($query)) {//Run the query. while ($row = mysql_fetch_array($result)){//message 1 echo "{$row['name_first']} {$row['name_last']} is in the database. Would you like to add slips now?"; } } /*else {//message 2 echo "{$row['name_first']} {$row['name_last']} is not in the database. Would you like to sign up .$name_first now?"; } *?/ else { die('Could not retreive data because: ' . mysql_error() .". The query was $query."); } ?> I reviewed the information I have on conditional statements, and decided that I hadn't really written one in the first example, (the if/else that is there is to provide a message if there is a connection problem) so I added the statements that begin with if ($row == '0'), but this breaks the entire script. <? if (isset($_POST['submit'])) { } include 'conf.inc.php'; $name_first = $_POST['name_first']; $name_last = $_POST['name_last']; $query = "SELECT * from tblparticipant WHERE name_first like '$name_first' AND name_last like '$name_last'"; if ($result = mysql_query($query)) {//Run the query. while ($row = mysql_fetch_array($result)){//message 1 if ($row == '0')//name is not in the database. I've also tried ($row > 0) and flipped the order of the conditions being tested echo "{$row['name_first']} {$row['name_last']} is not in the database. Would you like to sign up{$row['name_first']} now?"; } } else {//name is in the database echo "{$row['name_first']} {$row['name_last']} is is in the database. Would you like to add slips now"; } else { die('Could not retreive data because: ' . mysql_error() .". The query was $query."); } ?> Edited May 18, 2009 by Meg Quote Link to comment Share on other sites More sharing options...
jlhaslip Posted May 18, 2009 Report Share Posted May 18, 2009 Check for the number of rows returned from the query. http://ca2.php.net/mysql_num_rows Quote Link to comment Share on other sites More sharing options...
Meg Posted May 19, 2009 Author Report Share Posted May 19, 2009 Thanks for that reference. I had tried mysql_num_rows before posting and couldn't get anything to echo, which is why I went to the while() loop, as I at least was able to get the true statement to print. Here is the code I wrote with a second attempt with mysql_num_rows and no while() loop. I've commented out what I think each line is doing. I tried this code with while() also, but still no output. By the way, I have some pretty straight forward examples in my books that I can make work if I only want to see a name that is already in the database. None of these examples show also needing to provide for the name not being in the database. It seems to me that examples showing successful and unsuccessful logins are similar to what I want to do, (with the exception that I have two variables to print at once ($name_first, $name_last) rather than just one username) The organization of the script I have included here is based on a login scripts shown in a book example that uses mysql_num_rows. Do you see where my mistakes are? <?php if (isset($_POST['submit'])) {//check to see if form submitted } include 'conf.inc.php'; $name_first = $_POST['name_first']; $name_last = $_POST['name_last'];//user input defined as variables $query = "SELECT * from tblparticipant WHERE name_first like '$name_first' AND name_last like '$name_last'";//query statement with form variables included $result = mysql_query($query); {//Query runs. $num = mysql_num_rows($result); //check for number of rows returned from query if ($num > 0) //since only one row can be returned, this means name was found in database, so print the name with message { echo "{$row['name_first']} {$row['name_last']} is in the database. Would you like to add slips now?"; } elseif ($num == 0) {//name not found in database because it hasn't been entered yet, so print form input and message echo "{$row['name_first']}{$row['name_last']} is not in the database.Would you like to sign up{$row['name_first']}now?"; } ?> Quote Link to comment Share on other sites More sharing options...
jlhaslip Posted May 19, 2009 Report Share Posted May 19, 2009 if ($num > 0) //since only one row can be returned, this means name was found in database, so print the name with message { echo $name_first . ' is in the database. Would you like to add slips now?'; } else {//name not found in database because it hasn't been entered yet, so print form input and message echo $name_last . ' is not in the database. Would you like to sign up ' . $name_first . 'now? '; } Try that. You had not added any data from the Mysql search to any array, so $row was empty. Echo out the Search values only according to the IF statement. Since there are only two possible outcomes (found/not found), I removed the IF/ELSEIF and made it an IF/ELSE. *not tested* Quote Link to comment Share on other sites More sharing options...
Meg Posted May 20, 2009 Author Report Share Posted May 20, 2009 Here is the code with the suggested changes. Still not echoing anything out, but I think I understand your comments. To paraphrase, since I had removed the mysql_fetch_array(), (shown below) I was no longer asking to have the database results returned in an array, so there was no need to keep $row, is that correct? while ($row = mysql_fetch_array($result)){ echo "{$row['name_first']} {$row['name_last']} is in the database. Would you like to add slips now?"; Here is the script showing the IF/ELSE statement with the search values echoed out, but still no results on screen. I tried changing to double quotes (I see examples of that in the books I have) but that didn't change anything. <?php if (isset($_POST['submit'])) {//check to see if form submitted } include 'conf.inc.php'; $name_first = $_POST['name_first']; $name_last = $_POST['name_last'];//user input defined as variables $query = "SELECT * from tblparticipant WHERE name_first like '$name_first' AND name_last like '$name_last'";//query statement with form variables included $result = mysql_query($query); {//Query runs. $num = mysql_num_rows($result); //check for number of rows returned from query if ($num > 0) //since only one row can be returned, this means name was found in database, so print the name with message { echo $name_first. ' '.$name_last is in the database. Would you like to add slips now?'; } else {//name not found in database because it hasn't been entered yet, so print form input and message echo $name_first.' ' .$name_last is not in the database. Would you like to sign up .$name_first.' now? '; } ?> I've begun work on alternate design to get around this problem ( I have to come up with something that works by June 3!) but I'd really to solve it as it seems like it would be a pretty common need, don't you think? Quote Link to comment Share on other sites More sharing options...
jlhaslip Posted May 20, 2009 Report Share Posted May 20, 2009 $result = mysql_query($query); //Query runs. $num = mysql_num_rows($result); //check for number of rows returned from query echo 'Num Rows is : ' . $num . ' ' ; // add this to see what your num_rows value is being reported as if ($num > 0) { //since only one row can be returned, this means name was found in database, so print the name with message echo $name_first .' ' . $name_last . ' is in the database. Would you like to add slips now?'; } else {//name not found in database because it hasn't been entered yet, so print form input and message echo $name_first .' ' . $name_last . 'is not in the database. Would you like to sign up' .$name_first.' now? '; } ?> Try this. Quote Link to comment Share on other sites More sharing options...
Meg Posted May 20, 2009 Author Report Share Posted May 20, 2009 (edited) I added the line of code you suggested, but nothing is echoing out-just a blank page. $result = mysql_query($query); //Query runs. $num = mysql_num_rows($result); //check for number of rows returned from query echo 'Num Rows is : ' . $num . ' ' ; I know I am connecting OK because I can still get the search values to echo for the first condition with this code: <?php //This script works, but only when the first condition is met. if (isset($_POST['submit'])) { } include 'conf.inc.php'; $name_first = $_POST['name_first']; $name_last = $_POST['name_last']; $query = "SELECT * from tblparticipant WHERE name_first like '$name_first' AND name_last like '$name_last'"; if ($result = mysql_query($query)) {//Run the query. while ($row = mysql_fetch_array($result)){ echo "{$row['name_first']} {$row['name_last']} is in the database. Would you like to add slips now?"; } } else { die('Could not retreive data because: ' . mysql_error() .". The query was $query."); } ?> Edited May 20, 2009 by Meg Quote Link to comment Share on other sites More sharing options...
jlhaslip Posted May 20, 2009 Report Share Posted May 20, 2009 (edited) Place this line in the top of your php file. error_reporting(E_ALL); ini_set('display_errors', 1); // set to '1' for display errors on screen Edited May 20, 2009 by jlhaslip Quote Link to comment Share on other sites More sharing options...
Meg Posted May 20, 2009 Author Report Share Posted May 20, 2009 (edited) <?php I did it, but no error messages. I asked our tech guy (I'm a librarian) if error reporting was turned on and he said it was not. Is that something that has to be enabled on the server in order to see error messages? I get error messages when I use echo " Cound not add the record because: " . mysql_error() . ". The query was $query."; but is that for MySql and not PHP errors? Thanks for your help. error_reporting(E_ALL); ini_set('display_errors', 1); // set to '1' for display errors on screen if (isset($_POST['submit'])) {//check to see if form submitted } include 'conf.inc.php'; Edited May 20, 2009 by Meg Quote Link to comment Share on other sites More sharing options...
Meg Posted May 22, 2009 Author Report Share Posted May 22, 2009 Below is code that works, provided to me by a php programmer. Turns out there was no need for a while loop, and removing the "{$row['name_first']} in favor of $name_first $name_last results plus the $action= appropriate page returns the correct message. Thanks for your help troubleshooting, jlhaslip. $name_first = $_POST['name_first']; $name_last = $_POST['name_last']; $query = "SELECT * from tblparticipant WHERE name_first like '$name_first' AND name_last like '$name_last'"; if ($result = mysql_query($query)) { if ($row = mysql_fetch_array($result)){ echo "{$row['name_first']} {$row['name_last']} is in the database. Would you like to add reading slips now?"; $action = 'add_slips.html'; //This no longer goes to add_slips.html } else { echo "$name_first $name_last is not in the database. Would you like to add $name_first now?"; $action = 'add_member.html'; } } else { die('Could not retreive data because: ' . mysql_error() .". The query was $query."); } ?> 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.
Note: Your post will require moderator approval before it will be visible.