so far i made this OOP class but it not returning any values, any help guys i realy need it
public function countRows($table ='tb_cliente')
{
if($this->tableExists($table)){
$sql ="SELECT * FROM ".$table;
$query = #mysql_query($sql);
if($query){
$count = mysql_num_rows($query);
return true;
}
else{
return false;
}
}
You are not returning the value of $count.
Try return $count;
public function countRows($table ='tb_cliente')
{
if($this->tableExists($table)){
$sql ="SELECT * totalrows FROM ".$table;
$query = #mysql_query($sql);
if($query){
$count = mysql_num_rows(sql );
return $count;
}
else{
return false;`enter code here`
}
}
or you can use:
public function countRows($table ='tb_cliente')
{
if($this->tableExists($table)){
$sql ="SELECT count(*) as counted totalrows FROM ".$table;
$query = #mysql_query($sql);
if($query){
$count = mysql_result($query,0,"counted");
return $count;
}
else{
return false;
}
}
You don't need to fetch all data if the only goal is to count rows. You can use:
SELECT COUNT(column_name) FROM table_name;
This returns the number of values of the specified column.
Or you can use:
SELECT COUNT(*) FROM table_name;
which returns number of rows from selected table.
Or you can use:
SELECT COUNT(DISTINCT column_name) FROM table_name;
which returns the number of distinct values of the specified column.
The way you are preforming to count rows in table is kind of unnecessary overkill.
Related
i have this function separated in my working page.
public function countRow(){
$id = $_SESSION['id'];
$num = 1;
$query = "SELECT count(*) from `auditsummary` where bizID=? AND statusID=?";
$sql = $this->db->prepare($query);
$sql->bindParam(1,$id);
$sql->bindParam(2,$num);
$sql->execute();
}
what i'm really trying to do in this function is to count the number of rows that are results of the query but i don't know how to do it and also how to return the value.
As you use a PDOStatement for your query, after the execute, you can use
$count = $sql->rowCount();
More information:
http://php.net/manual/en/pdostatement.rowcount.php
And to return the result, you can just do:
return $count;
Information for this:
http://php.net/manual/en/function.return.php
Use
$query = "SELECT count(*) AS getCount from `auditsummary` where bizID=? AND statusID=?";
And get the values as you normally does
$count = $row["getCount"];
Here's how I do it:
$count = "SELECT * FROM yourtable WHERE x='x' and y='y'";
$result = $dbconn->prepare($count);
$result->execute();
$t_count = $result->rowCount();
echo $t_count;
Just want to get user_id and review_text values from table reviews, select name and city from table users where id=user_id and then return review_text, username and city to controller. Please help me.
public function did_get_reviews($item_id){
$this->db->select('user_id','review_text');
$this->db->from('reviews');
$this->db->where('post_id', $item_id);
$user_id = 'user_id' //??
$this->db->select('name','city');
$this->db->from('users');
$this->db->where('id', $user_id);
$query = $this->db->get('review_text','username','city'); //??
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
Update 1:
I just tried to join tables but it returns the values for review_text only but I also want to get values for name and city. Please check the code below:
public function did_get_reviews($item_id){
$this->db->select('reviews.review_text','users.name','users.city');
$this->db->from('reviews','users');
$this->db->where('reviews.post_id', $item_id);
$this->db->join('users','reviews.user_id = users.user_id');
$query = $this->db->get();
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
i think you need to use join query in SQL. if you use this code you can access to what you want
$result = $this->db->select('review, username, city')
->from('reviews')
->join('city', 'city.user_id = review.user_id')
->get();
print_r($result);
for more information about how you can write join query(left, inner or right) in codeigniter you can see this link
i hope that this code solve your problem
UPDATE
in your new question. your code have a little buggy. you need to write your select in this way
public function did_get_reviews($item_id){
$this->db->select('reviews.review_text,users.name,users.city')
->from('reviews','users')
->where('reviews.post_id', $item_id)
->join('users','reviews.user_id = users.user_id');
$query = $this->db->get();
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
in codeigniter select Active record; any column name separate with each other by , only (not by ' and ,)
in codeigniter documentation wrote that
Permits you to write the SELECT portion of your query:
$this->db->select('title, content, date');
$query = $this->db->get('mytable');
// Produces: SELECT title, content, date FROM mytable
You can get the value for user_id using:
$this->db->select('user_id','review_text');
$this->db->from('reviews');
$this->db->where('post_id', $item_id);
$result = $this->db->get();
print_r($result);
use the same $this->db->get(); to get the values of the second query as well. Once you get the values inside a variable you can iterate it. Something like:
foreach ($result->result() as $row)
{
echo $row->name;
echo $row->city;
}
In my code, 'product' table is returning every product details in the table.
Now I want to get specific product details of 0 to 5th product. How can I change this while loop to a for loop?
Code:
public function getProducts(){
$query = "SELECT * FROM shop_product";
$resultSet = Array();
$result = mysql_query($query) or die (mysql_error());
while($fetchResult = mysql_fetch_array($result,MYSQL_ASSOC)){
$resultSet[] = $fetchResult;
}
mysql_free_result($result);
return $resultSet;
}
You could simply change your query instead to return top five rows.
SELECT * FROM shop_product LIMIT 0,5
(or)
If you don't prefer the above solution, you could use array_slice() to get a portion of the array..
while($fetchResult = mysql_fetch_array($result,MYSQL_ASSOC)){
$resultSet[] = $fetchResult;
}
$fiverowsarray = array_slice($resultSet, 0 , 5); //<--- You can add like this..
Try with LIMIT and Where conditions
public function getProducts(){
$query = "SELECT * FROM shop_product LIMIT 0,5";
$resultSet = Array();
$result = mysql_query($query) or die (mysql_error());
while($fetchResult = mysql_fetch_array($result,MYSQL_ASSOC)){
$resultSet[] = $fetchResult;
}
mysql_free_result($result);
return $resultSet;
}
What is the most efficient way in MySQL and PHP to check if a list of ids all exist? I want the function return result to be true if all ids exists, else false.
I was thinking:
$ids = array(2233, 5545, 9478, 5343, 3545);
do_all_groups_exist($ids);
function do_all_groups_exist(array $ids = array()) {
if(empty($ids)) {
return true;
}
$SQL = "SELECT count(`id`) as count
FROM groups
WHERE `id` IN (" . implode(',', $ids) . ")";
...
$row = mysqli_fetch_object($result);
return (intval($row->count) === count($ids)) ? true : false;
}
Is there a better way?
You can get result in the sql statement itself
$countids= count($ids);
$sql= "SELECT CASE WHEN (
SELECT count(id) as cnt
FROM groups
WHERE id IN (" . implode(',', $ids) . ") )=$countids
THEN 'true'
ELSE 'false' END as result"
...
$row=mysqli_fetch_array($result);
echo $row['result'];
You will get result from coloumn name 'result'
It will return true only when all ids exists in table.
I need to store the results from an SQL COUNT query in a PHP variable.
I know ModX uses PDO to run its SQL queries but I am unsure of how to go about creating this.
So far I have tried:
$sql = "SELECT COUNT (*) FROM `table` WHERE x = $x";
$results = $modx->query($sql);
$count = mysql_fetch_array($results);
echo $count;
and:
$sql = "SELECT COUNT (*) FROM `table` WHERE x = $x";
$results = $modx->getCount($sql);
echo $results;
but with no results.
Would anyone know the correct way to do this in ModX (Revo) and PDO?
A couple different methods
$results = $modx->query("select `username` from modx_users");
if (!is_object($results)) {
return 'No result!';
}
else {
$r = $results->fetchAll(PDO::FETCH_ASSOC);
echo count($r);
print_r($r);
}
$results = $modx->query("select count(id) as uids from modx_users where id >= '1'");
if (!is_object($results)) {
return 'No result!';
}
else {
$r = $results->fetch(PDO::FETCH_ASSOC);
echo 'uids = '.$r['uids'];
}