nelzkiiee nelzkiiee - 5 months ago 49
SQL Question

Notice: Undefined variable: mysqli

I'm currently working on my simple Information System project. there's an error on my code and I've already search on web some answer but nothing works. The error says "Notice: Undefined variable: mysqli in C:\wamp\www\Members Information System\dependents_table.php on line 15"

Here is my code:

<?php
$member_query = $mysqli->query("select *from members order by member_id DESC")or die(mysql_error());
$member_row = $member_query->fetch_array();
$member_id = $member_row['member_id'];
$query = $mysqli->query("select * from dependents where member_id = '$member_id'") or die(mysql_error());
while ($row = $query->fetch_array()) {
$id = $row['dependent_id'];
?>


The line 15 is:

$member_query = $mysqli->query("select *from members order by member_id DESC")or die(mysql_error());


My database is:

<?php $conn = new PDO('mysql:host=localhost;dbname=nochp', 'root', ''); ?>


I hope you can help me. Thank you everyone.

Answer

You are using pdo for database connection but not using it to query string

Here's a simple example to differentiate between pdo and mysqli

// mysqli
$mysqli = new mysqli("example.com", "user", "password", "database");
$result = $mysqli->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);


// PDO
$pdo = new PDO('mysql:host=example.com;dbname=database', 'user', 'password');
$statement = $pdo->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo htmlentities($row['_message']);