php - 从一列中提取数据mysql DB不确定我在这里做错了什么?

时间:2015-09-15 17:35:37

标签: php html5 mysqli

php:从一列mysql DB中提取数据不确定我在这里做错了什么?

<tr data-id="4" data-parent="1">
    <td> Batch Number *</td>
    <td>Last Batch Built:
    <!-- Trying to grab last batch number from database -->
    <?php 
         $mysqli = new mysqli('localhost', 'root', '***', 
                              'abes_rocking_db');
         echo array_values(mysqli_fetch_array(
             $mysqli->query(
                 "SELECT batch_number FROM abes_table 
                 ORDER BY ID DESC limit 1")
             )
         )[0];          
    ?>
    </td>

1 个答案:

答案 0 :(得分:-1)

    <tr data-id="4" data-parent="1"><td> Batch Number *</td>
        <td>Last Batch Built:
        <!-- Grabs last batch number from database -->
        <?php 
        /* Establish Connection to Database Using PDO */
        /* ========================================== */
        $username= 'root';
        $password= 'lutefisk';
        $hostname='localhost';
        $password='**********';
        try 
        {
            $conn = new PDO("mysql:host=$hostname;dbname=abes_rocking_db",$username,$password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
            /* Sql statement that extracts data from database */
            /* ========================================== */
            $sql = "SELECT batch_number FROM abes_table ORDER BY idqa_1001 DESC LIMIT 1";
            foreach ($conn->query($sql) as $row)
            {
                /* prints data to form */
                /* =================== */
                echo "<strong style='color:#fff;'><em>" . $row["batch_number"] . "</em>" . "<br/>";
            }
            $conn = null;
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
        ?>
        </td>
        <td colspan="4">
            <input type="text" class="form-control" name="batch_number" id="batch_number">
        </td>
    </tr>