duan00529 2013-05-26 12:43 采纳率: 100%
浏览 61
已采纳

如何使用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

  • 写回答

3条回答 默认 最新

  • doujiyun0041 2013-05-26 13:18
    关注

    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};
       }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试