mysql values to display on PHP form 
June 18th, 2009, 12:53 AM
|  | Familiar Sight | | Join Date: Jun 2008 Location: CA
Posts: 204
| |
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? -
-
<td>Total English</td>
-
<Input type = 'text' Name ='TotalSTSEnglish' value='0' >
-
<td>Total Spanish</td>
-
<Input type = 'text' Name ='TotalSTSEnglish' value='0' >
| 
June 18th, 2009, 04:36 AM
|  | Moderator | | Join Date: Jan 2007 Location: Colombo
Posts: 1,439
| | | 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. -
<?php
-
mysql_connect("localhost","root","") or die(mysql_error());
-
mysql_select_db("books");
-
$result = mysql_query("select * from books "); // You can put your where clause here
-
$row = mysql_fetch_assoc($result);
-
$book_name = $row['name'];
-
?>
-
Now print the variables inside the value attribute on the input box. hope this will help. Thanks
| 
June 18th, 2009, 06:34 AM
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9 | | | re: mysql values to display on PHP form
you may also look at PDO's fetch*() methods.
| 
June 23rd, 2009, 12:51 AM
|  | Familiar Sight | | Join Date: Jun 2008 Location: CA
Posts: 204
| | | re: mysql values to display on PHP form
Thanks ak1dnar it works!
I appreciate the knowledge shared :) Quote:
Originally Posted by ak1dnar 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. -
<?php
-
mysql_connect("localhost","root","") or die(mysql_error());
-
mysql_select_db("books");
-
$result = mysql_query("select * from books "); // You can put your where clause here
-
$row = mysql_fetch_assoc($result);
-
$book_name = $row['name'];
-
?>
-
Now print the variables inside the value attribute on the input box. hope this will help. Thanks | | 
June 24th, 2009, 01:11 AM
|  | Familiar Sight | | Join Date: Jun 2008 Location: CA
Posts: 204
| | | 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? -
<table width='80%' border='0' summary=''>";
-
$result = mysql_query("select Total from recap where Cal_ID={$id} and category='Target Focus' ");
-
$row = mysql_fetch_assoc($result);
-
$Total = $row['Total'];
-
echo "<td><input type = 'checkbox' name ='Community' value='$row[Total]'/>Disabled Community </tr></td>";
-
echo "<tr>";
-
echo "</table>
-
| 
June 25th, 2009, 07:57 AM
|  | Moderator | | Join Date: Jan 2007 Location: Colombo
Posts: 1,439
| | | 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 ?
| 
June 25th, 2009, 05:23 PM
|  | Familiar Sight | | Join Date: Jun 2008 Location: CA
Posts: 204
| | | re: mysql values to display on PHP form
I check my sql and the value is 1. And yet still no check apeared. PHP -
$result = mysql_query("select Total from tblevent_recap where Cal_ID={$id} and category='Target Focus' and Name ='Disabled Community' ");
-
$row= mysql_fetch_assoc($result);
-
if(mysql_num_rows($result)!=0) {
-
echo "<td><input type = 'checkbox' name ='DisabledCommunity' value='checked'>Disabled Community </tr></td>";
-
echo "<tr>"; }
-
else
-
{
-
echo "<td><input type = 'checkbox' name ='DisabledCommunity' value=''>Disabled Community </tr></td>";
-
echo "<tr>";
-
}
-
-
I veiwed the HTML -
><input type = 'checkbox' name ='DisabledCommunity' value='checked'>Disabled Community </tr></td><tr><td>
| 
June 25th, 2009, 06:53 PM
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,858
Provided Answers: 9 | | | re: mysql values to display on PHP form
The correct attribute is 'checked'. -
<input type='checkbox' checked='checked' />
-
| 
June 26th, 2009, 04:57 AM
|  | Moderator | | Join Date: Jan 2007 Location: Colombo
Posts: 1,439
| | | 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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|