| | |
echo data
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 100
Reputation:
Solved Threads: 1
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:
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.
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)
<?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."; } ?>
to concatenate in php you use a period/full stop
change this line
to
change this line
PHP Syntax (Toggle Plain Text)
echo "<p>",$lname, ", ",$fname, ", ",$dob;
to
PHP Syntax (Toggle Plain Text)
echo "<p>".$lname. ", ". $fname. ", ".$dob. ",".$reg_num;
If you find this useful you can add to my reputation
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*
*PM asking for help will be ignored*
•
•
Join Date: Jan 2008
Posts: 100
Reputation:
Solved Threads: 1
•
•
•
•
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 !
PHP Syntax (Toggle Plain Text)
<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.
mysql_num_rows returns only the number of rows returned by that query.
Hope it helps.
php Syntax (Toggle Plain Text)
<?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. Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Jan 2008
Posts: 100
Reputation:
Solved Threads: 1
•
•
•
•
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)
<?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.
I put it in like you said:
PHP Syntax (Toggle Plain Text)
$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:
PHP Syntax (Toggle Plain Text)
if($row = mysql_fetch_array($result) > null){
Is this wrong?
•
•
Join Date: Jan 2008
Posts: 100
Reputation:
Solved Threads: 1
Ok Nav33n, this I don't understand 
Going by what you gave me, i played with it and found this to work:
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

Going by what you gave me, i played with it and found this to work:
PHP Syntax (Toggle Plain Text)
$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."; }
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.
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.
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.
php Syntax (Toggle Plain Text)
$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 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)
$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."; }
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.
•
•
Join Date: Jan 2008
Posts: 100
Reputation:
Solved Threads: 1
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.
![]() |
Similar Threads
- Parse error, unexpected T_STRING on line 26, but I don't see it (PHP)
- receiving data from a form in PHP? (PHP)
- msql data transer problems (MySQL)
- Update entire Mysql DataBase with PhP (PHP)
- php wont submit data into the database (PHP)
- Uptime (Perl)
Other Threads in the PHP Forum
- Previous Thread: regular expressions in php
- Next Thread: is the checkbox checked?
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic echo email encode error fcc file files folder form forms function functions google howtowriteathesis href htaccess html image images include insert integration ip java javascript joomla ldap limit link login loop mail menu methods mlm mod_rewrite multiple mysql oop open parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql structure syntax system table template tutorial update upload url validation validator variable video web xml youtube






