echo data

Thread Solved

Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

echo data

 
0
  #1
Jan 28th, 2008
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:

  1. <?php
  2.  
  3. include ("connect.php");
  4.  
  5. //what data to query
  6. $lname=$_POST['lname'];
  7. $fname=$_POST['fname'];
  8. $dob=$_POST['dob'];
  9. //Select the query from the database
  10. $query= "select * from voter where lname='$lname' AND fname='$fname' AND dob='$dob'";
  11. //get results
  12. $result=mysql_query($query);
  13. $num_results = mysql_num_rows($result);
  14. //set response if data received, else show no data
  15. if($num_results > 0 ){
  16. echo "<p>",$lname, ", ",$fname, ", ",$dob;
  17. } else {
  18. echo "<br /><p>","Your record was not found.<br /><br /> Please contact our office at xxx-xxx-xxxx for assistance.";
  19. }
  20.  
  21. ?>
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['reg_num']; but this did not work. Please point in the right direction. Thank you.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 36
Reputation: tirivamwe is an unknown quantity at this point 
Solved Threads: 2
tirivamwe's Avatar
tirivamwe tirivamwe is offline Offline
Light Poster

Re: echo data

 
0
  #2
Jan 28th, 2008
to concatenate in php you use a period/full stop

change this line
  1. echo "<p>",$lname, ", ",$fname, ", ",$dob;

to
  1. echo "<p>".$lname. ", ". $fname. ", ".$dob. ",".$reg_num;
If you find this useful you can add to my reputation
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: echo data

 
0
  #3
Jan 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: echo data

 
0
  #4
Jan 28th, 2008
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 !
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: echo data

 
0
  #5
Jan 28th, 2008
Originally Posted by nav33n View Post
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:
  1. <form action="fromform.php" method="post"><center>
  2. <p><b><center><font face="Arial" size="4" color="#545429"> Enter your information in the form below,</b>
  3. <i>all three must be correct to receive positive results:</i></font></center></p>
  4. <table border= "2" style: "solid"; color="#545429" width="800" height="120%"><center>
  5. <tr><td><center>
  6. <p><b><center><font face="Arial" size="4" color="#545429"> Enter First Name:
  7. <input name="fname" type="text" id="fname" size="20"></p>
  8. <p>Enter Last Name: <input name="lname" type="text" id="lname" size="20"></p>
  9. <p>Enter Date of Birth (i.e. mmddyyyy Like 09231982):
  10. <input name="dob" type="text" id="dob" size="20"></p>
  11. <p><input type="submit" name="submit" value="submit"></p></font></center></b>
  12. </tr></td>
  13. </table>
  14. </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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: echo data

 
0
  #6
Jan 28th, 2008
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.
  1. <?php
  2. .....blah blah blah.....
  3. $query="select * from table where firstname='$fname' && lastname='$lastname' && date_of_birth='$dob'";
  4. $result=mysql_query($query);
  5. $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)){
  6. }
  7. //$row will have all the information. You can then use the columnname as the index.
  8. 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.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: echo data

 
0
  #7
Jan 28th, 2008
Originally Posted by nav33n View Post
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.
  1. <?php
  2. .....blah blah blah.....
  3. $query="select * from table where firstname='$fname' && lastname='$lastname' && date_of_birth='$dob'";
  4. $result=mysql_query($query);
  5. $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)){
  6. }
  7. //$row will have all the information. You can then use the columnname as the index.
  8. 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:
  1. $query= "select * from voter where lname='$lname' AND fname='$fname' AND dob='$dob'";
  2. //get results
  3. $result=mysql_query($query);
  4. $row = mysql_fetch_array($result);
  5. //set response if data received, else show no data
  6. if($row = mysql_fetch_array($result)){
  7. echo "Welcome". $row['fname']." ".$row['lname']." Your date of birth is". $row['dob']." and your registration number is ". $row['reg_num'];
  8. }
  9. else
  10. {
  11. echo "<br /><p>","Your record was not found.<br /><br /> Please contact our office at "phone number" for assistance.";
  12. }

Its displaying my echo only. This makes me think i'm supposed to have something like:
  1. if($row = mysql_fetch_array($result) > null){

Is this wrong?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: echo data

 
0
  #8
Jan 28th, 2008
Ok Nav33n, this I don't understand

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

  1. $query= "select * from voter where lname='$lname' AND fname='$fname' AND dob='$dob'";
  2. //get results
  3. $result=mysql_query($query);
  4. $row = mysql_fetch_array($result);
  5. //set response if data received, else show no data
  6. if($row){ //should be if($row = mysql_fetch_array($result)) {
  7. echo "Congratulations"." ". $row['fname']." Your date of birth is"." ". $row['dob']." and your registration number is ". $row['reg_num'];
  8. }
  9. else
  10. {
  11. echo "<br /><p>","Your record was not found.<br /><br /> Please contact our office at xxx-xxx-xxxx for assistance.";
  12. }
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 59
Reputation: world_weapon is an unknown quantity at this point 
Solved Threads: 2
world_weapon's Avatar
world_weapon world_weapon is offline Offline
Junior Poster in Training

Re: echo data

 
0
  #9
Jan 28th, 2008
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.
  1. $query="select * from voter where lname='$lname' AND fname='$fname' AND dob='$dob'";
  2. $result=mysql_query($query);
  3. if($row=mysql_fetch_array($result)){
  4. echo "Congratulations"." ". $row['fname']." Your date of birth is"." ". $row['dob']." and your registration number is ". $row['reg_num'];
  5. }
  6. else {
  7. echo "<br /><p>","Your record was not found.<br /><br /> Please contact our office at xxx-xxx-xxxx for assistance.";
  8. }
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.
  1. $query="select * from voter where lname='$lname' AND fname='$fname' AND dob='$dob'";
  2. $result=mysql_query($query);
  3. if($result){
  4. $row=mysql_fetch_array($result);
  5. echo "Congratulations"." ". $row['fname']." Your date of birth is"." ". $row['dob']." and your registration number is ". $row['reg_num'];
  6. }
  7. else {
  8. echo "<br /><p>","Your record was not found.<br /><br /> Please contact our office at xxx-xxx-xxxx for assistance.";
  9. }
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.
The purpose of my existence is why I am here.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: echo data

 
0
  #10
Jan 28th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC