Hi need to count all rows in two MySQL tables.
Tables have same structure and the code I'm using in the model file is the following:
$this->regs_db->select ("*");
$this->regs_db->from("$this->table_images_civil, $this->table_images_military");
return $this->regs_db->count_all_results();
The result I'm getting is not correct at all. I know that in the first table there are 10,934 rows, in the second one there are 1,299 rows...but the result I get is 14,203,266.
What's wrong with the code above?
Thanks a lot for any hint.
EDIT: below table strusture...same for both tables
First, I'd drop Code Igniter's DB methods. They're (in my view) obstructive and useless for anything except basic query structures.
With that in mind, you can achieve what you want with something like this:
$sql = '
SELECT
(SELECT COUNT(id) FROM `'.$this->table_images_civil.'`) AS tbl1Count,
(SELECT COUNT(id) FROM `'.$this->table_images_military.'`) AS tbl2Count';
$request = $this->regs_db->query($sql);
$arr = $request->fetch_assoc();
$this->db->from('yourtable');
[... more active record code ...]
$query = $this->db->get();
$rowcount = $query->num_rows();
Related
I'm tryig to get the data that comes from the mysql query to use it later. I don't care if I have to output the data in a table with join clauses. I only need to be able to get a speficic part from it. But i want to get all the ones that are related or have relevance to each other.
this is made with the codeigniter MVC framework with php
example database with tables
I want to be able to access the data from each table with one query
function get_reg(){
$this->db->select('*');
$this->db->from('
tableA.*,
tableB.*,
tableC.*,
tableD.*
');
$this->db->where('tableA.name = tableB.name');
$this->db->where('tableC.name = tableD.name');
$this->db->where('tableA.name = tableD.name');
$this->db->where('tableC.name = tableB.name');
$query = $this->db->get();
return $query->result_array();
}
something like this to then be accessed like so:
$this->load_model->get_reg() //and get what I want
I dont know if this is posible.
To do that in a single query, you need to use JOIN syntax.
Your answer would look something like
$this->db->from('tableA');
$this->db->join('tableB', 'tableA.name = tableB.name', 'LEFT'); // the type of join depends on the behavior you want
$this->db->join('tableC', 'tableA.name = tableC.name', 'LEFT');
$this->db->join('tableD', 'tableA.name = tableD.name', 'LEFT');
$query = $this->db->get();
The above joins all the tables together based on the name, which appears to be the same in all the tables. The type of join matters when some rows of the joined tables don't have a value for name.
You can read more in the docs
I've this code:
public function getAllAccess(){
$this->db->select('accesscode');
$this->db->where(array('chain_code' => '123');
$this->db->order_by('dateandtime', 'desc');
$this->db->limit($this->config->item('access_limit'));
return $this->db->get('accesstable')->result();
}
I need to join it with another table (codenamed table), I've to tell it this. Not really a literal query but what I want to achieve:
SELECT * accesscode, dateandtime FROM access table WHERE chain_code = '123' AND codenames.accselect_lista != 0
So basically accesstable has a column code which is a number, let us say 33, this number is also present in the codenames table; in this last table there is a field accselect_lista.
So I have to select only the accselect_lista != 0 and from there get the corrisponding accesstable rows where codenames are the ones selected in the codenames.
Looking for this?
SELECT *
FROM access_table a INNER JOIN codenames c ON
a.chain_code = c.chain_code
WHERE a.chain_code = '123' AND
c.accselect_lista != 0
It will bring up all columns from both tables for the specified criteria. The table and column names need to be exact, obviously.
Good start! But I think you might be getting a few techniques mixed up here.
Firstly, there are two main ways to run multiple where queries. You can use an associative array (like you've started to do there).
$this->db->where(array('accesstable.chain_code' => '123', 'codenames.accselect_lista !=' => 0));
Note that I've appended the table name to each column. Also notice that you can add alternative operators if you include them in the same block as the column name.
Alternatively you can give each their own line. I prefer this method because I think its a bit easier to read. Both will accomplish the same thing.
$this->db->where('accesstable.chain_code', '123');
$this->db->where('codenames.accselect_lista !=', 0);
Active record will format the query with 'and' etc on its own.
The easiest way to add the join is to use from with join.
$this->db->from('accesstable');
$this->db->join('codenames', 'codenames.accselect_lista = accesstable.code');
When using from, you don't need to include the table name in get, so to run the query you can now just use something like:
$query = $this->db->get();
return $query->result();
Check out Codeigniter's Active Record documentation if you haven't already, it goes into a lot more detail with lots of examples.
Hi there i am working on PHP code that is selecting columns from two tables.
Here is my code:
$result2 = mysql_query("SELECT *
FROM `videos`, `m_subedvids`
WHERE `videos.approved`='yes' AND
`videos.user_id`='$subedFOR'
ORDER BY `videos.indexer`
DESC LIMIT $newVID");
while($row2 = mysql_fetch_array($result2))
{
$indexer = addslashes($row2['videos.indexer']);
$title_seo = addslashes($row2['videos.title_seo']);
$video_id = addslashes($row2['videos.video_id']);
$title = addslashes($row2['videos.title']);
$number_of_views = addslashes($row2['videos.number_of_views']);
$video_length = addslashes($row2['videos.video_length']);
}
When i try to print $indexer with echo $indexer; it's not giving me any results.
Where is my mistake in this code?
It seems to me like the key 'indexer' isn't in your results. It's hard to tell, since you haven't listed a definition for your table and you're using SELECT * so we can't see the names.
It makes the program easier to read later, if instead of SELECT *..., you use SELECT col1, col2, .... Yes, SELECT * will save you some typing right now, but you'll lose that time later when you or anyone else who works on your code has to check the table definition every time they work with that line of code.
So, try changing your query to explicitly select the columns you use. If it's an invalid column you'll get an error right away rather than this silent failure you're getting now, and you'll thank yourself later as well.
So long as videos.indexer is a unique field name among all tables used in the query you can change
$indexer = addslashes($row2['videos.indexer']);
to
$indexer = addslashes($row2['indexer']);
You don't need to (or can not) use the table name when referring to the result.
I have the following query using CodeIgniter. I want a list that includes a single row for each group and I want the row to inlcude the liveBlogGroupRecord status if it is "1".
Right now, I either get only rows for groups that have liveBlogGroupRecord=1 or, if I comment that line out, I get multiple group rows from the same group since the blogGroups table has multiple old values in it that have liveBlogGroupRecord=0.
$this->db->select('*');
$this->db->from('groups');
$this->db->join('blogGroups', 'groups.groupID = blogGroups.groupID', 'left');
$this->db->where('blogGroups.blogID', $blogID);
$this->db->where('blogGroups.liveBlogGroupRecord', 1);
$query = $this->db->get();
Be careful with SELECT *. Assuming your blogGroups and groups tables both have an id (or another column that with the same name) only the first one will be returned when you're JOINing another table.
Selectively choose which columns you need. Try something like this:
$this->db->select('groups.*');
/* And more selectively, whichever elements you have from `blogGroups` that you need (I have no idea what those are) */
$this->db->select('blogGroups.name');
$this->db->select('blogGroups.description');
$this->db->select('blogGroups.category');
/* etc */
how about adding $this->db->limit(1); before $query = $this->db->get();
I heed two variables storing the maximum id from a table, and the minimum id from the same table.
the first id is easy to be taken ,using find() and a query like
$first = Model::factory('product')->sale($sale_id)->find();
but how can i retrieve the last id? is there a sorting option in the Kohana 3 ORM?
thanks!
Yes, you can sort resulting rows in ORM with order_by($column, $order). For example, ->order_by('id', 'ASC').
Use QBuilder to get a specific values:
public function get_minmax()
{
return DB::select(array('MAX("id")', 'max_id'),array('MIN("id")', 'min_id'))
->from($this->_table_name)
->execute($this->_db);
}
The problem could actually be that you are setting order_by after find_all. You should put it before. People do tend to put it last.
This way it works.
$smthn = ORM::factory('smthn')
->where('something', '=', something)
->order_by('id', 'desc')
->find_all();
Doing like this, I suppose you'll be :
selecting all lines of your table that correspond to your condition
fetching all those lines from MySQL to PHP
to, finally, only work with one of those lines
Ideally, you should be doing an SQL query that uses the MAX() or the MIN() function -- a bit like this :
select max(your_column) as max_value
from your_table
where ...
Not sure how to do that with Kohana, but this topic on its forum looks interesting.