简体   繁体   中英

Selecting data from mysql php

I'm using this to select data from mysql in php

<?php
error_reporting(E_ALL);
$servername = "localhost";
$username = "******";
$password = "******";
$database = "*******";

// Create connection
$conn2 = new mysqli($servername, $username, $password, $database);
$stmt = $conn2->prepare('SELECT id, title, views from `blog_main`');

$stmt->execute();
var_dump($stmt);

$result = $stmt->get_result();
 while($r=$result->fetch_assoc())
{
        echo "id: " . $r["id"]. " - Name: " . $r["title"]. " " . $r["views"]. "<br>";
}

$conn->close();
?>

However no result is shown, but if I run this query on phpmyadmin interface it returns rows. This code worked fine on localhost but when I shifted it to godaddy's server, I'm facing this problem only in prepared statement. When I'm using normal query method it displays rows, only prepared statements are not working.

This is the value of var dump:

object(mysqli_stmt)#2 (10) { ["affected_rows"]=> int(-1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(0) ["field_count"]=> int(3) ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["sqlstate"]=> string(5) "00000" ["id"]=> int(1) }

Is there any kind of extension I need to enable or what might be the problem?

here is my answer for your question,

create file db.php

$dbInfo = array(
    'host'      => "localhost",
    'user'      => "******",
    'pass'      => "******",
    'dbname'    => "******"
);

create dbcon.php

class database{
    protected $databaseLink;
    function __construct(){
        include "db.php";
        $this->database = $dbInfo['host'];
        $this->mysql_user = $dbInfo['user'];
        $this->mysql_pass = $dbInfo['pass'];
        $this->databaseName = $dbInfo['dbname'];
        $this->openConnection();
        return $this->get_link();
    }
    function openConnection(){
    $this->databaseLink = mysqli_connect($this->database, $this->mysql_user, $this->mysql_pass, $this->databaseName);
    }

    function get_link(){
    return $this->databaseLink;
    }
}

create func.php

include "dbcon.php";

function selectData(){
        $db = new database();
        $selectQuery = "SELECT id, title, views FROM blog_main";
        $selectQuery = mysqli_query($db->get_link(), $selectQuery) or die(mysqli_error());
        while($r = mysqli_fetch_assoc($selectQuery)) {
            $rows[] = $r;
        }
        return $result = json_encode(($rows));
        mysqli_close($db->get_link());
     }

and in for example index.php you can get data from which SQL return

include "func.php";
$data = selectData();


$dataJson = json_decode($data, TRUE);

foreach ($dataJson as $key => $value) {
    echo "id: " . $value['id']. " - Name: " . $value['title']. " " . $value['views']. "<br>";
}

good luck

Seems like it was the disable driver issue. I enabled the stmt_mysqli driver and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM