繁体   English   中英

如何获取行然后显示在选项上

[英]How to fetch row then display on option

嗨,我有一个新手问题。 如果id == 1,则显示值为1的商业名称,后跟数据库中的所有值,否则,当id == 0时,显示所有值。

我尝试了这个,但是没有用,请帮助我

              $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>";
    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>";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM