Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.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 从ID Mysql查询内容_Php_Html_Mysql - Fatal编程技术网

Php 从ID Mysql查询内容

Php 从ID Mysql查询内容,php,html,mysql,Php,Html,Mysql,我有麻烦从一个ID显示的内容这里是一个例子,我在做什么 <?php $query = "SELECT * FROM home_text"; $select_home_text_query = mysqli_query( $connection, $query ); while ( $row = mysqli_fetch_assoc( $select_home_text_query ) ) { $home_content = $row[ '1' ]; ?> <div c

我有麻烦从一个ID显示的内容这里是一个例子,我在做什么

<?php
$query = "SELECT * FROM home_text";
$select_home_text_query = mysqli_query( $connection, $query );

while ( $row = mysqli_fetch_assoc( $select_home_text_query ) ) {
    $home_content = $row[ '1' ];
?>
<div class="row">
    <div class="col-md-12 separator">
        <p class="lead">
            <?php echo $home_content;?>
        </p>
    </div>
</div>
<?php
}
?>
这是来自数据库的图像

内容就是我想展示的

谢谢你的帮助

make

$home_content = $row[ '1' ];
排队

$home_content = $row['home_id'];
更改$home_内容=$row['1'];吨$home_内容=$row['home_id'];并改用mysqli_fetch_数组

将mysqli_fetch_assoc替换为mysqli_fetch_数组,这样您可以通过提及列数组来调用数据,第一列从0开始,第二列从1开始,依此类推

<?php
$query = "SELECT * FROM home_text";
$select_home_text_query = mysqli_query( $connection, $query );

while ( $row = mysqli_fetch_array( $select_home_text_query ) ) {
    $home_content = $row[0];
?>
<div class="row">
    <div class="col-md-12 separator">
        <p class="lead">
            <?php echo $home_content;?>
        </p>
    </div>
</div>
<?php
}
?>
mysqli_fetch_assoc:您将按列的名称调用,而不会忘记引号。例如:$row['home_id']

mysqli_fetch_数组:您将通过列位置调用。例如:$row[0]


mysqli_fetch_对象:您将其称为提及列名称的对象。例如:$row->home\u id

这里提供了一个更新版本的代码,如果不向查询传递限制1,则可以在需要时处理多行

在这里,您将有更多的控件,并可以格式化$out变量的输出

希望能有帮助

<?php
$query = "SELECT * FROM home_text";

$out = '';
$result = $mysql->query( $query );
if ($result->num_rows > 0){
    while($row = $result->fetch_assoc()){
     $id = $row['home_id];
     $content = $row['home_contents'];
     $names = $row['home_names'];
     //format how you want it on the html
     $out .= " $id | $names | $content";
    }
}

?>
<div class="row">
    <div class="col-md-12 separator">
        <p class="lead">
            <?php echo $out;?>
        </p>
    </div>
</div>


您在何处建立了与数据库的连接?由于您正在以关联数组的形式获取记录,因此您应该使用feild名称,如$row['home_id'],仅打印整行将不起作用。您需要单独调用字段,如echo$home_content[col name];OP使用mysqli_fetch_assoc获取行,因此$row应该是一个包含列名作为键的关联数组。因此,除非OP有一个指定名为0的列,否则应该抛出一个未定义的索引。为什么要使用mysqli\u fetch\u数组而不是mysqli\u fetch\u assoc?使用它会将行作为关联数组和索引数组返回,这在这里是不必要的。
<?php
$query = "SELECT * FROM home_text";

$out = '';
$result = $mysql->query( $query );
if ($result->num_rows > 0){
    while($row = $result->fetch_assoc()){
     $id = $row['home_id];
     $content = $row['home_contents'];
     $names = $row['home_names'];
     //format how you want it on the html
     $out .= " $id | $names | $content";
    }
}

?>
<div class="row">
    <div class="col-md-12 separator">
        <p class="lead">
            <?php echo $out;?>
        </p>
    </div>
</div>