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 以下代码,运行结果报错
  • ¥15 Vivado仿真数据出错
  • ¥15 银河麒麟不支持vant等前端框架怎么办?
  • ¥15 vue3的子组件在父页面调用不显示不生效问题
  • ¥15 cadence PEX
  • ¥15 phython创建了文件 显示在项目下面,但运行不了,运行时还是运行main文件,并且说main文件不允许并行运行
  • ¥20 C++哈希表的设计构造哈希表
  • ¥15 FutureWarning:不推荐使用空或全 NA 条目的 DataFrame 串联行为。怎么改呢?
  • ¥15 Chatgpt突然无法正常显示数学公式,如何解决?