Good Morning,

I have a script that is working just fine as it stands, thanks to ya'll :) I wanted to make a change to it but am having trouble making this change work. The scripts says to "select * from my databases", using "Where" three data inputs. This tells me that I should be able to echo anything from the database because of the "select *". However, the only thinks I can display are the 3 data inputs. Here is my code:

<?php 

include ("connect.php"); 

//what data to query
$lname=$_POST['lname'];
$fname=$_POST['fname'];
$dob=$_POST['dob'];
//Select the query from the database
$query= "select * from voter where lname='$lname' AND fname='$fname' AND dob='$dob'";
//get results
$result=mysql_query($query);
$num_results = mysql_num_rows($result);
//set response if data received, else show no data
if($num_results > 0 ){
echo "<p>",$lname, ", ",$fname, ", ",$dob;
} else {
echo "<br /><p>","Your record was not found.<br /><br /> Please contact our office at xxx-xxx-xxxx for assistance.";
}

?>

If I add an additional data type to my echo (i.e., $reg_num) i get a blank screen. It seems to not want me to see any other data that is in my table other than the three mentioned above (lname, fname, dob). How do I tell it that i want to display more data than what is being shown? I tried adding a line to lines 4 - 6 such as $reg_num=$_POST; but this did not work. Please point in the right direction. Thank you.

to concatenate in php you use a period/full stop

change this line

echo "<p>",$lname, ", ",$fname, ", ",$dob;

to

echo "<p>".$lname. ", ". $fname. ", ".$dob. ",".$reg_num;

I'm afraid this did not work. I tried replacing 'dob' with 'reg_num' but it only displays the fname and lname. If I leave the string the way it is and add the 'reg_num', it displays a blank white page.

Thats because, reg_num variable is null. Where is reg_num coming from ? From a previous page ? You can use print_r($_POST) for debugging purpose. This will print all the variables that are posted from page1 to page2.(PS. You should write print_r($_POST); in page 2.) Try that and tell me if you are posting reg_num !

Thats because, reg_num variable is null. Where is reg_num coming from ? From a previous page ? You can use print_r($_POST) for debugging purpose. This will print all the variables that are posted from page1 to page2.(PS. You should write print_r($_POST); in page 2.) Try that and tell me if you are posting reg_num !

This is page 2 of 2, page 1 is where the user inputs the 3 items (lname, fname, dob), clicks submit, then page 2 does the communicating with mysql. Here is the input form i have from page 1:

<form action="fromform.php" method="post"><center>
<p><b><center><font face="Arial" size="4" color="#545429"> Enter your information in the form below,</b>
<i>all three must be correct to receive positive results:</i></font></center></p>
<table border= "2" style: "solid"; color="#545429" width="800" height="120%"><center>
<tr><td><center>
<p><b><center><font face="Arial" size="4" color="#545429"> Enter First Name: 
<input name="fname" type="text" id="fname" size="20"></p>
<p>Enter Last Name: <input name="lname" type="text" id="lname" size="20"></p>
<p>Enter Date of Birth (i.e. mmddyyyy Like 09231982): 
<input name="dob" type="text" id="dob" size="20"></p>
<p><input type="submit" name="submit" value="submit"></p></font></center></b>
</tr></td>
</table>
</center></form>

Page 2's query runs based on the input from this one. But what i want is to be able to display other data within the table on page 2 as well.

I'm not sure I understand the print_r($_POST) you are referring to, how will this go in page one?

Oh, Well, first of all, you are not fetching the record from the table. What you are doing is, checking if a row exists in the table with the provided details. If yes, then print the "input values". But you should do something like this.

<?php
.....blah blah blah.....
$query="select * from table where firstname='$fname' && lastname='$lastname' && date_of_birth='$dob'";
$result=mysql_query($query);
$row=mysql_fetch_array($result); //if you are sure that the query returns only 1 row. If you are not sure, then use the while loop. while($row=mysql_fetch_array($result)){
}
//$row will have all the information. You can then use the columnname as the index.
echo "Welcome". $row['fname']."  ".$row['lastname']." Your date of birth is". $row['date_of_birth']." and your registration number is ". $row['reg_num'];

mysql_num_rows returns only the number of rows returned by that query.
:) Hope it helps.

Oh, Well, first of all, you are not fetching the record from the table. What you are doing is, checking if a row exists in the table with the provided details. If yes, then print the "input values". But you should do something like this.

