如何获取行然后显示选项

时间:2014-07-10 06:41:46

标签: php mysql

嗨,我有一个新手问题。 如果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>";

1 个答案:

答案 0 :(得分:0)

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