Why Is My Mysql_query Returning No Result?
Solution 1:
You're mixing mysqli with PDO exceptions try/catch, plus there's an extra bracket in your try block. Those two APIs do not mix.
See the comments in your code: (and the fixed code below that)
Sidenotes: You may want to change foreach(mysqli_fetch_array($result) as $row) to while ($row = mysqli_fetch_assoc($result))
Using foreach(mysqli_fetch_array($result) as $row) will produce repeated results of the rows and only the first letter from each row.
try {
// Setting the query and runnin it...$result = mysqli_query($connection,"SELECT * FROM table");
if(mysqli_num_rows($result)>0)
){
// ^-- one bracket too manyforeach(mysqli_fetch_array($result) as$row) {
echo$row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';
}
}else{
echo"QUERY IS NOT OKAY";
} // Closing the connection.$connection = null;
}catch(PDOException $e) { // this should be catch (Exception $e)echo$e->getMessage();
}
Change it to this:
try {
// Setting the query and runnin it...$result = mysqli_query($connection,"SELECT * FROM `table`");
if(mysqli_num_rows($result)>0)
{
foreach(mysqli_fetch_array($result) as$row) {
echo$row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';
}
}else{
echo"QUERY IS NOT OKAY";
}
}
catch (Exception$e)
{
echo$e->getMessage();
}
Add error reporting at the top of your file(s)
error_reporting(E_ALL);
ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
Rewrite:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$connection=mysqli_connect("localhost"/*hostname*/,
"username"/*username*/,
"password"/*password*/,
"dbname"/*database name*/);
try {
// Setting the query and runnin it...$result = mysqli_query($connection,"SELECT * FROM `table`");
if(mysqli_num_rows($result)>0)
{
// use while instead of foreach// while ($row = mysqli_fetch_assoc($result)){foreach(mysqli_fetch_array($result) as$row) {
echo$row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';
}
}else{
echo"QUERY IS NOT OKAY";
}
}
catch (Exception$e)
{
echo$e->getMessage();
}
Solution 2:
instead of
$result = mysql_query($connection,"SELECT * FROM table");
use
$result = mysqli_query($connection,"SELECT * FROM table");
instead of
mysql_fetch_array($result)
use
mysqli_fetch_array($result)
Solution 3:
I suppose you should be using mysqli_query along with mysqli_connect and not mysql_query
Solution 4:
Try this out
<?php$mysqli = new mysqli("localhost", "username", "password", "Database name");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// Setting the query and runnin it...$result = $mysqli->prepare("SELECT * FROM table");
/* execute query */$result->execute();
$result = $mysqli->get_result();
while ($row= $result->fetch_assoc())
{
echo$row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';
}
?>Solution 5:
Most probably, something is wrong with your connection. As mysqli* don't throw exceptions, you can't catch them.
Those functions return valid objects or FALSE, 0 or null on error. So you need to check for that.
Here is the code, that should tell you what's wrong with your communication with the DB.
<?phpfunctionctest() {
$connection=mysqli_connect("localhost"/*hostname*/,
"username"/*username*/,
"password"/*password*/,
"dbname"/*database name*/);
if($connection) {
$result = mysqli_query($connection,"SELECT * FROM `table`");
if($result) {
if(mysqli_num_rows($result)>0) {
foreach(mysqli_fetch_array($result) as$row) {
echo$row['rowa']. ' - '. $row['rowb']. ' - '
. $row['rowc']. ' - '
. $row['rowd']. ' - '
. $row['rowe'].'<br />';
}
return;
}
echo"0 rows";
return;
}
echo"No result";
return;
}
echo"No connection";
$connection = null;
}
?>
Post a Comment for "Why Is My Mysql_query Returning No Result?"