943,608 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 3721
  • PHP RSS
Jan 28th, 2008
0

echo data

Expand Post »
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 Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster
rickarro is offline Offline
107 posts
since Jan 2008
Jan 28th, 2008
0

Re: echo data

to concatenate in php you use a period/full stop

change this line
PHP Syntax (Toggle Plain Text)
  1. echo "<p>",$lname, ", ",$fname, ", ",$dob;

to
PHP Syntax (Toggle Plain Text)
  1. echo "<p>".$lname. ", ". $fname. ", ".$dob. ",".$reg_num;
Reputation Points: 11
Solved Threads: 2
Light Poster
tirivamwe is offline Offline
36 posts
since Oct 2005
Jan 28th, 2008
0

Re: echo data

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster
rickarro is offline Offline
107 posts
since Jan 2008
Jan 28th, 2008
0

Re: echo data

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 !
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jan 28th, 2008
0

Re: echo data

Click to Expand / Collapse  Quote originally posted by nav33n ...
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:
PHP Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 1
Junior Poster
rickarro is offline Offline
107 posts
since Jan 2008
Jan 28th, 2008
0

Re: echo data

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 Syntax (Toggle Plain Text)
  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.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jan 28th, 2008
0

Re: echo data

Click to Expand / Collapse  Quote originally posted by nav33n ...
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 Syntax (Toggle Plain Text)
  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:
PHP Syntax (Toggle Plain Text)
  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:
PHP Syntax (Toggle Plain Text)
  1. if($row = mysql_fetch_array($result) > null){

Is this wrong?
Reputation Points: 10
Solved Threads: 1
Junior Poster
rickarro is offline Offline
107 posts
since Jan 2008
Jan 28th, 2008
0

Re: echo data

Ok Nav33n, this I don't understand

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

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Junior Poster
rickarro is offline Offline
107 posts
since Jan 2008
Jan 28th, 2008
0

Re: echo data

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.
php Syntax (Toggle Plain Text)
  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.
php Syntax (Toggle Plain Text)
  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.
Reputation Points: 21
Solved Threads: 2
Junior Poster in Training
world_weapon is offline Offline
63 posts
since Apr 2004
Jan 28th, 2008
0

Re: echo data

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster
rickarro is offline Offline
107 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: regular expressions in php
Next Thread in PHP Forum Timeline: Inserting recurring event/appointment type





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC