I have the following code which should select a row from my DB with the current day's date.
$sql = "SELECT * FROM Forecast WHERE date(epoch) LIKE '". $mysqldate . "';";
echo $sql . "\n";
$result = mysqli_query($conn, $sql);
if (!$result || mysqli_num_rows($result) == 0) {
$row = mysqli_fetch_object($result);
$pop = 0;
$forecast = new DailyForecast($row->epoch, ...);
return $forecast;
}
else {
echo "no data found..\n";
}
Running SELECT * FROM Forecast WHERE date(epoch) LIKE '2015-11-03' in PHPmyAdmin works fine. In my script it return false or nothing...
Your condition looks like it's the wrong way round.
if (!$result || mysqli_num_rows($result) == 0) {
So if there's no result or no rows, you start executing code that seems to expect rows. However if there is a result or there are rows you echo 'no data found'
Try this instead:
if ($result && mysqli_num_rows($result) > 0) {
Related
I'm trying to count the rows which are not NULL inside a table when exucting the query inside Phpmyadmin it gives me the right output.
SELECT COUNT(`column_name`) FROM `Table_name`
but when I'm trying to execute it inside Php it always returns one I tried 2 methods both returning one for some reasons any ideas ?
method 1
$query = "SELECT COUNT(`column_name`) FROM `Table_name`";
if ($result = $mysqli->query($query)) {
$field1name = $rowcount=mysqli_num_rows($result);
echo '<tr>
<td>English</td>
<td>'.$field1name.'</td>
</tr>';
$result->free();
}
method 2
$query = "SELECT COUNT(`column_name`) FROM `Table_name`";
if ($result = $mysqli->query($query)) {
while ($rowcount = $result->fetch_assoc()) {
$field1name = $rowcount=mysqli_num_rows($result);
echo '<tr>
<td>Bahdini</td>
<td>'.$field1name.'</td>
</tr>';
}
$result->free();
}
The SELECT COUNT Query returns a resultset of 1 row, in that row you get the number of rows: 10228 as you stated.
the function mysqli_num_rows returns the number of rows in the RESULTSET, that's why it returns 1.
You have several assigment in a row .. which is your expected result ??
$field1name = $rowcount =mysqli_num_rows($result);
Instaed You should use a proper column alias for your count and then query, fecth, loop over the result and show
$query = "SELECT COUNT(`column_name`) my_count FROM `Table_name`";
$result = mysqli_query($conn, $query );
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row["my_count"]. "<br>";
}
} else {
echo "0 results";
}
I get result from first query correctly However, when I want to use them in second query in WHERE condition I get query error 1064. If I remove WHERE it will work fine. Also,when I try to echo variables inside while in second query code it will print.the variables will not work only in WHERE in second query
$queryDate = "SELECT date , time from DATES where ID = $ID";
$result = mysqli_query($connection, $queryDate);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$date = $row['date'];
$time = $row['time'];
}
}
$queryCom = "SELECT * from DATES, BOOKING where dates.time = $time and booking.IDofFullDate= $date";
$result1 = mysqli_query($connection, $queryCom);
if (!$result1)
{
die("Query Faile". mysqli_errno($connection));
}
if ($result1->num_rows >0) {
echo $date;
echo $time;
}
on your second query try this below:
$queryCom = "SELECT * from DATES as dates, BOOKING as booking where dates.time = $time and booking.IDofFullDate= $date";
My site is successfully inserting date from values the user has entered, however, when it comes to getting data from the database I have a problem.
Here's my code:
$sql = "SELECT cost FROM settings LIMIT 1";
if ($conn->query($sql) === TRUE)
{
$cost = $sql;
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
I'm just getting an error (Error: SELECT cost FROM settings LIMIT 1) and I'm unsure how to identify the problem. Everything looks correct from my point of view, obviously it's not.
Try this code. This code works only if you have properly connected to DB.
$sql = "SELECT `cost` FROM `settings` LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["cost"];
}
} else {
echo "0 results";
}
Try using backtics,
$sql = "SELECT `cost` FROM `settings` LIMIT 1";
And also,
if ($sql->num_rows > 0) {
.....
Actually the error is in.
if ($conn->query($sql) === TRUE)
{
}
$conn->query($sql) will not return TRUE for SELECT QUERY ,it will return a result object. So the condition becomes false and you are getting the else part printed.
This is my search code,it works but when I got to print go even without entering any character. It still searches. Worst, it prints all the items in the database what would I do. Thanks.
if(isset($_GET['search'])) {
$search_value= $_GET['searchbox'];
//$sql="SELECT idemp,sn FROM employee
//WHERE idemp like '%$search_value%' OR
//sn like '%$search_value%'";
$sql = "select * from employee where (id_no like '%$search_value%' OR sn like '%$search_value%')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "'$result->num_rows' result/s for '$search_value'";
print "<br><br>";
You might need to change your if condition as
if(isset($_GET['search']) && !empty($_GET['searchbox'])){
// do search
}
if(isset($_GET['search'])) will return true because it is set to empty string.
if(isset($_GET['search']) && $_GET['search'] )
is the condition you are looking for
First check that your getting any string or not,
Run your query only when you got something.
if(isset($_GET['searchbox']) && $_GET['searchbox'] != ''){
$search_value= $_GET['searchbox'];
$sql = "select * from employee where (id_no like '%$search_value%' OR sn like '%$search_value%')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "'$result->num_rows' result/s for '$search_value'";
print "<br><br>";
}
$id = 2;
// query to fetch delayed
$sql = "SELECT * FROM leads WHERE status = ('$id') LIMIT 1";
$obResult = $objConnection->query($sql);
// query to fetch if there is no delayed
$q = "SELECT * FROM leads WHERE status = ('$id') AND later != '1' ORDER BY postnummer LIMIT 1";
$oResult = $objConnection->query($q);
// primary query to run, which makes use of the two above accordingly
$query = "SELECT * FROM leads WHERE status = ('$id') LIMIT 1";
$objResult = $objConnection->query($query);
while ($row = $objResult->fetch_object()) {
if (new DateTime() > $row->delay && $row->delay != '0000-00-00 00:00:00') {
while ($row = $obResult->fetch_object()) {
echo $row->firmanavn;
}
} else {
while ($row = $oResult->fetch_object()) {
echo $row->firmanavn;
}
}
}
Changed my code to this, and still i got the same problem, the if clause if met, but it echoes from my else instead
Reason is that you are using same variable names that overwrite each other. $objResult rename this to something like $objResult2 for the inner query.
One thing to keep in mind is that your inner query inside a loop is really unnecessary unless you did not provide some piece of code. You can just put that query outside of while loop. Will save you time & memory.
Although I think there are other ways this code could be cleaned up, it seems to me that you're attempting to compare a DateTime object to a date string, which is going to yield unpredictable results. Try changing your while part to:
$current_date = new DateTime();
while ($row = $objResult->fetch_object()) {
if (
$current_date->format('Y-m-d H:i:s') > $row->delay &&
$row->delay != '0000-00-00 00:00:00'
) {
while ($row = $obResult->fetch_object()) {
echo $row->firmanavn;
}
} else {
while ($row = $oResult->fetch_object()) {
echo $row->firmanavn;
}
}
}
I'm not sure this will fully solve your issues, but it should get you closer.