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.");
}
?>