Connecting Tech Pros Worldwide Forums | Help | Site Map

mysql values to display on PHP form

ddtpmyra's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: CA
Posts: 231
#1: Jun 18 '09
Hi,

Im looking for sample code that will retrieve the values from mysql query. Most of my code has the "while($row = mysql_fetch_assoc($result))" and that will display the values by rows. How about the display are in indent format?


Belowi s the input text box that I wanted to show the values from mysql query. Anybody can show me how not using the while function?
Expand|Select|Wrap|Line Numbers
  1.  
  2. <td>Total English</td>
  3.       <Input type = 'text' Name ='TotalSTSEnglish' value='0' >
  4. <td>Total Spanish</td> 
  5.       <Input type = 'text' Name ='TotalSTSEnglish' value='0' >   

ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,441
#2: Jun 18 '09

re: mysql values to display on PHP form


The while() loop will keep fetching new rows until mysql_fetch_assoc() returns FALSE, which means there are no more rows to fetch.
So if you only need to get one row from your mysql query (assuming your query returns only one row) here is the solution.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. mysql_connect("localhost","root","") or die(mysql_error());
  3. mysql_select_db("books");
  4. $result = mysql_query("select * from books "); // You can put your where clause here 
  5. $row = mysql_fetch_assoc($result);
  6. $book_name =  $row['name'];
  7. ?>
  8.  
Now print the variables inside the value attribute on the input box. hope this will help. Thanks
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 4,295
#3: Jun 18 '09

re: mysql values to display on PHP form


you may also look at PDO's fetch*() methods.
ddtpmyra's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: CA
Posts: 231
#4: Jun 23 '09

re: mysql values to display on PHP form


Thanks ak1dnar it works!
I appreciate the knowledge shared :)
Quote:

Originally Posted by ak1dnar View Post

The while() loop will keep fetching new rows until mysql_fetch_assoc() returns FALSE, which means there are no more rows to fetch.
So if you only need to get one row from your mysql query (assuming your query returns only one row) here is the solution.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. mysql_connect("localhost","root","") or die(mysql_error());
  3. mysql_select_db("books");
  4. $result = mysql_query("select * from books "); // You can put your where clause here 
  5. $row = mysql_fetch_assoc($result);
  6. $book_name =  $row['name'];
  7. ?>
  8.  
Now print the variables inside the value attribute on the input box. hope this will help. Thanks

ddtpmyra's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: CA
Posts: 231
#5: Jun 24 '09

re: mysql values to display on PHP form


A follow-up question on this topic... now I have a check box but Im having problem to pull the information. Inside the talbe the field value = 1, how come it wont display?

Expand|Select|Wrap|Line Numbers
  1. <table width='80%' border='0' summary=''>";
  2.     $result = mysql_query("select Total from recap where Cal_ID={$id} and category='Target Focus'  ");
  3.     $row = mysql_fetch_assoc($result);
  4.     $Total =  $row['Total'];
  5.     echo "<td><input type = 'checkbox' name ='Community' value='$row[Total]'/>Disabled Community </tr></td>";
  6.     echo "<tr>";                            
  7. echo "</table>
  8.  
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,441
#6: Jun 25 '09

re: mysql values to display on PHP form


check box values doesn't appear as the HTML output , did you check the generated html source from the browser view source option ?
ddtpmyra's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: CA
Posts: 231
#7: Jun 25 '09

re: mysql values to display on PHP form


I check my sql and the value is 1. And yet still no check apeared.


PHP
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("select Total from tblevent_recap where Cal_ID={$id} and category='Target Focus' and Name ='Disabled Community' ");
  2. $row= mysql_fetch_assoc($result);
  3. if(mysql_num_rows($result)!=0) {
  4. echo "<td><input type = 'checkbox' name ='DisabledCommunity' value='checked'>Disabled Community </tr></td>";
  5. echo "<tr>";  }
  6. else 
  7. echo "<td><input type = 'checkbox' name ='DisabledCommunity' value=''>Disabled Community </tr></td>";
  8. echo "<tr>";
  9. }
  10.  
  11.  
I veiwed the HTML
Expand|Select|Wrap|Line Numbers
  1. ><input type = 'checkbox' name ='DisabledCommunity' value='checked'>Disabled Community </tr></td><tr><td>
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 5,132
#8: Jun 25 '09

re: mysql values to display on PHP form


The correct attribute is 'checked'.

Expand|Select|Wrap|Line Numbers
  1. <input type='checkbox' checked='checked' />
  2.  
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,441
#9: Jun 26 '09

re: mysql values to display on PHP form


I think you may better to follow these tutorials on HTML form markup.
http://www.w3schools.com/html/html_forms.asp
Reply