i have issue with my database mySQL select from table

Dears

please help me in this matter

i have issue with my database mySQL
it is working with me fine with inserting items to the table but in selecting and fetching not working.

[php]
$sql2 = 'SELECT * FROM users';
$result2 = $conn->query($sql2);
echo $result2->num_rows;
if ($result2->num_rows == 0) {
// output data of each row
while($row = $result2->fetch_assoc()) {
echo "id: " . $row[“id”]. " - Name: " . $row[“firstname”]. " " . $row[“lastname”]. “
”;
}
} else {
echo “0 results”;
}[/php]

always give me 0 result and i am sure there is content in that table ( users )

please help me guys I tried every thing I almost give up.

thanks,

sorry this is the code

[php]$sql2 = 'SELECT * FROM users';
$result2 = $conn->query($sql2);
echo $result2->num_rows;
if ($result2->num_rows > 0) {
// output data of each row
while($row = $result2->fetch_assoc()) {
echo "id: " . $row[“id”]. " - Name: " . $row[“firstname”]. " " . $row[“lastname”]. “
”;
}
} else {
echo “0 results”;
}[/php]

hi

this code works fine with mysqli driver:
[php]<?php
$conn = new mysqli(‘localhost’, ‘test’, ‘test’, ‘test’);

$sql2 = 'SELECT * FROM users';
$result2 = $conn->query($sql2);

echo $result2->num_rows;

if ($result2->num_rows > 0) {
// output data of each row
while ($row = $result2->fetch_assoc()) {
echo "id: " . $row[“id”] . " - Name: " . $row[“firstname”] . " " . $row[“lastname”] . “
”;
}
} else {
echo “0 results”;
}[/php]

but if you want to use PDO you have to rewrite the code like this:
[php]<?php
$conn = new PDO(‘mysql:dbname=test;’, ‘test’, ‘test’, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

$sql2 = 'SELECT * FROM users';
$result2 = $conn->query($sql2);

echo $result2->rowCount();

if ($result2->rowCount() > 0) {
// output data of each row
while ($row = $result2->fetch(PDO::FETCH_ASSOC)) {
echo "id: " . $row[“id”] . " - Name: " . $row[“firstname”] . " " . $row[“lastname”] . “
”;
}
} else {
echo “0 results”;
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service