<?php
.....blah blah blah.....
$query="select * from table where firstname='$fname' && lastname='$lastname' && date_of_birth='$dob'";
$result=mysql_query($query);
$row=mysql_fetch_array($result); //if you are sure that the query returns only 1 row. If you are not sure, then use the while loop. while($row=mysql_fetch_array($result)){
}
//$row will have all the information. You can then use the columnname as the index.
echo "Welcome". $row['fname']."  ".$row['lastname']." Your date of birth is". $row['date_of_birth']." and your registration number is ". $row['reg_num'];

mysql_num_rows returns only the number of rows returned by that query.
:) Hope it helps.

If i still use the "if" statement, then do I still need a comparison? like "if x is > 0". Right now its just giving me my "else" results. I can use an IF because the results of the input form will produce either 1 row of data else no data at all.

I put it in like you said:

$query= "select * from voter where lname='$lname' AND fname='$fname' AND dob='$dob'";
//get results
$result=mysql_query($query);
$row = mysql_fetch_array($result);
//set response if data received, else show no data
if($row = mysql_fetch_array($result)){
echo "Welcome". $row['fname']."  ".$row['lname']." Your date of birth is". $row['dob']." and your registration number is ". $row['reg_num'];
} 
else 
{
echo "<br /><p>","Your record was not found.<br /><br /> Please contact our office at "phone number" for assistance.";
}

Its displaying my echo only. This makes me think i'm supposed to have something like:

if($row = mysql_fetch_array($result) > null){

Is this wrong?

Ok Nav33n, this I don't understand :)

Going by what you gave me, i played with it and found this to work:

$query= "select * from voter where lname='$lname' AND fname='$fname' AND dob='$dob'";
//get results
$result=mysql_query($query);
$row = mysql_fetch_array($result);
//set response if data received, else show no data
if($row){ //should be if($row = mysql_fetch_array($result)) {
echo "Congratulations"." ". $row['fname']." Your date of birth is"." ". $row['dob']." and your registration number is ". $row['reg_num'];
}
else 
{
echo "<br /><p>","Your record was not found.<br /><br /> Please contact our office at xxx-xxx-xxxx for assistance.";
}

In my IF i just put if($row) and left the rest out and this worked. Not that i'm complaining, it would just be nice to know why, just for my learning curve and all :) Everything i've read tells me that the "$row = mysql_fetch_array($result)" needs to be in there.

confused :)

From what I have read, I think what you want to know is that mysql_fetch_array($result) returns an array if the mysql result had at least one row. It will return false if there was no row of data from the query. The first $row=mysql_fetch_array($result) assigns either false or an array of the data from the query to the variable $row. You could use that in the if() statement, but I don't know why it wouldn't work for you.

$query="select * from voter where lname='$lname' AND fname='$fname' AND dob='$dob'";
$result=mysql_query($query);
if($row=mysql_fetch_array($result)){
echo "Congratulations"." ". $row['fname']." Your date of birth is"." ". $row['dob']." and your registration number is ". $row['reg_num'];
}
else {
echo "<br /><p>","Your record was not found.<br /><br /> Please contact our office at xxx-xxx-xxxx for assistance.";
}

I use it that way quite a bit. It will evaluate the expression $row=mysql_fetch_array($result) and if it had a row, well then, $row would not equal false, making the statement true. It should therefore execute the code to display the information.
I don't know if defining the variable $row twice would cause problems. You can check out the way mysql_fetch_row() works at php.net. Although most of the time I use the $result variable to check if the query was successful instead.

$query="select * from voter where lname='$lname' AND fname='$fname' AND dob='$dob'";
$result=mysql_query($query);
if($result){
$row=mysql_fetch_array($result);
echo "Congratulations"." ". $row['fname']." Your date of birth is"." ". $row['dob']." and your registration number is ". $row['reg_num'];
}
else {
echo "<br /><p>","Your record was not found.<br /><br /> Please contact our office at xxx-xxx-xxxx for assistance.";
}

I guess it all depends on how you feel about it. If you are focused on speed, then you would probably use the latter because if the query returned no results, then $result would be false, and mysql_fetch_array($result) would never be executed because there would be no point to fetch an array from an empty result set. It would return false too. For more info on mysql_query() check out this at php.net.
If you are sure there is only one result this will work great for you. But if you need to do it for more than one result then the while($row=mysql_fetch_array($result)) will allow you to go through each row. You can make sure there is only one row if any matches occur by using the "limit 1" sql syntax. Basically change $query to $query="select * from voter where lname='$lname' AND fname='$fname' AND dob='$dob' LIMIT 1"; and only one row will exist regardless if the query matches multiple rows from the table.
Hope this helps you somehow.

Great explanation. The latter is closer to what i have. My query should result in either 1 record or no record, so what you've put here is perfect. I'll change my code to reflect what you've provided, its cleaner looking. Thanks for helping me understand, ya'll are doing a great job of helping us newbies, much appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.