As you can see inside my while loop i declare i variable $TYPES..
This is my first Query
$first = "SELECT DISTINCT DATE_FORMAT(z.DatePaid,'%M %d, %Y') AS Paid
FROM tblStudPayments z
INNER JOIN tblPersonalData p ON p.StudNo=z.StudNo
WHERE z.StudNo=p.StudNo AND z.SY='".$SY."' AND z.Sem='".$Sem."' ORDER BY z.DatePaid;";
$fs = safe_query($first);
$numrows = mysql_num_rows($fs);
if($numrows>0)
{
while($dataf = mysql_fetch_assoc($fs))
{
$types =$dataf['Paid'];
}
}
I wanted to pass the value of $TYPES to my second Query
And This is my Second Query
$sql="SELECT DISTINCT p.StudNo, p.LName, p.FName, p.MName, p.NName, c.Description, p.YearLevel, d.Status,
'".$types."' AS DateEnlisted,
FROM tblPersonalData p
INNER JOIN tblStudPayments sp ON sp.StudNo=p.StudNo AND sp.SY='".$SY."' AND sp.Sem='".$Sem."'
INNER JOIN tblStatusHistory d ON d.StudNo=sp.StudNo AND d.SY=sp.SY AND d.Sem=sp.Sem
INNER JOIN tblCourses c ON c.CourseCode=d.CourseCode AND c.HSOrCollege='".$dType."'
INNER JOIN tblUserAcct u ON u.UserName=p.StudNo
";
$sql.=" HAVING DateEnlisted = '".$a['DateEnrolled']."' ";
$sql.=" ORDER BY p.StudNo ASC;";
At the bottom of SELECT Statement you can see my variable $TYPES i get it from my first query.
The problem is it doesn't get all the data.. it only get the last data from mysql
Thanks in Advance..
[Image Suggested by Kundu Updated][1]
[Image Suggested by Kundu echo $sql][2]
i try one query but the loading of data takes 10-15MIN.
Put the 2nd query inside the 1st query while loop
In your while loop you put the value in a variable & evry time this variable is updated.So at the end of the loop it will return last type value. Either put it in a array, so it will store all the value.But that case you need to convert the array into string when you put it in 2nd query.Also make sure when you put value in $type it should be unique.
update your code like this:
$types=array();
while($dataf = mysql_fetch_assoc($fs))
{
$types[] = $dataf['Paid'];
}
$allTypes = implode(",`", array_unique($types));
Now in your query:
$sql="SELECT DISTINCT p.StudNo, p.LName, p.FName, p.MName, p.NName, c.Description, p.YearLevel, d.Status,
`".$allTypes."` AS DateEnlisted,
FROM tblPersonalData p
INNER JOIN tblStudPayments sp ON sp.StudNo=p.StudNo AND sp.SY='".$SY."' AND sp.Sem='".$Sem."'
INNER JOIN tblStatusHistory d ON d.StudNo=sp.StudNo AND d.SY=sp.SY AND d.Sem=sp.Sem
INNER JOIN tblCourses c ON c.CourseCode=d.CourseCode AND c.HSOrCollege='".$dType."'
INNER JOIN tblUserAcct u ON u.UserName=p.StudNo
";
Related
function getAllReferrals(){
$sql = "(SELECT r.referral_date,c.lastname,c.middlename,c.firstname,c.gender,r.presenting_problem,e.employee_nickname
AS nickname FROM CLIENT c INNER JOIN referral1 r ON c.referral_id = r.referral1_id INNER JOIN assign_psychotherapist ap
ON ap.a_referral_id = c.referral_id INNER JOIN employee e ON ap.a_psychotherapist_id = e.empid WHERE r.referral_status ='Assigned'
OR r.referral_status ='Accepted' ORDER BY referral_date DESC ) UNION ALL (SELECT r.referral_date,c.lastname,c.middlename,c.firstname,
c.gender,r.presenting_problem,v.volunteer_nickname AS nickname FROM CLIENT c INNER JOIN referral1 r ON c.referral_id = r.referral1_id
INNER JOIN assignvolunteer av ON av.Vreferralid = c.referral_id INNER JOIN volunteer v ON av.Vvolunteerid = v.volid
WHERE r.referral_status ='Assigned' OR r.referral_status ='Accepted' ORDER BY referral_date DESC )";
$query = $this->db->query($sql);
if ($query->num_rows() > 0){
return $query->result();
}else{
return NULL;
}
}
Message: Invalid argument supplied for foreach() -> having this error
With the given information let me answer the question.
how can i pass this query to my controller in CodeIgniter
Understand that query isn't passed to the controller from the model but what's passed is data. what you have to do is returned the data from model to controller and set the data into the $data variable in the controller which is then passed to the view.
Adding more you are getting above error because there is no data(result array is empty) in the array. Make sure that you query returns array of data as well. Try executing it manually in MySQL and see the result.
Try result_array() which returns the query result as a pure array, or an empty array when no result is produced.
Read more
First try execute your query manuly, and check get any result you can try your program. Invalid argument supplied for foreach() -> having this error this type of error occurred your result array may be empty.
first create a model . and paste this code on model like your model name is ex_model.php
function getAllReferrals(){
$sql = "(SELECT r.referral_date,c.lastname,c.middlename,c.firstname,c.gender,r.presenting_problem,e.employee_nickname
AS nickname FROM CLIENT c INNER JOIN referral1 r ON c.referral_id = r.referral1_id INNER JOIN assign_psychotherapist ap
ON ap.a_referral_id = c.referral_id INNER JOIN employee e ON ap.a_psychotherapist_id = e.empid WHERE r.referral_status ='Assigned'
OR r.referral_status ='Accepted' ORDER BY referral_date DESC ) UNION ALL (SELECT r.referral_date,c.lastname,c.middlename,c.firstname,
c.gender,r.presenting_problem,v.volunteer_nickname AS nickname FROM CLIENT c INNER JOIN referral1 r ON c.referral_id = r.referral1_id
INNER JOIN assignvolunteer av ON av.Vreferralid = c.referral_id INNER JOIN volunteer v ON av.Vvolunteerid = v.volid
WHERE r.referral_status ='Assigned' OR r.referral_status ='Accepted' ORDER BY referral_date DESC )";
$query = $this->db->query($sql);
if ($query->num_rows() > 0){
return $query->result();
}else{
return NULL;
}
and create controller and get add this code like
<?php
public function reffers(){
$this->load->model('ex_model');
$data['refferdata'] = $this->ex_model->getAllReferrals();
$this->load->view('reffer');
}
?>
now create a view name reffer.php and get all data from foreach loop like
<?php
foreach($refferdata as $r)
{
echo $r->r.referral_date.<br>;
echo $r->c.lastname.<br>;
// and so on like this in view
}
?>
I'm working with a mysql query to select data from multiple tables using LEFT OUTER JOIN. Now i get the following error when i exequte the query:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'wg.werkbon_global_id = wk.werkbon_klant_globalid LEFT OUTER
JOIN users AS u' at line 16
Only the problem is that i can't find out what's wrong with my query.
PHP Query:
$query = '
SELECT
wg.werkbon_global_id AS id,
wg.werkbon_global_status AS status,
wg.werkbon_global_date_lastedit AS date,
usr.user_firstname AS monteur_vn,
usr.user_insertion AS monteur_tv,
usr.user_lastname AS monteur_an,
wg.werkbon_global_type AS type,
wg.werkbon_global_layout AS layout,
wg.werkbon_global_werkzaamheden AS werkzaamheden,
wg.werkbon_global_opmerkingen AS opmerkingen,
wk.werkbon_klant_nummer AS klantnr
FROM
werkbon_klant AS wk
LEFT OUTER JOIN werkbon_global AS wg
wg.werkbon_global_id = wk.werkbon_klant_globalid
LEFT OUTER JOIN users AS usr
usr.user_id = wg.werkbon_global_monteur_finish
WHERE
wk.werkbon_klant_nummer = '.$db->Quote($klantid).'
ORDER BY id ASC;
$result = $db->loadAssoc($query);
I think my problem has something todo with left outer join but what?
You are missing the ON operator in your joins!
The correct syntax for a join is:
SELECT * FROM x LEFT JOIN y ON condition WHERE...
$query = "
SELECT
wg.werkbon_global_id AS id,
wg.werkbon_global_status AS status,
wg.werkbon_global_date_lastedit AS date,
usr.user_firstname AS monteur_vn,
usr.user_insertion AS monteur_tv,
usr.user_lastname AS monteur_an,
wg.werkbon_global_type AS type,
wg.werkbon_global_layout AS layout,
wg.werkbon_global_werkzaamheden AS werkzaamheden,
wg.werkbon_global_opmerkingen AS opmerkingen,
wk.werkbon_klant_nummer AS klantnr
FROM
werkbon_klant AS wk
LEFT OUTER JOIN werkbon_global AS wg
wg.werkbon_global_id = wk.werkbon_klant_globalid
LEFT OUTER JOIN users AS usr
usr.user_id = wg.werkbon_global_monteur_finish
WHERE
wk.werkbon_klant_nummer = '.$db->Quote($klantid).'
ORDER BY id ASC";
$result = $db->loadAssoc($query);
Make sure there isn't missing quote
Problem soved thanks to arkascha
The fixed query is now:
$query = '
SELECT
wg.werkbon_global_id AS id,
wg.werkbon_global_status AS status,
wg.werkbon_global_date_lastedit AS date,
usr.user_firstname AS monteur_vn,
usr.user_insertion AS monteur_tv,
usr.user_lastname AS monteur_an,
wg.werkbon_global_type AS type,
wg.werkbon_global_layout AS layout,
wg.werkbon_global_werkzaamheden AS werkzaamheden,
wg.werkbon_global_opmerkingen AS opmerkingen,
wk.werkbon_klant_nummer AS klantnr
FROM
werkbon_klant AS wk
LEFT OUTER JOIN werkbon_global AS wg ON
wg.werkbon_global_id = wk.werkbon_klant_globalid
LEFT OUTER JOIN users AS usr ON
usr.user_id = wg.werkbon_global_monteur_finish
WHERE
wk.werkbon_klant_nummer = '.$db->Quote($klantid).'
ORDER BY id ASC';
$result = $db->loadAssoc($query);
#fred i don't need to add quotes by column names. You only need to add quotes by string/blob values.
#johny my $db->Quote() function will add qoutes automaticly. I don't need to add them and put everything in quote's.
Thanks all for help.
I have a query like this:
$query = "SELECT a.sender_id,
a.recipient_id,
a.form_id, due_date,
a.completed,
f.name,
p.priority,
u.first_name,
u.last_name,
SUM(a.completed) as completed_sum
FROM form_assignments a
JOIN forms f ON (form_id = f.id)
JOIN users u ON (sender_id = u.id)
JOIN priorities p ON (priority_id = p.id)
WHERE recipient_id = '{$_SESSION['user_id']}'
ORDER BY due_date ASC";
And a while loop like this:
$assignment_count = (mysqli_num_rows($result));
$assignments_row = array();
while ($row = mysqli_fetch_array($result)) {
$sender = $row['first_name'] . ' ' . $row['last_name'];
$form_id = $row['form_id'];
$form_name = $row['name'];
$priority = $row['priority'];
$due_date = date('m/d/Y', strtotime($row['due_date']));
$completed = $row['completed'];
$not_done = $assignment_count - $row['completed_sum'];
}
And it's only returning one row. It seems my SUM(a.completed) as completed_sum is causing the issues because the query worked fine before I added it, but I want to add up all the values in completed to use in my $not_done variable.
Can anyone help clarify what I'm doing wrong?
When you use an aggregate function like SUM, all the results will be aggregated into one row unless you use a GROUP BY clause to segregate them. But it looks to me like you don't need a SUM in the first place. Your loop is subtracting this value from a total, so you just need the value from each row -- when you subtract them all you'll have subtracted the total. So just select a.completed rather than SUM(a.completed).
For $not_done, you need to initialize it before the loop:
$not_done = $assignment_count;
Then during the loop you should do a running subtraction:
$not_done -= $row['completed'];
Try this query :
SELECT
a.sender_id,
a.recipient_id,
a.form_id,
a.due_date,
a.completed,
f.name,
p.priority,
u.first_name,
u.last_name,
b.completed as completed_sum
FROM
form_assignments AS a
LEFT JOIN (
SELECT form_id,SUM(completed) FROM form_assignments GROUP BY form_id
) AS b ON (a.form_id = b.form_id)
LEFT JOIN forms AS f ON (form_id = f.id)
LEFT JOIN users AS u ON (sender_id = u.id)
LEFT JOIN priorities AS p ON (priority_id = p.id)
WHERE
recipient_id = '{$_SESSION['user_id']}'
ORDER BY due_date ASC
I have the following code for selecting from multiple tables where the order number matches.
$orderNumber = $_GET['orderNumber'];
$sql = $db->prepare("
SELECT
*
from `KC_Orders`
INNER JOIN
`KC_Payments`
on KC_Orders.orderNumber = KC_Payments.orderNumber
INNER JOIN
`KC_OrderStatus`
on KC_Orders.orderNumber = KC_OrderStatus.orderNumber
INNER JOIN
`KC_Statuses`
on KC_OrderStatus.statusID = KC_Statuses.statusID
WHERE
orderNumber= :orderNumber");
$sql->execute(array(':orderNumber' => $orderNumber));
$orderInfo = $sql->fetchAll();
Now when I var_dump($orderInfo); it returns: array(0) { } What is wrong? All the tables include the same $orderNumber field within it. If I take the WHERE part out it works just fine except it returns every row not just one. (obviosly).
Please help us!
You need to specify what the OrderNumber is from, and the * is from (what table). So try this new code:
$sql = $db->prepare("SELECT KC_Orders.* from `KC_Orders` INNER JOIN `KC_Payments` on KC_Payments.orderNumber = KC_Orders.orderNumber INNER JOIN `KC_OrderStatus` on KC_OrderStatus.orderNumber = KC_Order.orderNumber INNER JOIN `KC_Statuses` on KC_Statuses.statusID = KC_OrderStatus.statusID WHERE KC_Orders.orderNumber= :orderNumber");
Ordernumber in where clause should have table prefix if it exists in multiple tables
Try this
$orderNumber = (int)$_GET['orderNumber'];
Then where :ordernumber is put $orderNumber
Then execute
I want to generate xml file from several tables in a MySQL database using PHP.
$query = "
SELECT
t.value,
d.value,
ty.value,
p.id,
p.ref,
p.is_renting,
p.price,
p.adress,
p.living_space,
p.rooms
FROM
jos_jea_properties p
INNER JOIN
jos_jea_towns t
ON
t.id = d.town_id
INNER JOIN
jos_jea_departments d
ON
d.id = p.department_id
INNER JOIN
jos_jea_types ty
ON
ty.id = p.type_id";
$result = mysql_query ($query, $link) or die("Could not complete database query");
But, it show me Could not complete database query, have you any idea?
There's something wrong in your query. Use mysql_error() to get the message returned by MySQL.
$result = mysql_query ($query, $link) or die("Could not complete database query: ".mysql_error()));
However, your query gets a lot of fields named value. Use aliases instead, otherwise they're getting replaced
$query = "SELECT t.value t_value, d.value d_value, ty.value ty_value,
p.id, p.ref, p.is_renting, p.price, p.adress,
p.living_space, p.rooms FROM jos_jea_properties
p INNER JOIN jos_jea_towns t ON t.id = d.town_id
INNER JOIN jos_jea_departments d ON d.id = p.department_id
INNER JOIN jos_jea_types ty ON ty.id = p.type_id";
Have you an idea about a code php How to generate a file xml for image since mysql database using php?