This question already has an answer here:
- mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource or result 32 answers
First, sorry for my English.
Hi, I'm quite new to PHP, and I'm learn it in OOP. I'm doing a work project now, which use PHP.
I have created 3 PHP files, and I want to see the output from a db query.
The files are db_connect.php
, login.php
, and query.php
.
The query.php
is just for storing the query, so that if I want to write a query, I just call a function inside query.php
. But when I run it, it gives me warning:
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in ../query.php
I have tried to fix it, but it still gives me an error object given
or null given
beside this boolean given
.
Any ideas what I'm doing wrong and suggestion what I suppose to do?
By the way, this is my file :
db_connect.php
<?php
class sql_con {
public $conn;
public function __construct()
{
$this_com = "localhost";
$user = "root";
$pass = "root";
$db = "db";
try {
$this->conn = mysqli_connect($this_com,$user,$pass,$db);
if (mysqli_connect_error())
{
die("koneksi eror : ".mysqli_connect_errno()." = ".mysqli_connect_error());
// exit();
}
}
catch(Exception $e) {
echo $e . mysqli_connect_error();
}
}
}
?>
login.php
<?php
require_once('db_connect.php');
require_once("query.php");
$str_query = "select * from ";
$table = "table";
$condition =" where user=$user and pass=$pass";
$conn = new db_connect();
$query = new query();
$result = $query->query_sql($conn->conn,$str_query,$condition,$table);
$result = mysqli_fetch_assoc($query);
echo $result;
$sql_c = new sql_con();
mysqli_close($sql_c->conn);
query.php
<?php
require_once("db_connect.php");
class query {
var $query;
var $conn;
public function query_sql($conn, $string_query, $condition, $table)
{
$conn =new sql_con();
$query = mysqli_query($conn,$string_query.$table.$condition);
return $query;
}
}