如何使用while循环和在php中的变量中存储的fetch值从数据库中获取所有行数据
hi everyone i have table in the database from where i fetch data one column of table has user input n i fetch the data on the basis of user input like if user enter 2 in the textbox only that rows that contain number 2 will display like code of insertion on the basis of no of room
<?php
$d=$_POST['roomno'];
echo $d;
?>
<?php
//database connection
$db = new PDO("mysql:host=localhost;dbname=ems",'root','');
//query
$sql = "INSERT INTO table1 (c,c1, c2, c3, c4,c5,c6,c7,c8,c9,c10,c11,c12,c13) VALUES ('',:c1, :c2, :c3, :c4,:c5, :c6, :c7, :c8, :c9,:c10, :c11, :c12,'$d')";
$stmt = $db->prepare($sql);
foreach ($data_t1 as $i => $value) {
$stmt->execute(array(
':c1'=>$data_t1[$i],
':c2'=>$data_t2[$i],
':c3'=>$data_t3[$i],
':c4'=>$data_t4[$i],
':c5'=>$data_t11[$i],
':c6'=>$data_t22[$i],
':c7'=>$data_t33[$i],
':c8'=>$data_t44[$i],
':c9'=>$data_t111[$i],
':c10'=>$data_t222[$i],
':c11'=>$data_t333[$i],
':c12'=>$data_t444[$i],
));
} ?>
coding for fetchng the data
<?php
include('config.php');
$sa="select * from table1 where c13='$d'";
$result=mysql_query($sa) or die(mysql_error());
echo "<table border='1'>
<tr>
</tr>";
<?php
$i = 1;
while($row = mysql_fetch_array($result)) {
echo ${"r" . $i . "1"} = $row['c'];
echo ${"r" . $i . "2"} = $row['c1'] ;
echo ${"r" . $i . "3"} = $row['c2'];
echo ${"r" . $i . "4"} = $row['c3'];
echo ${"r" . $i . "5"} = $row['c4'];
$i++;
}
result
$d=3;mean user enter 3
3 room deatils enter in three rows of table like ths
row1 2 4 5 6 3(user enter value)
row2 12 14 15 16 3(user enter value)
row3 22 44 55 63 3(user enter value)
now as i mention in my fetch query that fetch the data where c13(room)=user input($d);
result of theses above three row display
now i want to store each of theses three row values in different varaibles
in the similar manner if user enter 2 or 4 i want to store two or four rows values in different varaible
I recommand you to use Michael Sazonov's way.. but if you don't want arrays here you go:
<?php
$i = 1;
while($row = mysql_fetch_array($result)) {
${"r" . $i . "1"} = $row['c'];
${"r" . $i . "2"} = $row['c1'] ;
${"r" . $i . "3"} = $row['c2'];
${"r" . $i . "4"} = $row['c3'];
${"r" . $i . "5"} = $row['c4'];
$i++;
}
Now The contents are stored in $r11 $r12 $r13.. $r35 and you have to echo them manually or using 2 for loops
<?php
for($i=1; $i<=3; $i++) {
for($j=0; $j<=5; $j++) {
echo ${'r'.$i.$j};
}
}
?>
Simply:
$data = array() ; //Declare an array
while($row = mysql_fetch_array($result)){
$data[] = $row ; //Add every row to array
}
echo $data['c4'] ; // Work with the array ETC
Of course it will display only the last row. Within each iteration over the query result you are rewriting the $r
-nth variable, so the result you'll see after the loop are only the values that were assigned during the last iteration.
Use
$row = array();
while($row[] = mysql_fetch_array($result)){
}
As result you will get all the data stored as an array in $row
.
What it does is as follows:
Iterates over the query result and and fetching it. The fetching method returns an array of the current iterated row values. This array is pushed into the $row
array. So, each row value could be accessed at a corresponding index of $row
.
Use print_r( $row );
to see the structure.
Further use of the array is as follows:
echo $row[ 0 ][ 'c1' ];
echo $row[ 0 ][ 'c2' ];
echo $row[ 1 ][ 'c3' ];
If you want to show the input depending on a dynamical rows amount, use this:
$count = count( $row );
for( $i = 0 ; $i < $count; $i++ ){
echo $row[ $i ][ 'c1' ];
}