This question already has answers here:
Codeigniter select query with AND and OR condition
(8 answers)
Closed 5 years ago.
I am doing this to build a query and to get seats from a "flights" table in codeigniter
$this->load->database();
$this->load->dbforge();
$where = "seatsF>0 OR seatsB>0 OR seatsE>0";
$this->db->where($where);
$this->db->where('approved', 1);
$query=$this->db->get("Flights");
$result=$query->result_array();
echo $this->db->last_query();
die;
return $result;
My current query being generated by query builder in php is
SELECT * FROM `Flights` WHERE `seatsF` >0 OR `seatsB` >0 OR `seatsE` >0 AND `approved` = 1
But the result contains result where "approved"=0 also, I guess what my query is doing is that it gets 1 seatsF>0 and then OR shortcircuits and returns true and it is not really checking the whole query. How can I produce a query like this by query builder:-
SELECT * FROM `Flights` WHERE `(seatsF` >0 OR `seatsB` >0 OR `seatsE` >0) AND `approved` = 1
You need to wrap the OR's in ().
$where = "(seatsF>0 OR seatsB>0 OR seatsE>0)";
Related
This question already has an answer here:
PDO, MySQL SELECT statement returning boolean true?
(1 answer)
Closed 11 months ago.
I have seen similar posts to this one already existing but none of them have helped.
When I run this PHP code with $retrievestat = 1, I get a value of 1 return by the search query in php:
$retrievestat = $_POST["statIdentifier"];
echo $retrievestat;
// Retrieve The Player With The Most Rounds Won With Their Rounds
if ($retrievestat == 1)
{
$checkuniquequery = "SELECT COUNT(*) FROM playerstats WHERE roundswon = (SELECT MAX(roundswon) FROM playerstats);"; // Get number of rows that have a roundswon value equal to the max in the table
$stmt = $conn->prepare($checkuniquequery);
echo ($stmt->execute());
if ($stmt->execute() == 1) // If only one row has the max roundswon then get the username + roundswon
{
$mostroundswonquery = "SELECT username, MAX(roundswon) FROM players, playerstats WHERE players.id = playerstats.id";
$stmt = $conn->prepare($mostroundswonquery);
//echo ($stmt->execute());
}
However, when I run this query in phpMyAdmin:
"SELECT COUNT(*) FROM playerstats WHERE roundswon = (SELECT MAX(roundswon) FROM playerstats);"
I get 2 returned as the output.
Any ideas why this is happening and how to fix it?
The execute method on PDO statement ($stmt->execute()) does return either true (if execution was successful) or false if not. It does not return the return value of the SQL statement. On a side node: $stmt->execute() == 1 does behave the same as $stmt->execute() == true !
To get the actual return value you need to fetch the result after execution.
E.g. you can use $stmt->fetch() to get actual result.
Have a look at the documentation to learn more: https://www.php.net/manual/en/class.pdostatement.php
This question already has answers here:
Count rows from results of a "mysql_query"
(9 answers)
MySQL - count total number of rows in php
(11 answers)
count number of rows in table using php
(6 answers)
How can I count the numbers of rows that a MySQL query returned?
(12 answers)
PHP SQL get SELECT COUNT(user) to variable
(5 answers)
Closed 4 years ago.
I have a database table timestamps_sessions containing timestamps of when a user begins an exercise on my webpage, and which is only updated when the user actually finishes it. Therefore, every row always has a value in the started column, but not always in the finished column. The latter is NULL by default.
My SELECT COUNT() statement works perfectly when I query it in Sequel Pro, and returns the correct integer of 11. That is to say: there are indeed only eleven rows that have values in both started and finished.
Yet when I execute it in PHP, it returns an object containing
{
current_field: null,
field_count: null,
lengths: null,
num_rows: null,
type: null
}
The statement I successfully query in Sequel Pro is the following:
SELECT COUNT(finished) FROM timestamps_sessions
The statement I unsuccessfully use in PHP is the following:
$sql = "SELECT COUNT(finished) FROM timestamps_sessions";
$result = $conn->query($sql);
$exercise['clears'] = $result;
There are several other SELECT queries being performed to the same database and same table without issue. It's only the COUNT() statement that seems to be malfunctioning.
What am I doing wrongly, and how should I do it instead?
My goal is to count the number of rows with a non-empty finished column, without passing on the actual data in order to preserve bandwidth. All I need is the integer.
First of all, $result is an object as expected. It's the result returned by the mysqli::query() method. Before you can access the data from this query, you need to fetch it. It will be easier if you give an alias to the count, as it will become easier to access the count.
$sql = "SELECT COUNT(finished) as cnt FROM timestamps_sessions";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$exercise['clears'] = $row['cnt'];
mysqli::query() docs
mysqli::fetch_assoc() docs
you have missing code mysql_fetch_array in order to fetch first record
$sql = "SELECT COUNT(finished) totals FROM timestamps_sessions";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$total = $row['totals'];
The argument passed to the count function can be anything, since you just want the number of rows, not their data.
SELECT COUNT(1) FROM timestamps_sessions WHERE finished IS NOT NULL;
$sql = "SELECT COUNT(finished) AS count_finished FROM timestamps_sessions";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $exercise['clears'] = $row['count_finished'];
Give the count an alias like count_finished. Then from the result object you need to fetch the row. The row has your data in it.
Take a look at this https://www.w3schools.com/php/php_mysql_select.asp
This question already has answers here:
Understanding SUM(NULL) in MySQL
(2 answers)
Closed 4 years ago.
I want to select data from MySQL with a a where condition.
I'm sure the condition is false that's why it shouldn't return any data or any row but it returns null. I checked it with $stmt2->num_rows > 0 and this condition is true.
How can i prevent this from happening?
How my code suppose to work : this condition should be false for the first time and be true after I insert some data.
I think it's for SUM() that is used in select statement. How can I do that?
$q2="SELECT SUM(liked), SUM(disliked) FROM like_dislike WHERE post_id=?";
$stmt2 = $conn->prepare($q2);
$stmt2->bind_param('i', $post_id);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($like_status, $dislike_status);
if ($stmt2->num_rows > 0 ) {
$stmt2->fetch();
}
else {
$like_status =0;
$dislike_status =0;
}
My select statement result in phpmyadmin:
According to the documentation :
SUM([DISTINCT] expr)
Returns the sum of expr. If the return set has no rows, SUM() returns NULL. The DISTINCT keyword can be used to sum only the distinct values of expr.
If there are no matching rows, SUM() returns NULL.
So you will always have NULL as result if you have no data.
This question already has answers here:
MySQLi count(*) always returns 1
(4 answers)
Closed 5 years ago.
I have a table called quote in my database with 5 rows. However when I try to count the rows it always returns 1 instead of 5. I am using the code below:
$connection = mysqli_connect("host","username","password","database");
$querya = "SELECT COUNT(id) FROM quote";
$resulta = mysqli_query($connection, $querya);
$max = mysqli_num_rows($resulta);
$srow = rand(1,$max);
<br /> There are <?php echo $max ?> number of rows
I am counting the id column which is a primary key and therefore never null. I have also tried it by using count(*) but get the same result. Where am I going wrong?
You can't pair mysqli_num_rows with count. If you want to use mysqli_num_rows you'd have to select * (which would be slow). Instead select count(*) as total, and use total.
If you want to use COUNT() in your query give it an alias that you can return:
$querya = "SELECT COUNT(`id`) AS `Total` FROM `quote`";
$resulta = mysqli_query($connection, $querya);
$row = mysqli_fetch_assoc($resulta);
echo $row['Total']; // identifier here is the same as the alias in the query
This question already has answers here:
How to get the total number of rows of a GROUP BY query?
(11 answers)
Closed 8 years ago.
How can I return the number of counted rows with PDO?
This is my code:
$n=$dbh->prepare("SELECT count(*) FROM users_notifications WHERE userid=0 OR userid=:userid");
$n->bindParam(":userid",$userdata['id']);
$n->execute();
$notifications = count($n);
This returns just 1. Although there are 2 results when I run the query in the database: SELECT count(*) FROM users_notifications WHERE userid=0 OR userid=:userid
The count SQL function is an aggregate function that returns one row with the number of rows matching the where clause.
PHP's count returns the number of rows in a result set, which, as illustrated in the previous paragraph, should be one. Instead, you should retrieve the value of the result column. E.g.:
$n=$dbh->prepare("SELECT count(*) as c FROM users_notifications WHERE userid=0 OR userid=:userid");
$n->bindParam(":userid",$userdata['id']);
$n->execute();
$result = $n->fetch(PDO::FETCH_ASSOC);
$notifications = $result['c'];