dongll0502 2014-07-10 06:41
浏览 24
已采纳

如何获取行然后显示选项

Hi I have A newbie question. if id == 1 then display the commercial name that has a value of 1 follow by all the value from database then else when id == 0 display all the value..

I try this but not working please help me..

              $mod_query  = "SELECT * FROM ";
                      $mod_query .= "fm_third_party ORDER BY commercial_name asc";
                      $mod_result = mysql_query($mod_query) or die(mysql_error());



                $cname = array();           
                while ($row = mysql_fetch_assoc($mod_result))
                {
                    $cname = $row['id'];
                    $ids = $row['id'];

                    }

                    if ($ids == 1)
                    {
                        echo "<option>$cname</option>";

    do {  
    ?>
                      <option value="<?php echo $row_rsTP['third_party_id']?>"
    <?php if (!(strcmp($row_rsTP['third_party_id'], $row_ap['company_co']))) {echo "SELECTED";} ?>><?php echo $row_rsTP['commercial_name'] ." - ".$row_rsTP['date_expire'] . " - " .$row_rsTP['ao_id'];?></option>
                      <?php
    } while ($row_rsTP = mysql_fetch_assoc($rsTP));
    $rows = mysql_num_rows($rsTP);
    if($rows > 0) {
    mysql_data_seek($rsTP, 0);
    $row_rsTP = mysql_fetch_assoc($rsTP);
                }

    }
    else 
    {
        $mod_query1  = "SELECT * FROM ";
        $mod_query1 .= "fm_third_party ORDER BY commercial_name asc";
        $mod_result1 = mysql_query($mod_query1) or die(mysql_error());

        while ($row1 = mysql_fetch_assoc($mod_result1))
        {
            echo "<option value=".$row1['third_party_id'].">".$row1['commercial_name']. " </option>";
  • 写回答

1条回答 默认 最新

  • dqsw7529 2014-07-10 09:27
    关注
        Make a habit of using 'PDO', your coding will much easier www.php.net/manual/en/intro.pdo.php 
        I suggest you don't use mysql_ functions. They're deprecated, they're unsafe.
    
        how i would connect to a database.
    
        try {
            $pdo = new PDO('mysql:host=localhost;dbname=data', 'user','password');
    
            /*We’d like our PDO object to throw a PDOException any time it fails to do what we
            ask. We can configure it do to so by calling the PDO object’s setAttribute method:*/
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
            /*set the character encoding of a MySQL connection, 
            but the most reliable way is to run this SQL query: SET NAMES "utf8"*/
            $pdo->exec('SET NAMES "utf8"');
    
            $sql = "SELECT FROM fm_third_party ORDER BY commercial_name asc";
    
            /*The query method looks just like exec in that it accepts an SQL query as an argument
            to be sent to the database server; what it returns, however, is a PDOStatement object,
            which represents a result set containing a list of all the rows (entries) returned from
            the query.*/
            $results = $pdo->query($sql);
    
        } catch (PDOException $e) { // This only says all faults will be stored in a variable called '$e'
            $error = 'Unable to connect to the database server.';
            include 'error.html.php';
            exit();
        }
    ?>
    Now once you connect the 'PDO' way, you'll be able to do magic with your newly created '$pdo' object.
    
    Remember '$pdo' is now an object, think about that for a moment.......
    
    I would save my query results this way.
    
    $results = $pdo->query($sql);
    
    Instead of doing it this way 
    
    while ($row = mysql_fetch_assoc($mod_result)) {
        $cname = $row['id'];
        $ids = $row['id'];
    }
    
    
    if ($ids == 1) {
        echo "<option>$cname</option>"; // You forgot to specify which value in the array you want
    
    'try this way'
    
    foreach ($result as $row) {
        $cname = $row['id']; // Array
        $ids = $row['id']; // Array
    }
    
    if ($ids == 1) {
        echo "<option>$cname[1]</option>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题