$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];
}
Related
I created the code using mysqli_fetch_assoc with 'while' as shown below.
But it does not work.
if ($ result = mysqli_query ($ dbconn, $ query)) {
It works by here.
while ($ row = mysqli_fetch_assoc ($ result)) {
It does not work from here.
I can not find the wrong part.
If I do not use 'while', it works as follows.
What is the problem?
// not works
$query = "select * from member where f_status='1'";
if ($result=mysqli_query($dbconn, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
if ($row[f_status]==0) {
error("No data");
} else {
echo $row[f_user_id];
echo $row[f_user_name];
}
}
mysqli_free_result($result);
}
// works
$query = "select * from member where f_status='1'";
$result = mysqli_query($dbconn, $query);
$row = mysqli_fetch_assoc($result);
if ($row) {
echo $row[f_user_id];
echo $row[f_user_name];
} else {
error("No data");
}
I think the problem is the way you have iterated while loop and condition to show No data message,
You should try this way:
$query = "select * from member where f_status='1'";
if ($result = mysqli_query($dbconn, $query)) {
if ($result->num_rows) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row[f_user_id];
echo $row[f_user_name];
}
}
else {
error("No data");
}
mysqli_free_result($result);
}
Hope this should solve your issue.
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']);
}
}
}
if ($result = $db->query("SELECT * FROM tab WHERE ID = $id")) {
while($row = $result->fetch_assoc() ){
echo "ID:". $row['ID'];
echo "Product:" .$row['Product'];
... }
$result->close();
} else {
echo "could not retrieve data from db"; }
I only get one result, but it should be a lot more.
How do I get all the results?
btw I cannot use fetch_all.
You are using an ID in your query, that means that you are asking for a specific item. In order to select all you have to remove this condition.
Your code would look like:
if ($result = $db->query("SELECT * FROM tab")) {
while($row = $result->fetch_assoc() ){
echo "ID:". $row['ID'];
echo "Product:" .$row['Product'];
}
$result->close();
} else {
echo "There is no data in the database";
}
Replace
$result = $db->query("SELECT * FROM tab WHERE ID = $id"
with
$result = $db->query("SELECT * FROM tab WHERE ID = $id LIMIT 1"
You must select both id and product from tab and also add a where statement.
Your code would look like:
if ($result = $db->query("SELECT ID, Product, user FROM tab WHERE user='".$username."' ")) { //$username = john;
while($row = $result->fetch_assoc() ){
echo "ID:". $row['ID'];
echo "Product:" .$row['Product'];
}
} else {
echo "There is no data in the database";
} result->close();
Let me hope your equate value is the name john it may be anything. so you will get all result rows from the database where the word john is listed.
put $username = 'john'; before the if statement.
The process of fetching data from a MYSQL result goes like this:
Connect to the host
Select the DB
Run the query
if ($result) //Check if the return is true
while($row = mysqli_fetch_array($result)) {
// do something with the $row
//like print it
}
}
In order to make fully sure print the query using
echo $your_query
Then run that query through phpMyAdmin and see what results or error you get.
Is there a quick way to filter a mysql_query result to get a list containing only values of a specific column?
$query = mysql_query("SELECT * FROM users");
$list_of_user_names = get_values($query,"names");
What is the name of the function to be used in place of get_values?
Assuming your field name in databse is "names"
$query = mysql_query("SELECT names FROM users");
while($row = mysql_fetch_assoc($query)){
echo $row['names'];
echo "<br>";
}
NOTE : mysql_* functions are deprecated in new version of php, use mysqli_* or PDO
Use below function.
function get_values($q,$c)
{
$arr = array();
while($row = mysql_fetch_array($q))
{
$arr[] = $row[$c];
}
return $arr; // return all names value.
}
Try this:
$query = mysql_query("SELECT names FROM users");
if (!$query) {
echo "Could not successfully run query from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($query) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $names
while ($row = mysql_fetch_assoc($query)) {
echo $row["names"];
}
Got a issue where if the rows are equal to 0 the else statement is not called. if i type in the correct details on the site, the first condition is met and the xml is displayed, however if the incorrect details are entered, the error xml is not displayed.
echo "<users>";
$result = mysql_query("SELECT * FROM `numbers` WHERE `email` = '".$email."' AND `password` = '".$password."'") or die('Invalid query: ' . mysql_error());
if (!$result) die('Invalid query: ' . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
if(mysql_num_rows($result) != 0)
{
echo "<usercallback>";
echo "<id>".$row['id']."</id>";
//echo "<number>".$row2['number']."</number>";
//echo "<gender>".$row2['gender']."</gender>";
//echo "<relationship>".$row2['relationship']."</relationship>";
echo "</usercallback>";
}
else
{
echo "<usercallback>";
echo "<id>error</id>";
echo "</usercallback>";
}
}
echo "</users>";
actually you have to put the if mysql_num_rows check outside the while block
echo "<users>";
$result = mysql_query("SELECT * FROM `numbers` WHERE `email` = '".$email."' AND `password` = '".$password."'") or die('Invalid query: ' . mysql_error());
if(mysql_num_rows($result) != 0)
{
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<usercallback>";
echo "<id>".$row['id']."</id>";
//echo "<number>".$row2['number']."</number>";
//echo "<gender>".$row2['gender']."</gender>";
//echo "<relationship>".$row2['relationship']."</relationship>";
echo "</usercallback>";
}
}
else
{
echo "<usercallback>";
echo "<id>error</id>";
echo "</usercallback>";
}
echo "</users>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
If there are no rows then $row will be false and it will never enter the above while loop.
So you can not get error message
You check for rows while performing the loop, you should check for the number of rows BEFORE the while loop.
It looks like you're expecting a zero-row response from MySQL to result in an undef $result. That's not what happens. mysql_query() returns null if there was an error. You can test the "truth" of its return value to see if the query ran correctly. If the query results in 0 rows of response, it ran correctly. The value to test, then is mysql_num_rows($result).
However, it can be done simpler. I usually write this like:
$res = mysql_query("SELECT 1");
if (!$res) {
//do whatever error reporting if the SQL was bad
//though you should probably deal with any condition that ends up
//here before you go into production!
}
if (mysql_num_rows($res) == 0) {
//put out my "no results found" message
}
while ($row = mysql_fetch_assoc($res)) {
//do my per-row business
}
mysql_fetch_assoc() returns false when there are no (or no more) results to return. So if it's empty, that while loop will never run, so it doesn't need to be inside an else.