This my screenshot problem I have
I have the following codes in my view.
$jumlah = $this->db->query("
SELECT * FROM actor
JOIN film_actor ON film_actor.actor_id=actor.actor_id
JOIN film ON film.film_id=film_actor.film_id
JOIN film_category ON film_category.film_id=film.film_id
JOIN category ON category.category_id=film_category.category_id
WHERE first_name like $u->first_name AND last_name like $u->last_name");
?>
why my query not working? I think I have done with that code, but why I am getting error message.
Sorry, i can't describe my problem so detail, but i have image for describe that.
$array = array('first_name' => $u->first_name, 'last_name' => $u->last_name);
$jumlah = $this->db->select('*')->from('actor');
$this->db->join('film_actor', 'film_actor.actor_id=actor.actor_id');
$this->db->join('film', 'film.film_id=film_actor.film_id');
$this->db->join('film_category', 'film_category.film_id=film.film_id');
$this->db->join('category', 'category.category_id=film_category.category_id ');
$this->db->like($array); // WHERE `first_name` LIKE '%$u->first_name%' AND `last_name` LIKE '%$u->last_name%'
Related
I have five tables that produce the results I need, but with five different queries.
Tables are [temperature, partSize, volume, stiffness and weight]. Each table has a column "type" that holds identical values among all tables.
The following queries gives me the results I need...but in five different tables.
$result_temp = mysqli_query($conn, "SELECT * FROM temperature WHERE '%{$_POST['temperature']}%' LIKE '%{$_SESSION['temperature']}%'");
$result_size = mysqli_query($conn, "SELECT * FROM partSize WHERE '%{$_POST['partSize']}%' LIKE '%{$_SESSION['partSize']}%'");
$result_vol = mysqli_query($conn, "SELECT * FROM volume WHERE '%{$_POST['volume']}%' LIKE '%{$_SESSION['volume']}%'");
$result_stiff = mysqli_query($conn, "SELECT * FROM stiffness WHERE '%{$_POST['stiffness']}%' LIKE '%{$_SESSION['stiffness']}%'");
$result_wght = mysqli_query($conn, "SELECT * FROM weight WHERE '%{$_POST['weight']}%' LIKE '%{$_SESSION['weight']}%'");
I need a solution that combines the results of these tables and I am stuck. I keep getting this error: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean
I'm know mySQL will do this and I'm pretty sure it's a simple syntax error but I cannot figure this out.
I've tried this (and many variations...with CONCAT, without CONCAT, tick marks, no tick marks):
$result = mysqli_query($conn, "SELECT * FROM `temperature` WHERE CONCAT ('%{$_POST['partSize']}%') LIKE CONCAT ('%{$_SESSION['partSize']}%') INNER JOIN `partSize` ON temperature.type = partSize.type INNER JOIN `volume` ON partSize.type = volume.type INNER JOIN `stiffness` ON volume.type = stiffness.type INNER JOIN `weight` ON stiffness.type = weight.type ");
The problem seems to arise as soon as I try to INNER JOIN or JOIN the tables. I've spent hours on this already. :/ Can anyone help?
I don't know what version of PHP you're using, but I'd encourage you to switch to PDO and to also use prepared statements (regardless of your version). However the syntax for your statement should be:
SELECT [columns] FROM [first table] INNER JOIN [other tables] ON [first table] WHERE [conditions]
With that being said, take a look at this example:
<?php
$config = array(
'host' => '[db host ip]',
'username' => '[db username]',
'password' => '[db password]',
'name' => '[db name]'
);
$dsn = "mysql:dbname=$config[name];host=$config[host];charset=utf8mb4";
$db = new PDO($dsn, $config['username'], $config['password']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$stmt = $db->prepare('
SELECT
*
FROM
`temperature` AS t
INNER JOIN `partSize` AS p ON t.`type` = p.`type`
INNER JOIN `volumne` AS v ON t.`type` = v.`type`
INNER JOIN `stiffness` AS s ON t.`type` = s.`type`
INNER JOIN `weight` AS w ON t.`type` = w.`type`
WHERE
t.`temperature` LIKE CONCAT('%', :temperature, '%') AND
t.`partSize` LIKE CONCAT('%', :partSize, '%') AND
t.`volumne` LIKE CONCAT('%', :volumne, '%') AND
t.`stiffness` LIKE CONCAT('%', :stiffness, '%') AND
t.`weight` LIKE CONCAT('%', :weight, '%');');
// Execute the query passing the $_POST values as the parameters
$stmt->execute(array(
':temperature' => $_POST['temperature'],
':partSize' => $_POST['partSize'],
':volume' => $_POST['volume'],
':stiffness' => $_POST['stiffness'],
':weight' => $_POST['weight']
);
// Get all of the rows returned by the query and store them in an associative array
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
I think there is something changed in the union between Laravel 4 and Laravel 4.1. I have 2 models.
$photos = DB::table('photos')->select('id', 'name', 'created_at');
$videos = DB::table('videos')->select('id', 'name', 'created_at');
I want to union the 2 querys and order the 2 querys with the created_at field.
$photos = $photos->orderBy('created_at', 'desc');
$combined = $photos->union($videos);
With Laravel 4 it gives me this query:
select `id`, `name`, `created_at` from `videos`
union
select `id`, `name`, `created_at` from `photos`
order by `created_at` desc
This works ok, it sorts the results for both querys together. In Laravel 4.1 it gives me this query:
(select `id`, `name`, `created_at` from `videos`)
union
(select `id`, `name`, `created_at` from `photos` order by `created_at` desc)
This results in a list of videos and after that an ordered list of photos. I need to have a list where the to combined querys are sorted. I want Laravel to give me this query:
(select `id`, `name`, `created_at` from `videos`)
union
(select `id`, `name`, `created_at` from `photos`)
order by `created_at` desc
How do get this working in Laravel?
This i believe is a bug and is not fixed yet. I have the same issue when trying to sort union queries.
$query1->union($query2)->orderBy('foo','desc')
causes the order by clause to be added to $query 1 alone.
Adding orderBy individually to $query1 and $query2 and then doing a union like below
$query1->orderBy('foo desc');
$query2->orderBy('foo desc');
$query1->union($query2);
This obviously works but it does not produce the same result as doing a orderBy on the union's result.
For now, the workaround seem to be doing something like
$query = $query1->union($query2);
$querySql = $query->toSql();
$query = DB::table(DB::raw("($querySql order by foo desc) as a"))->mergeBindings($query);
This would produce a query like:
select * from (
(select a as foo from foo)
union
(select b as foo from bar)
) as a order by foo desc;
And that does the trick.
I don't really know Laravel, but I'll bet this will do it:
$photos = DB::table('photos')->select('id', 'name', 'created_at');
$videos = DB::table('videos')->select('id', 'name', 'created_at');
$combined = $photos->union($videos)->orderBy('created_at', 'desc');
It seems to be fixed in this pull request: https://github.com/laravel/framework/pull/3901
It should work if you add orderBy methods in the chaining to both of them, like this:
$photos = DB::table('photos')->select('id', 'name', 'created_at')->orderBy('created_at', 'desc');
$videos = DB::table('videos')->select('id', 'name', 'created_at')->orderBy('created_at', 'desc');
$combined = $photos->union($videos);
Right now, as Barmar said, Laravel only knows that the photos query should be ordered, since you do that in your third line, which can be removed if you do it like above.
You can try with DB::query() like below:
DB::query('(Select id,name,created_at from photos)
union
(Select id,name,created_at from videos) order by created_at ASC');
I guess as of know it will work. Still looking for actual solution!
You can use fromSub to create a sub query of unions. I find this much cleaner than using mergeBindings.
$photos = DB::table('photos')->select('id', 'name', 'created_at');
$videos = DB::table('videos')->select('id', 'name', 'created_at')->union($photos);
$result = DB::query()->fromSub($querySql, 'a')->orderBy('year', 'desc')->get();
I am trying to pull a list of Events, also seeing which members have paid for the Events. I then want to see if they are on the committee, to see if they have admin permissions.
I have successfully done this, using three SQL queries, then using three foreach loops to build the Array.
I am SURE this can be done with one SQL query and one foreach loop, however I have not yet mastered the JOIN technique.
I am using Expression Engine, Codeigniter Active Record, I will display to you the SQL output and also what my current EE functions look like.
THANKS FOR THE HELP! :D
SQL to select ALL events which are active
SELECT `id` as event_ID, `name` as event_name, `description` as event_description
FROM (`events`)
WHERE `events_category_id` = '1'
AND `active` = 1
ORDER BY `name` asc
EE CODE to achieve this:
$query = $this->theDb->select('id as event_ID, name as event_name, description as event_description')
->order_by("name", "asc")
->get_where('events', array('event_category_id'=>$event_type,'active'=>1));
**
SQL to find what EVENT IDs the user has paid for
**
SELECT DISTINCT `products`.`event_ID` as joinedID
FROM (`transactions_items`)
JOIN `transactions` ON `transactions`.`id` = `transactions_items`.`id`
JOIN `products` ON `products`.`id` = `transactions_items`.`product_id`
JOIN `events` ON `events`.`id` = `products`.`event_ID`
WHERE `transactions`.`member_id` = 27500
AND `events`.`active` = 1
AND `event_category_id` = '1'
ORDER BY `events`.`name` asc
EE CODE to achieve this
$query = $this->theDb->select('products.event_ID as joinedID')
->distinct()
->order_by("events.name", "asc")
->join('transactions', 'transactions.id = transactions_items.id')
->join('products', 'products.id = transactions_items.product_id')
->join('events', 'events.id = products.event_ID')
->get_where('transactions_items', array('transactions.member_id' => $memberID, 'events.active' => 1,'activity_category_id'=>$activity_type));
SQL to find ADMIN rights
SELECT `events`.`id` as event_ID, `admins`.`admin_role_id` as role_id, `admins_roles`.`name` as role_description
FROM (`admins`)
JOIN `admins_roles` ON `admins`.`admin_role_id` = `admins_roles`.`id`
JOIN `events` ON `events`.`id` = `admins`.`event_ID`
WHERE `admins`.`member_id` = 27500
AND `events`.`active` = 1
EE CODE to achieve this
$query = $this->theDb->select('events.id as event_ID, admins.admin_role_id as role_id, admins_roles.name as role_description')
->join('admins_roles', 'admins.admin_role_id = admins_roles.id')
->join('events', 'events.id = admins.event_ID')
->get_where('admins', array('admins.member_id' => $memberID, 'events.active' => 1));
FOR EACH LOOPS
// Create list of Events setting defaults
foreach($events_list as $row)
{
$combinedEvents[$row->event_ID] = array(
'eventID' => $row->event_ID,
'eventName' => $row->event_name,
'eventDescription' => $row->event_description,
'isJoined' => 0,
'roleID' => 0,
'roleDescription' => "",
);
}
// Add Committee roles
foreach($admin_list as $row)
{
$combinedEvents[$row->event_ID]['roleID'] = $row->role_id;
$combinedEvents[$row->event_ID]['roleDescription'] = $row->role_description;
}
// Add Transactions
foreach($transaction_list as $row)
{
$combinedEvents[$row->joinedID]['isJoined'] = 1;
}
I don't quite understand the FOREACH part because I've never touched PHP - but you should be able to solve the multiple SQL queires using the ;with clause. I have created an example in response to another question here and here. Is this what you're looking for?
I have the following query:
select
`t`.`id` AS `id`,
`rm`.`role_id` AS `role_id`,
`t`.`id` AS `sequence`,
`t`.`parent_id` AS `parent_id`,
`t`.`label` AS `label`,
`t`.`order` AS `order`,
(case
when isnull(`rm`.`id`) then 0
else 1
end) AS `description`,
`tb`.`label` AS `parent_label`
from
((`tbl_menu` `t`
left join `tbl_menu` `tb` ON ((`t`.`parent_id` = `tb`.`id`)))
left join `tbl_role_menu` `rm` ON ((`rm`.`menu_id` = `t`.`id`))) and rm.role_id = $role_id
where
isnull(`tb`.`label`)
union select
`t`.`id` AS `id`,
`rm`.`role_id` AS `role_id`,
`t`.`parent_id` AS `parent_id`,
`t`.`parent_id` AS `sequence`,
`t`.`label` AS `label`,
`t`.`order` AS `order`,
(case
when isnull(`rm`.`id`) then 0
else 1
end) AS `description`,
`tb`.`label` AS `parent_label`
from
((`tbl_menu` `t`
left join `tbl_menu` `tb` ON ((`t`.`parent_id` = `tb`.`id`)))
left join `tbl_role_menu` `rm` ON ((`rm`.`menu_id` = `t`.`id`))) and rm.role_id = $role_id
where
(`tb`.`label` is not null)
order by `sequence` , `parent_id` , `label`
On both queries, on the second left join I have to pass a variable $role_id. Currently, I have this query on a view but if a try passing a criteria condition, the resulting query is
select * form menu_links where role_id = $role_id
Being menu_links the name of the view. This doesn't give me the result I want. I need a way to add this parameter to this query and transform it into a CDbCriteria in order to pass it to a CGridView. Any help?
Thanks.
Consider using CArrayDataProvider.
CArrayDataProvider acts as a wrapper around a simple associative array and CGridView won't know the difference. You can even apply pagination, sorting etc. An example showcasing these features can be found in the documentation:
$rawData=Yii::app()->db->createCommand('SELECT * FROM tbl_user')->queryAll();
$dataProvider=new CArrayDataProvider($rawData, array(
'id'=>'user',
'sort'=>array(
'attributes'=>array(
'id', 'username', 'email',
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
I give you a simple example you will figure out how to apply it to you case
$sql= "select * form menu_links where role_id = :role_id";
$role_id='Something you will get from your could';
$command = Yii::app()->db->createCommand($sql);
// And finally the command you can replace the role id with varibale is
$command->bindParam(":role_id", $role_id, PDO::PARAM_STR);
$result = $command->queryAll();
I hope this was what you were asking.
So I have been lookin for mistake for a while, but still can't find it.
Here is the code -
$this->db->select('*');
$this->db->from('friendRequests');
$this->db->where(array('friendRequests.status' => 1, 'users.status' => 1));
$this->db->or_where(array('friendRequests.senderId' => $this->session->userdata('userId'), 'friendRequests.receiverId' => $this->session->userdata('userId')));
$this->db->join('users', 'users.id = '.$this->session->userdata('userId'));
$query = $this->db->get();
It provides me this error -
Unknown column '1' in 'on clause'
SELECT *
FROM (`friendRequests`)
JOIN `users` ON `users`.`id` = `1`
WHERE `friendRequests`.`status` = 1
AND `users`.`status` = 1
OR `friendRequests`.`senderId` = '1'
OR `friendRequests`.`receiverId` = '1'
If an entry is surrounded in backticks it counts as a column even if it would not be one without the backtics. It thinks that 1 is a column on the JOIN line because of this.
Apparently this is a product of the join method in CI. You can fix it very easily by moving that condition to the WHERE clause. There's no need for it to be in the JOIN clause.
JOIN does not work that way; the syntax is:
JOIN 'tablename' ON 'tablename.field' = 'othertable.field'
If I had to assume you were trying to get a user's friend requests:
JOIN 'users' ON 'users'.'id' = 'friendRequests'.'receiverId'