MYSQL ERROR 1096 - NO TABLES USED - CODEIGNITER - php

I am using a custom query as the Active Record equivelant did not work for me.
When placing the Query in my Database Software SQLYOG it works fine however in CodeIgniter it says
A Database Error Occurred
Error Number: 1096
No tables used
SELECT *
Filename: C:\xampp\htdocs\midas\system\database\DB_driver.php
Line Number: 330
Here is my Query:
SELECT intervention.department_id, department_name, COUNT(*)
FROM intervention
LEFT JOIN department ON department.department_id = intervention.department_id
GROUP BY intervention.department_id, department.department_name
ORDER BY COUNT(*) desc
LIMIT 1
It is a bit of a strange problem.
Here is my Schema also:
http://i.imgur.com/mKNtc.png

Its ok I figured it out.
For custom query in Codeigniter you cannot use get method after.

EDIT
This will not work. As noted below only COUNT(*) or COUNT(table.field) work.
I think you need to specify which table you are using COUNT(*) on, so change it to something like COUNT(department.*) or COUNT(intervention.*)

If you use custom queries in Code Igniter you must return the result (Database object) to controller, because get method (from $this->db) doesn´t work.

Related

Laravel 4.2 Insert from Select

I am completely new to Laravel Framework. And I am using 4.2 version.
I am trying to run below sql statements through function calling from routes.php.
The page shows no error.
But there is no 'truncate' or 'insert' happend in Database.
Here is the code in Controller funtion:
DB::statement('TRUNCATE TABLE calc2');
DB::statement(
"Insert into calc2
Select groups, members, date(timestamp) as Date, count(id) as Total, sum(order) as Order
from Order
where order > 0 and date(timestamp) >= '$lastday'
group by groups, members, date(timestamp)
");
I checked DB connections. I also run these query directly in MySQL. It is working fine.
I tried DB::select instead of DB::statement and I tried with Eloquent/raw too.
But still Database having 0 records only.
Thanks in advance if anyone can suggest me where I am doing mistake.
Are you commiting your queries?
If not, just use DB::commit();
Doc: http://laravel.com/docs/4.2/database#database-transactions

fulltext search method didn't work

i tried to build a search engine in my php script using full text feature,but it didn't get any results.
steps i did:
i altered a 2 fileds to full text
ALTER TABLE stack_ask ADD FULLTEXT(q_title,q_body)
then i added some data
INSERT INTO articles (q_title,q_body) VALUES('MySQL Tutorial','DBMS stands for DataBase ...');
then i made a variable to recieve a post data from input
$q_title=$db->real_escape_string($_POST['search_word']);
then the query:
SELECT * FROM stack_ask WHERE MATCH (q_title,q_body) AGAINST ('$q_title');
when i tried to search something like database it didn't give any result and output the following error:
call to amember function fetch_assoc on a non-object
what's the problem?
Hi Mohamed amin,
Make sure that q_title,q_body has indexing or not. Read this article
may it will be useful for you.
http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html
after multiple trials,i solved the problem by the following:
1-i checked the index type of 2 columns using the following sql:
show index from stack_ask
and they were full text
2-i checked the table type ,which was MYISAM
2-the correct sql query should be like the following:
SELECT *, MATCH(q_title,q_body) AGAINST ('$q_title') FROM stack_ask ORDER BY id DESC
not:
"SELECT id, MATCH(q_title,q_body) AGAINST ('$q_title') FROM stack_ask ORDER BY id DESC

CakePHP: HABTM not performing join properly on habtmDelete call

We are upgrading from Cake 1.2 to 1.3.8, and using the ExtendedAssociations behavior.
One of the problems we are having, is in our Store model, which has a HABTM relationship to Module via stores_modules.
Retrieving the data is all good, but one issue moving to 1.3.8 seems to have introduced is that when we want to delete all Modules from the Store - in the Stores model
$this->habtmDeleteAll('Module', $this->id);
The problem is that this produces the following SQL query (as it is SELECTING all stores_modules first, to see what to delete)
SELECT StoresModule.module_id FROM stores_modules AS StoresModule WHERE StoresModule.store_id = 1 AND Module.system_default = 0
_
2011-03-29 18:27:46 Warning: Warning (512): SQL Error: 1054: Unknown column 'Module.system_default' in 'where clause' in [/usr/lib/cakephp_1.3.8/cake/libs/model/datasources/dbo_source.php, line 684]
So it appears the problem is that the above SQL statement is not adding the JOIN to the modules table.. hence it cannot fine Module.system_default
Any help on this would be great, it's really got me stumped :)
Chris.
I'm not totally sure I understand, but it appears you are not calling your models correctly. If you need to delete all associated model records, you would probably do something like this:
$this->Store->Modules->deleteAll(array('Store.id' => $id), true, false);
NOT TESTED!

Yii framework pagination problem with MySQL FOUND_ROWS

I am trying to implement pagination in my search results with Yii. I have pagination working on my browse record pages, but for some reason, am having trouble with getting it working in search.
So far, I have the following SQL to produce the search results:
SELECT SQL_CALC_FOUND_ROWS DISTINCT user.id FROM user, personal_info WHERE (personal_info.bio LIKE '%a%' ) AND personal_info.user_id = user.id AND user.role = 'F' LIMIT 0, 8;
Which is passed to ActiveRecord as follows:
$results = $this->findAllBySql($sql);
Immediately afterwards I run this code:
$rows = $this->findBySql("SELECT FOUND_ROWS() as row_count;");
echo $rows->row_count;
Strangely, I am receiving the following error when I try and execute the above code:
Property "Search.row_count" is not defined.
For some reason, Yii is not able to retrieve the FOUND_ROWS value from MySQL.
I have pretty much exactly the same PHP code in my browse records page (but different SQL), and it works perfectly. Not sure why in this situation Yii is unable to retrieve the FOUND_ROWS value. I've tried running this code directly inside MySQL to see if there was something wrong with my SQL, but it retrieves the FOUND_ROWS value with no problems - this problem only happens when I try to do it inside Yii.
Any idea what I maybe doing wrong?
Many thanks!
Turns out that I needed to define $row_count inside the model before it would recognise it as a variable I can refer to.

MySQL Query problem in CodeIgniter

So I'm using the following:
$r = new Record();
$r->select('ip, count(*) as ipcount');
$r->group_by('ip');
$r->order_by('ipcount', 'desc');
$r->limit(5);
$r->get();
foreach($r->all as $record)
{
echo($record->ip." ");
echo($record->ipcount." <br />");
}
Standard:
SELECT `ip`, count(*) as ipcount FROM (`soapi`) GROUP BY `ip` ORDER BY `ipcount` desc LIMIT 5;
And I only get the last (fifth) record echo'ed out and no ipcount echoed.
Is there a different way to go around this? I'm working on learning DataMapper (hence the questions) and need to figure some of this out. I haven't quite wrapped my head around the whole ORM thing.
Is there a way to set the count(*) as ipcount without the funny select() statement? I don't think it's triggering for some reason. This could also be a bug in DataMapper, but I'm less certain of that.
Also I found that even if I use the $r->query() method it doesn't return anything except the last entry if I use something like SELECTipFROMsoapiWHERE 1;. It will however return everything (like it should) if I say SELECT * FROM soapi WHERE 1;. If it doesn't have the * it only returns the last line.
Just verified with the new query, anything except selecting all columns (*) only returns the last record. Any help with this would be great. You can craft a statement like select *, count(*) as ipcount but then you still don't have access to it via $record->ipcount.
For your case, once you use COUNT() function in MySQL, it will only return 1 value. Hence you ip result data would not display out.
I suggest you just split this 2 queries.
1. for COUNT(*)
2. for ip
Hope this help.

Categories