I have set up a query as such:
$query = 'SELECT SGC.sys_id, TBL.semester, SGC.bonus, SGC.exam, SGC.ca FROM SubjectGradeComponent AS SGC, ';
$query .= '(SELECT `sys_id`, `semester` FROM AcademicYearTerm AS AYT, SubjectYearTermLevel AS SYTL WHERE academic_year = "' . $academic_year . '" AND SYTL.subject_id = ' . $subject_id . ' AND SYTL.form_level = ' . $form_level. ' AND SYTL.yearTerm_id = AYT.yearTerm_id) AS TBL ';
$query .= 'WHERE SGC.sys_id = TBL.sys_id;';
However when I run the query, $mysql->query($query);it returns an empty result with 0 rows. Running the same query on phpmyadmin shows the desired result. I have looked around but do not understand the problem.
$mysql->error does not show any error message either
EDIT:
generated query is like this:
SELECT SGC.sys_id, TBL.semester, SGC.bonus, SGC.exam, SGC.ca FROM SubjectGradeComponent AS SGC, (SELECT `sys_id`, `semester` FROM AcademicYearTerm AS AYT, SubjectYearTermLevel AS SYTL WHERE academic_year = "2018-2019" AND SYTL.subject_id = 1 AND SYTL.form_level = 1 AND SYTL.yearTerm_id = AYT.yearTerm_id) AS TBL WHERE SGC.sys_id = TBL.sys_id;""
Question is where are the "" from?
Looks like you want a JOIN query instead.
You should also use prepared statement with placeholders ? instead of injecting values directly into the query.
$query = "SELECT SGC.sys_id,
AYT.semester,
SGC.bonus,
SGC.exam,
SGC.ca
FROM SubjectGradeComponent AS SGC
JOIN AcademicYearTerm AS AYT
ON SGC.sys_id = AYT.sys_id
JOIN SubjectYearTermLevel AS SYTL
ON SYTL.yearTerm_id = AYT.yearTerm_id
WHERE academic_year = ?
AND SYTL.subject_id = ?
AND SYTL.form_level = ?";
Related
I am new to Codeigniter 4 and trying to write SELECT UNION SQL Statements. According to the user guide in codeigniter 4, there is a second parameter in select() method that allows to write custom SQL Statements by setting it to false. Unfortunately it is not working. Can someone help please? Is there any alternate solution?
$query = "SELECT acnt_id, acnt_opbal as sum_amount
FROM accounts
WHERE acnt_id = '" . $acnt_id . "'
UNION
SELECT txn_acnt_id_dr as acnt_id, SUM(txn_amount_dr) as sum_amount
FROM transactions
WHERE txn_acnt_id_dr = '" . $acnt_id . "'
AND txn_date < '" . $fdate . "'
GROUP BY txn_acnt_id_dr
UNION
SELECT txn_acnt_id_cr as acnt_id, -SUM(txn_amount_cr) as sum_amount
FROM transactions
WHERE txn_acnt_id_cr = '" . $acnt_id . "'
AND txn_date < '" . $fdate . "'
GROUP BY txn_acnt_id_cr";
$result = $this->builder()->select($query, false)->get()->getResult();
According to CodeIgniter 4 Documentation ->getResult() should be used after building the query in order to produce your query
<?php
$builder = $db->table('mytable');
$query = $builder->get(); // Produces: SELECT * FROM mytable
foreach ($query->getResult() as $row) {
echo $row->title;
}
Addapting this to your code you'll get:
<?php
$builder = $this->builder()->select($query, false);
$query = $builder->get();
$result = $query->getResult();
Or make it all 1 line you can use Covariance and Contravariance:
$result = ($this->builder()->select($query, false)->get())->getResult();
I've got a pretty complex SQL statement and want to add a WHERE clause that only selects results WHERE the hidden column IS NOT '1', however it needs to relate to a specific user.
i.e If in table 1 hidden is 1 and userid is 1 I don't want to get this results. However as there is no record for user 2 in that table I want them to see it.
This is what I have managed to get working so far:
$where .= " AND uh.hidden IS NULL ";
However if I login as User 2 then I see the same results as user 1.
How do I make it so results are shown based on the user too?
SQL query:
$pdo = new PDO('mysql:host=localhost;dbname=myDB', 'root', 'root');
$select = 'SELECT tl.id,
tl.name,
tl.locale,
ROUND(AVG(pr.rating),0) AS rating ';
$from = ' FROM theList AS tl ';
$join = ' LEFT JOIN post_rating AS pr ON tl.id = pr.postid ';
$join2 = ' LEFT JOIN user_hidden_list AS uh ON uh.est_id = tl.id ';
$opts = isset($_POST['filterOpts']) ? $_POST['filterOpts'] : [''];
$where = ' WHERE 1 = 1 ';
if (in_array("pub", $opts)) {
$where .= " AND pub = 1";
}
if (in_array("bar", $opts)) {
$where .= " AND bar = 1";
}
$where = ' WHERE uh.hidden IS NULL ';
$group = ' GROUP BY tl.id, tl.name ';
$sql = $select . $from . $join . $join2 . $where . $group;
$statement = $pdo->prepare($sql);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo($json);
I have this little issue with sql that doesn't function. I have a query which grows based on specific critera. Here it is
$SELECT = ' SELECT * ';
$FROM = ' FROM a ';
$WHERE = ' WHERE TRUE ';
$how = ' GROUP By a_id ';
$result = mysqli_query($link, $SELECT . $FROM . $WHERE . $how );
This works but the second stage doesn't
$FROM .= " INNER JOIN b ON b_aid = a_id";
$WHERE .= " AND b_id = $id";
Specifically, the additional $WHERE . causes an undefined variable. If it is removed, I can see within the array the line I am after.
The normal query $result = mysqli_query($link, $SQL) works ok for the above;
Thanks
So I have to add a WHERE query to this plugin I'm using for a reporting feature on a WordPress site. I have no time to do anything but add in another column and filter by the values in that column as there is not that much data to manage each update. The default value for the column I added is zero but I'll add new entries to represent years new people are added. However, when I filter based on the column value the whole query breaks and doesn't show up. I have no idea why. Here is the section involving its set up query displaying results.
<?php
$sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . "presidentsreport_breakdown WHERE list_id = " . $atts['list_id'];
$total_breakdowns = $wpdb->get_var($sql);
$sql = "SELECT p.person_id, p.name, p.notes, p.school_year, b.breakdown_id, b.name as breakdown, b.description as breakdown_description FROM " . $wpdb->prefix . "presidentsreport_person p INNER JOIN " . $wpdb->prefix . "presidentsreport_breakdown b ON b.breakdown_id = p.breakdown_id INNER JOIN " . $wpdb->prefix . "presidentsreport_list l ON l.list_id = b.list_id";
$clean_where = " WHERE l.list_id = " . $atts['list_id'];
$where = "";
if($search != ''){
$where = " AND (p.name LIKE %s)";
$arg = '%' . $search . '%';
$args = array($arg);
}
$where = $wpdb->prepare($where, $args);
$order = " ORDER BY b.sort_order, b.breakdown_id, p.sort_name, p.name, p.person_id";
$results = $wpdb->get_results($sql . $clean_where . $where . $order);
?>
If I add anything in the variable $where it breaks the whole query. So if I add
<?php
$where = " WHERE p.school_year <= 2011";
?>
or
<?php
$where = " WHERE p.school_year = 0";
?>
Nothing will show up, For the last example if the default value is 0 everything should show up regardless. Thanks in advance for reading through!
Don't add WHERE to your variable. It is already assigned in $clean_where
$clean_where = " WHERE l.list_id = " . $atts['list_id'];
Here ------------^
You need to concatenate your addition parameters to the $where variable:
$where .= " AND p.school_year <= 2011";
There's no need of WHERE in where!
I need to grab a result from one query and pop it into another.
first query
$query = 'SELECT * FROM singleprop.jos_mls WHERE MSTMLSNO = ' . $mlsnum . ';';
$result = mysql_query($query);
$row = mysql_fetch_row($result);
second query
$aquery = 'SELECT * FROM singleprop.jos_agents WHERE AGTBRDIDMM = ' . $row[0] . ';';
$aresult = mysql_query($aquery);
$agent = mysql_fetch_row($aresult);
I know about JOIN, but don't know how to apply it with a 3rd table. Does my model have something to do with $this->?
The code looks good. You could write a query using join, which you are aware of. What is the question?
SELECT *
FROM singleprop.jos_mls as mls JOIN singleprop.jos_agents
ON singleprop.jos_mls.KEY = singleprop.jos_agents.KEY
WHERE mls.MSTMLSNO = $mlsnum
where KEY is the join key
OR
SELECT *
FROM singleprop.jos_agents
WHERE AGTBRDIDMM = (
SELECT COL_NAME
FROM singleprop.jos_mls
WHERE MSTMLSNO = ' . $mlsnum . '
)
where COL_NAME is the column name for AGTBRDIDMM in the first table