Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何获取行然后显示在选项上_Php_Mysql - Fatal编程技术网

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

Php 如何获取行然后显示在选项上,php,mysql,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

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

现在的输出是什么…?$id变量在while循环中创建,并在if条件外部使用。它是在循环之前的某个地方声明的,因为$id的作用域应该在循环之后结束。请缩进代码也。嗨,谢谢你回答没有输出选项。我尝试过,但当商业名称没有值或为零时,将显示数据库中选项中的所有值。。你能告诉我怎么做吗?不要使用mysql函数。它们不受欢迎,不安全。我知道,我理解你只是在学习php。但是试着用一种好的方法来学习它——使用mysqli_uu函数,或者更好的PDO。
    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>";