dongtaoxue4674 2017-08-13 23:16
浏览 26
已采纳

Php到MySQL数据库连接

Im not sure whats going wrong with this code. I have a paragraph stored in the database under content, but for some reason I can't get it to display. Any help would be greatly appreciated, I'm a newer developer and welcome any feedback.

<?php
require_once('db_connection.php'); //connection credentials

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

$sql = "SELECT content FROM content";
$result = mysqli_query($conn, $sql);

while($row = mysqli_fetch_row($result)) { 
    echo $row;
}

mysqli_close($conn);
?>
  • 写回答

1条回答 默认 最新

  • dongmopu6734 2017-08-13 23:20
    关注

    echo $row; you're missing the column's object's array itself that you want to echo.

    What you want is echo $row[0];

    However, both column and table name are the same, so make sure that that is indeed correct.

    As per the manual:

    Example pulled from the manual:

    $query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
    
    if ($result = mysqli_query($link, $query)) {
    
        /* fetch associative array */
        while ($row = mysqli_fetch_row($result)) {
            printf ("%s (%s)
    ", $row[0], $row[1]);
        }
    
        /* free result set */
        mysqli_free_result($result);
    }
    

    If the above failed, then that could mean that your query may have failed and you need to check for errors on the query, using mysqli_error($conn).

    Reference:

    Same thing goes for the connection.

    Reference:

    Example from the manual:

    <?php
    $link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
    
    if (!$link) {
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
        exit;
    }
    
    echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
    echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
    
    mysqli_close($link);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python如何循环调取规律性名称的数据
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!
  • ¥20 win11账户锁定时间设为0无法登录
  • ¥45 C#学生成绩管理系统
  • ¥30 matlab appdesigner私有函数嵌套整合