I have this piece of code in a function of mine that echos "Empty" if it cannot grab the data stored in the variable $result. The "Empty" part works fine. However, it will not echo the users data if the conditional enters the else part, why?
$result = mysql_query("SELECT * FROM `users` ORDER BY `earnings` DESC LIMIT ".$load.",1") or die (mysql_error());
if(!mysql_fetch_row($result)) {
echo "Empty";
} else {
while($data = mysql_fetch_array($result))
{
if($name == true) {
echo ucfirst($data['user']);
} else {
echo ucfirst($data['earnings']);
}
}
}
By calling mysql_fetch_row($result) as a test, you are missing the first row of your data always. Check if number of rows is 0 instead.
HI I think you issue is in mysql_fetch_array(). This function will return array not associative array..
you can use
$result = mysql_query("SELECT * FROM `users` ORDER BY `earnings` DESC LIMIT ".$load.",1") or die (mysql_error());
if(mysql_num_rows($result) == 0) {
echo "Empty";
} else {
while($data = mysql_fetch_array($result,MYSQL_BOTH))
{
if($name == true) {
echo ucfirst($data['user']);
} else {
echo ucfirst($data['earnings']);
}
}
}
Use mysql_num_rows intead of mysql_fetch_row to check empty recordset.
I pass the 2nd parameter in mysql_fetch_array which MYSQL_BOTH that will return both array and associative array..
Or you can use
mysql_fetch_assoc() function istead of mysql_fetch_array()..
Feel free to ask question..
try this
$result = mysql_query("SELECT * FROM `users` ORDER BY `earnings` DESC LIMIT ".$load.",1") or die (mysql_error());
if(!$result) {
echo "Empty";
} else {
while($data = mysql_fetch_array($result))
{
if($name == true) {
echo ucfirst($data['user']);
} else {
echo ucfirst($data['earnings']);
}
}
}
Related
$sql = mysqli_query($db, "SELECT COUNT(ID) from bruker");
if (!$sql) {
while ($row1 = mysqli_fetch_array($sql, MYSQL_NUM)); {
echo $row1;
}
} else {
echo "Funker ikke";
}
I cant get it to work, all I get is the: else "Funker ikke".
1) Using wrong if condition. If query return true then insert into if condition
2) Wrong echo data using MYSQL_NUM. It return numeric array
$sql = mysqli_query($db, "SELECT COUNT(ID) from bruker");
if ($sql) {// if true
while ($row1 = mysqli_fetch_array($sql, MYSQL_NUM)) {
echo $row1['0'];//numeric array
}
} else {
echo "Funker ikke";
}
UPDATED as per below comments
$sql = mysqli_query($db, "SELECT COUNT(ID) from bruker");
if ($sql) {
$row1 = mysqli_fetch_array($sql, MYSQL_NUM);
echo $row1['0']; //numeric array
} else {
echo "Funker ikke";
}
Read http://php.net/manual/en/mysqli-result.fetch-array.php
Your code is correct except the not sign before sql in if condition.According to you if your sql query is wrong then the $row1 is displayed else if it is correct then "funker ikke" gets printed.Just the opposite. This code works:
$sql = mysqli_query($db, "SELECT COUNT(ID) from bruker");
if ($sql) {
// as the query can only return 1 row the while look is unnecessary
//while ($row1 = mysqli_fetch_array($sql, MYSQL_NUM)) {
$row = mysqli_fetch_array($sql, MYSQL_NUM)
echo $row[0];
} else {
echo "Funker ikke";
}
Your code are having two error there.
First, it should be true condition only can get into the display loop. But you put the condition as !$sql means false only can get into the loop. So, you should change !$sql to $sql. Besides, after the While loop should not direct put the semicolon. You try see the following code
$sql = mysqli_query($db, "SELECT COUNT(ID) from bruker");
if ($sql) {
while ($row1 = mysqli_fetch_array($sql, MYSQL_NUM))
{
echo $row1[0];
}
}
else {
echo "Funker ikke";
}
There are a few errors in your code
First you do not need a while loop to process a single result, SELECT COUNT() can only return one row.
Second as mysqli_query() for a SELECT query will return either FALSE or a mysqli_result object it would be better to flip your if condition as below and test specifically for FALSE rather than TRUE.
Third, a mysqli_fetch_array($sql, MYSQL_NUM) returns an array, you have to use array notation to get the only returned value i.e. $row[0]
Also when any mysqli_ function fails it leaves some error information that is very useful, so show that info or send it to an application error file if you dont want users to see them when the code goes live.
$sql = mysqli_query($db, "SELECT COUNT(ID) from bruker");
if ($sql === FALSE) {
echo "Funker ikke";
echo mysqli_error($db);
exit;
} else {
$row = mysqli_fetch_array($sql, MYSQL_NUM);
echo $row[0];
}
I have read several of these posts on the site, but still can't find the answer to my problems. I have a while loop where for every entry in the database the table populates. Although if the table in the database is empty, I want it to display a messaged instead. Any ideas of what is wrong here? (aware of the deprecated tags)
$result = mysql_query("SELECT * FROM blog");
while($row = mysql_fetch_array($result))
{
if(count($row) === 0)
{
echo 'No Data';
}
<table code>
}
Use mysql_num_rows for counting rows from DB.
<?php
if (mysql_num_rows($result) > 0) {
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>...</tr>';
}
echo '</table>';
} else {
echo 'No result found';
}
?>
EDIT: updated code for table.
$result = mysql_query("SELECT * FROM blog");
if(mysql_num_rows($result) === 0) {
echo 'No Data';
} else {
while($row = mysql_fetch_array($result)) {
// code
}
}
use mysql_num_rows() to get count of query result rows
$rows = mysql_num_rows($result);
if($rows > 0) {
// do your stuff
}
Use mysql_num_rows for counting number of rows are returned in query.
Try this. mysql_num_rows will check the records in database. On true condition it will allow to execute the while loop other wise else condition will execute.
$result = mysql_query("SELECT * FROM blog");
if(mysql_num_rows($result) > 0) {
echo 'No Result Found';
} else {
while($row = mysql_fetch_array($result)) {
// Here your Data
}
}
If you want to display when no record found then
$result = mysql_query("SELECT * FROM blog");
while($row = mysql_fetch_array($result))
{
if(count($row) == 0 || count($row) < 1)
{
echo 'No Data';
}
else
{
//print records
}
}
<?php
$soil_ph = $_POST['soilph'];
$query = "select ph_id,ph_name,ph_from,ph_to from tbl_soilph
where '$soil_ph' between ph_from and ph_to";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
if ($row == 0)
{
echo 'Invalid or out of range';
}
else
{
$ph = $row['ph_name'];
echo $row['ph_name'];
}
}
?>
---echo not working # ($row=0)--- can someone help me?
the code above works fine it gives result, but when there's no data it doesn't show the message "invalid input"?
First of all, you should always escape your variables before using them in database queries (unless you use prepared statements, which you should):
$soil_ph = $_POST['soilph'];
$query = "SELECT ph_id, ph_name, ph_from, ph_to
FROM tbl_soilph
WHERE '" . mysql_real_escape_string($soil_ph) . "' BETWEEN ph_from AND ph_to";
$result = mysql_query($query);
To inspect whether you have any results, you should use mysql_num_rows() after making sure the query didn't fail:
if ($result && mysql_num_rows($result)) {
while ($row = mysql_fetch_array($result)) {
// do your stuff
}
} else {
// aww, nothing there
}
Further reading: MySQLi, PDO
$soil_ph = $_POST['soilph'];
$query = "select ph_id,ph_name,ph_from,ph_to from tbl_soilph
where '$soil_ph' between ph_from and ph_to";
$result = mysql_query($query);
if($result && mysql_num_rows($result)) {
while() {
}
} else {
echo 'invalid input';
}
Check this code , you have change the way you loop
if (mysql_num_rows($result) < 1) {
echo 'Invalid or out of range';
}else{
while($row = mysql_fetch_array($result)){
$ph = $row['ph_name'];
echo $row['ph_name'];
}
}
Actually mysql_fetch_array function return a single dimension array at each time execute. You can do like this:
if($results){
echo 'No Results';
}else{
while($row = mysql_fetch_array($result))
{
if(!empty($row)){
echo $row['ph_name'];
}else{
echo 'invalid';
}
}
What i try to do is check the user's Id by verifying in a cookie (called "identification").
thats my code:
if(!$noCookie) {
$sql = "SELECT * FROM `". TABLE_PREFIX ."_logged` WHERE `logged_cookie` = '". $usersCookie ."' LIMIT 1";
$query = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($query) <= 0) {
echo 'yes cookie';
$row = mysql_fetch_array($query) or die(mysql_error());
print_r($row);
while ($row=mysql_fetch_array($query)) {
echo $usersId = $row["logged_user_id"];
}
} else {
echo 'no cookie';
}
}
What happends is that if the user's cookie is the same one like in the table,
it will return the user's id.
so thats what im trying to do, the IF says "yes cookie", but inside the while it doesnt do anything!
and its not giving me an error.
Since there are no rows, there's nothing to be fetched.
if (mysql_num_rows($query) <= 0) {
Should this be > 0?
You only ran mysql_fetch_array when mysql_num_rows($query) evaluated to 0 or less; i.e. there are no rows.
Perhaps you meant:
if (mysql_num_rows($query) > 0) { // <-----
echo 'yes cookie';
$row = mysql_fetch_array($query) or die(mysql_error());
// ...
}
else {
echo 'no cookie';
}
Also note that you're running mysql_fetch_array in two different places, which is normally not right. Your while loop will not include the first result row, because you already did mysql_fetch_array once before the loop.
if(!$noCookie) {
$sql = "SELECT * FROM `". TABLE_PREFIX ."_logged` WHERE `logged_cookie` = '". $usersCookie ."' LIMIT 1";
$query = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($query) > 0) {
echo 'yes cookie';
$row = mysql_fetch_array($query) or die(mysql_error());
foreach ($row as $r)
echo $r["logged_user_id"];
} else {
echo 'no cookie';
}
}
My code looks like this:
$result = mysql_query("SELECT * FROM `picdb` WHERE `picid` = '$picid' ") or trigger_error(mysql_error());
while($row = mysql_fetch_array($result)){
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); }
Problem is that when there is no row to select, nothing happens, my $row array is empty.
How can i have this present a message of "no row found" when no rows match the select?
try
if (mysql_num_rows($result) == 0) {
// Show message
} else {
// do your while stuff here
}
Use mysql_num_rows() to determine the amount of rows in your result.
$result = mysql_query("SELECT * FROM `picdb` WHERE `picid` = '$picid' ") or trigger_error(mysql_error());
if(mysql_num_rows($result) == 0) {
echo "No row found!"
} else {
while($row = mysql_fetch_array($result)) {
foreach($row AS $key => $value) {
$row[$key] = stripslashes($value);
}
}
}
if (mysql_num_rows($result) == 0) {
echo "Result is empty!";
}
You should've read the description of mysql_query, it gives you the answer: http://php.net/mysql_query
mysql_num_rows() only works on buffered queries. If you have potentially a large/huge result set you might switch to unbuffered queries and num_rows is not an option anymore.
You can test the return value of the first call to mysql_fetch_array(). If it's FALSE there were no matching records.
<?php
$result = mysql_query("SELECT * FROM `picdb` WHERE `picid` = '$picid' ") or trigger_error(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if ( !$row ) {
echo '0 rows';
}
else {
do {
echo htmlspecialchars($row['x']), "<br />\n";
} while($row=mysql_fetch_array($result, MYSQL_ASSOC));
}
The downside of this do { } while() is that the $row=mysql_fetch... code has been duplicated. But it's only a small doublet.
This method never seems to fail for me.
if(!$result = mysqli_query($con,"SELECT * FROM `picdb` WHERE `picid` = '$picid' ")){
// Enter you error message here.
} else {
// Do all your magic.
}