PHP没有显示MySQLi查询结果

时间:2017-09-19 06:39:57

标签: php mysql

我是PHP和MySQL的新手,我做了一些类似于手册和指南的内容,但我无法获取结果。当我在浏览器中运行它时,标题显示在页面上,而不是数据库内容。这是我的代码:

<?php

$hostname='localhost';
$username='root';
$password='12345678';

$db=mysqli_connect($hostame, $username, $password);
$selectdb=mysqli_select_db('journal', $db);

$result=mysqli_query("SELECT id,nn,npd,date FROM journal ORDER by id ASC"); ?>

<table border="1">

<thead>
    <tr>
        <th rowspan="2">Header 1</th> <!-- id -->
        <th rowspan="2">Header 2</th> <!-- nn -->
        <th rowspan="2">Header 3</th> <!-- npd -->
        <th rowspan="2">Header 4</th> <!-- date -->
    </tr>
</thead>

<tbody>
    <?php while($array=mysqli_fetch_assoc($result))
    { ?>
        <tr>
            <td><?php echo $array['id']; ?></td>
            <td><?php echo $array['nn']; ?></td>
            <td><?php echo $array['npd']; ?></td>
            <td><?php echo $array['date']; ?></td>
        </tr>
    <?php } ?>
</tbody> 
</table>
<?php mysqli_close($db); ?>

3 个答案:

答案 0 :(得分:0)

您没有将连接参数传递给错误的mysqli_query。

$db = mysqli_connect($hostname, $username, $password, $database_name);

$sql = "SELECT id,nn,npd,date FROM journal ORDER by id ASC";
$result = mysqli_query($db, $sql);

答案 1 :(得分:0)

将第一个参数添加到$result=mysqli_query($con,$sql_query); 作为连接

onChangeTab

答案 2 :(得分:0)

尝试此代码,看看是否对您有所帮助     

$hostname='localhost';
$username='root';
$password='12345678';

$db=mysqli_connect($hostame, $username, $password,'journal');

$result=mysqli_query($db,"SELECT id,nn,npd,date FROM journal ORDER by id ASC"); 
?>

<table border="1">

<thead>
<tr>
    <th rowspan="2">Header 1</th> <!-- id -->
    <th rowspan="2">Header 2</th> <!-- nn -->
    <th rowspan="2">Header 3</th> <!-- npd -->
    <th rowspan="2">Header 4</th> <!-- date -->
</tr>
</thead>

<tbody>
<?php while($array=mysqli_fetch_assoc($result))
{ ?>
    <tr>
        <td><?php echo $array['id']; ?></td>
        <td><?php echo $array['nn']; ?></td>
        <td><?php echo $array['npd']; ?></td>
        <td><?php echo $array['date']; ?></td>
    </tr>
<?php } ?>
</tbody> 
</table>
<?php mysqli_close($db); ?>