使用mysqli fetch_assoc获取数据?

时间:2016-10-26 09:22:00

标签: php mysql mysqli

我在使用MySQLi从数据库中获取某些数据时遇到了一些问题,我不确定我做错了什么,已经尝试过几种不同的方式来调用表中的内容似乎什么也没做。

自从我编写了php之后已经有一段时间了,我仍然试图了解MySQli的变化。

这是我运行代码时得到的结果 -

Connected successfully
Could not successfully run query (SELECT * FROM articles) from DB:

这里是代码 -

<?php 
require_once('config.php');
//global $config;
$security_check = 1;

if(!isset($security_check))
{
    echo "This is restricted directory";
    exit();
}

function viewarticles()
{
    global $config;

    // Create connection
    $conn = new mysqli($config['hostname'], $config['username'], $config['password']);

    // Check connection
    if ($conn->connect_error) 
    {
        die("Connection failed: " . $conn->connect_error);
    } 
    else 
    {
        echo "Connected successfully";
        echo "<br/>";

    }

    $sql = "SELECT * FROM articles";
    $result = $conn->query($sql, MYSQLI_STORE_RESULT);


    if (!$result) {
        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
        exit;
    }

    if (mysql_num_rows($result) == 0) {
        echo "No rows found, nothing to print so am exiting";
        exit;
    }

    // While a row of data exists, put that row in $row as an associative array
    // Note: If you're expecting just one row, no need to use a loop
    // Note: If you put extract($row); inside the following loop, you'll
    //       then create $userid, $fullname, and $userstatus
    while($row = $result->fetch_assoc())
    {
        echo $result->title;
    }

    $result->free();

    $conn->close();



}    

viewarticles();

?>

0 个答案:

没有答案