This question already has an answer here:
I am trying to select data from a MySQL table to show data's in my c# datagridview, but I get one of the following error messages:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /var/www/html/data.php on line 15
This is my PHP code:
<?php
$servername = "********";
$username = "root";
$password = "pws";
$dbname = "dbs";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT userid,password,first_name,last_name,role,active FROM aster_users";
$result = mysql_query($sql);
while($row = mysqli_fetch_array($result))
{
$userid=$row['userid'];
$password=$row['password'];
$first_name=$row['first_name'];
$last_name=$row['last_name'];
$role=$row['role'];
$active=$row['active'];
}
$conn->close();
?>
Help me,
</div>
You cannot mix the mysql and mysqli. For database queries please use mysqli_query($conn,$query)
. http://php.net/manual/en/mysqli.query.php
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT userid,password,first_name,last_name,role,active FROM aster_users";
$result = mysqli_query($conn,$sql); // NOTICE HOW THIS IS MYSQLI QUERY. NOT MYSQL
while($row = mysqli_fetch_array($result))
{
$userid=$row['userid'];
$password=$row['password'];
$first_name=$row['first_name'];
$last_name=$row['last_name'];
$role=$row['role'];
$active=$row['active'];
}
mysqli_close($conn); //Use Procedural Code Here. not $conn->close();
Mixing mysql and mysqli
in your code !! and Object oriented style
with Procedural style
Use
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT userid,password,first_name,last_name,role,active FROM aster_users";
if (!$result = $conn->query($sql)) {
printf("Error: %s
", $conn->error);
}
$rows = $result->num_rows;
if ($rows > 0) {
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
// your code
}
} else {
echo "No result found";
}
$sql = "SELECT userid,password,first_name,last_name,role,active FROM aster_users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$userid=$row['userid'];
$password=$row['password'];
$first_name=$row['first_name'];
$last_name=$row['last_name'];
$role=$row['role'];
$active=$row['active'];
}
} else {
echo "0 results";
}
$conn->close();
Please don't mix mysql_*
& mysqli_*object oried
style in php.