I want to write a mySQL query like the following in zend framework 2.
How to convert it in zend Db select convention?
SELECT profiles.*,
user.email,
user.first_name,
user.last_name,
portfilio_images.profileImage,
images.url AS imgurl
FROM profiles
INNER JOIN user ON user.user_id = profiles.ownerId
LEFT JOIN (
SELECT *
FROM portfilio_images
WHERE portfilio_images.profileImage = '1'
) as portfilio_images ON portfilio_images.profileId = profiles.id
LEFT JOIN images ON images.id = portfilio_images.imageId
WHERE profiles.ownerId != '4' AND (profiles.name LIKE '%a%' OR user.first_name LIKE '%a%' OR user.last_name LIKE '%a%')
GROUP BY user.user_id
You can also use Zend\Db\Sql\Select that allows you to build complex queries programatically. See the documentation here:
http://zf2.readthedocs.org/en/release-2.1.1/modules/zend.db.sql.html#zend-db-sql-select
You could always just perform a raw query:
$sql = "SELECT * FROM test";
$statement = $abAdapter->query($sql);
$result = $statement->execute();
$resultSet = new ResultSet;
$resultSet->initialize($result);
return $resultSet;
Or if you want to add some parameters
$sql = "SELECT * FROM test where col = ?";
$statement = $dbAdapter->query($sql);
$result = $statement->execute(array(99));
$resultSet = new ResultSet;
$resultSet->initialize($result);
return $resultSet;
Related
I want to view data from mysql in codeigniter based on user who is logged in
but I got an error "A Database Error Occurred". I think I wrote the wrong code
t.USER = $this->session->userdata('user_id');
Here's the code:
function get_rekomen()
{
$query = $this->db->query("SELECT
c.*,
t.produk_id,
t.id_transdet,
t.kategori_id,
t.total_qty,
t.USER
FROM
transaksi_detail AS t
LEFT JOIN
(
SELECT
g.id_produk,
p.slug_produk,
p.foto,
p.foto_type,
p.harga_diskon,
p.diskon,
p.harga_normal,
p.judul_produk,
g.kat_id,
k.judul_kategori
FROM
(
SELECT
MAX(m.id_produk) AS id_produk,
m.kat_id
FROM
produk AS m
GROUP BY
m.kat_id
)
AS g
INNER JOIN
produk AS p
ON p.id_produk = g.id_produk
LEFT JOIN
kategori AS k
ON k.id_kategori = g.kat_id
)
AS c
ON c.kat_id = t.kategori_id
WHERE
t.USER = $ this -> session -> userdata('user_id');
<<// i think here's the problem
ORDER BY
total_qty DESC limit 1")->result();
return $query;
}
and the question is what is the correct code?
Here's one solution, but first make sure on your controller the $this->load->library('session'); is loaded or you can just add in on autoload.php.
Also you must add parameter to your model that will handle the user_id in session, so for example here's your model looks like:
function get_rekomen($user_id) { ...your query here... }
Then on condition using the user_id will be
t.USER = $user_id;
So on your controller, you can call your model like:
$user_id = $this->session->userdata('user_id');
$this->ModelClass->get_rekomen($user_id);
Additional: On your login phase, if the user success from logging in, the you must set the user data on session such as this $this->session->set_userdata('user_data', $session_data);
Hope this helps!
Maybe you can use this. You need to define $user_id first before adding them to WHERE statement...
$user_id = $this->session->userdata('user_id');
$sql = "SELECT c.*, t.produk_id, t.id_transdet, t.kategori_id, t.total_qty, t.USER
FROM transaksi_detail AS t
LEFT JOIN(SELECT g.id_produk, p.slug_produk, p.foto, p.foto_type, p.harga_diskon, p.diskon, p.harga_normal, p.judul_produk, g.kat_id, k.judul_kategori
FROM(SELECT MAX(m.id_produk) AS id_produk, m.kat_id
FROM produk AS m
GROUP BY m.kat_id)
AS g
INNER JOIN produk AS p ON p.id_produk = g.id_produk
LEFT JOIN kategori AS k ON k.id_kategori = g.kat_id)
AS c ON c.kat_id = t.kategori_id
WHERE t.USER = '$user_id'
ORDER BY total_qty DESC limit 1";
$query = $this->db->query($sql);
return $query->result();
Hope this can help you...
I have this sql statement for my search function.
$a_search = "$a%";
$sql = "SELECT a.*, u.*
FROM articles AS a
LEFT JOIN users AS u
ON a.written_by = u.username
WHERE a.written_by OR a.title LIKE ? // want to search for both
ORDER BY a.written_by ASC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s",$a_search);
$stmt->execute();
$result = $stmt->get_result();
// more code ...
Works fine except one thing. It searches the articles ONLY where the a.title match. But I also want to search by the author and that's why I added a.written_by. However it still only searches for the a.title. So my question is how can I search for both at the same time?
What you wrote has no condition on a.written_by
you have to do something like
$a_search = "$a%";
$sql = "SELECT a.*, u.*
FROM articles AS a
LEFT JOIN users AS u
ON a.written_by = u.username
WHERE a.written_by LIKE ? OR a.title LIKE ? // want to search for both
ORDER BY a.written_by ASC";
$stmt = $conn->prepare($sql);
$stmt->bind_param($a_search,$a_search);
$stmt->execute();
$result = $stmt->get_result();
// more code ...
This will send the result where a.written_by LIKE $a_search or a.title LIKE $a_search
you cannot give a common or and....gotta mention it in each case...for e.g.
$a_search = "$a%";
$sql = "SELECT a.*, u.*
FROM articles AS a
LEFT JOIN users AS u
ON a.written_by = u.username
WHERE a.written_by ='cat' OR a.title LIKE 'dog' ? // want to search
ORDER BY a.written_by ASC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s",$a_search);
$stmt->execute();
$result = $stmt->get_result();
// more code ..
I am trying to write a long tail where statement which containing another select query and how I can do it. Currently, I am using Ignited Datatables Library with my CodeIgniter 3 installation.
SELECT ContactId,
ReceiverId,
SenderId,
IF(ReceiverId = 1,
(SELECT first_name
FROM users
WHERE users.id = contacts_connectivity.SenderId),
(SELECT first_name
FROM users
WHERE users.id = contacts_connectivity.ReceiverId)) AS FirstName,
IF(ReceiverId = 1,
(SELECT email
FROM users
WHERE users.id = contacts_connectivity.SenderId),
(SELECT email
FROM users
WHERE users.id = contacts_connectivity.ReceiverId)) AS SenderEmail,
IF(ReceiverId = 1,
(SELECT phone
FROM users
WHERE users.id = contacts_connectivity.SenderId),
(SELECT phone
FROM users
WHERE users.id = contacts_connectivity.ReceiverId)) AS SenderPhone,
IF(ReceiverId = 1,
(SELECT company
FROM users
WHERE users.id = contacts_connectivity.SenderId),
(SELECT company
FROM users
WHERE users.id = contacts_connectivity.ReceiverId)) AS SenderCompany,
ModulesId,
(SELECT ModuleName
FROM modules
WHERE ModuleId = contacts_connectivity.ModulesId) AS ModuledName,
FROM_UNIXTIME(AddedDate, "%m/%d/%Y") AddedDate
FROM contacts_connectivity
WHERE (ReceiverId = 1
OR SenderId = 1)
AND (ReceiverId
OR SenderId NOT IN
(SELECT (ReceiverId
OR SenderId)
FROM contacts_connectivity
WHERE (ReceiverId
OR SenderId) = 1))
I noticed that My query doesn't return any array at all. I already tried to write the where statement like this-
$this->datatables->where("(ReceiverId = ' . $id . ' OR SenderId = ' . $id . ')");
$this->datatables->where(" AND (ReceiverId OR SenderId NOT IN (SELECT (ReceiverId OR SenderId) FROM contacts_connectivity WHERE (ReceiverId OR SenderId) = ' . $id . '");
What am I doing wrong?
You invalid use parameters. Try something like this:
$this->datatables->where("(ReceiverId=$id OR SenderId=$id )");
Second string should be fixed too the same way. Also check that your $id param properly escaped.
I have a left join select statement that I want to select specific columns based on the value of a variable that has either a '0' or a '1'. I thought I could implement this using CASE, but I don't seem to be having any luck.
If $variable contains a '0' I want my select statement to retrieve only users.user, table2.total1 and table2.total2, but if the $variable contains a '1' I want to select only users.user and table2.total.
$value = '1';
$conn->query(
"select users.user, table2.total, table2.total1, table2.total2
FROM users
LEFT JOIN table2 on users.user = table2.total AND $table2.date = CURDATE()
WHERE users.user = 'marketing' OR users.user = 'sales'
");
Does anyone have any ideas? Thanks.
You don't need to do this with sql, if in fact the query differ only by the select clause you could use a conditional and a variable:
$value = '1';
if($variable == '1') {
$select = "select users.user, table2.total1, table2.total2 ";
}
else {
$select = "select users.user, table2.total ";
}
$conn->query(
$select . "
FROM users
LEFT JOIN table2 on users.user = table2.total AND $table2.date = CURDATE()
WHERE users.user = 'marketing' OR users.user = 'sales'
");
I need a MySQL query (Standard SQL) to get article information from drupal 7 (title, image, body, publish date). Only last revision.
Sounds like you want an EntityFieldQuery:
$query = new EntityFieldQuery;
$results = $query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'article')
->execute();
if (isset($results['node'])) {
$nodes = node_load_multiple(array_keys($results['node']));
foreach ($nodes as $node) {
$created = $node->created;
$image_uri = $node->field_image[$node->language][0]['uri'];
// ...
}
}
Ok, I found a solution:
SELECT node.title, body.body_value, FROM_UNIXTIME(node.created) AS Created, file_managed.uri AS image
FROM node INNER JOIN
field_data_body AS body ON node.nid = body.entity_id INNER JOIN
file_usage ON file_usage.id = node.nid INNER JOIN
file_managed ON file_usage.fid = file_managed.fid
WHERE (node.type = 'article')