$sql = "SELECT * FROM
fbr.*, c.sample, ci.dob, ci.country, ci.sex
FROM `finalBloodReport` fbr ,`clients` c, `client_info` ci
WHERE fbr.sampleSerialNo = (CONCAT(c.resellerSerialId,'-',c.kitSerialNo) )
AND c.client_id = ci.id";
This is my query it works fine and gives the desired output,But since i am showing data in table i also want to get columns name only for this query to show them as table header .
I tried Something like this , but result is null.
$sql = "SHOW COLUMNS
fbr.*, c.sample, ci.dob, ci.country, ci.sex
FROM `finalBloodReport` fbr ,`clients` c, `client_info` ci
WHERE fbr.sampleSerialNo = (CONCAT(c.resellerSerialId,'-',c.kitSerialNo) )
AND c.client_id = ci.id";
Can anyone help ? I am not so good with mysql ; (
You need to retrieve column information after query is executed. This can be done by using mysqli_fetch_field()
You code should look like following
$result = mysqli_query($sql);
while($field = mysqli_fetch_field($result)) {
print $field->name . "\t";
}
while ($row = mysqli_fetch_row($result) {
print_r($row);
}
Related
Currently I had a table called crm_leads for my Leads page where I already have many records. Now for the same page I have created few new fields which is now going to be stored in another table called crm_leads_details. Now I'm storing all my POST values in an array format like so:
$main=array(
'priority'=>$this->input->post('priority'),
'lead_status'=>$this->input->post('lead_status'),
'comment'=>$this->input->post('comment'),
'sub_status'=>$this->input->post('sub_status'),
'lead_agent'=>$this->input->post('lead_agent'),
'emailopt'=>$this->input->post('emailopt')=='Y' ?'Y':'N',
// 'kitchen'=>$this->input->post('kitchen')
);
$loginid=$this->leads_model->update_lead($main,$id);
Now in my model I'm trying to use this statement to update both my tables but it does not work:
function update_lead($maindata,$id,$w=false)
{
if($w==true){
$cond=$id;
}
else{$cond['id']=$id;}
$sql = "UPDATE `crm_leads` LEFT JOIN `crm_leads_details` ON `crm_leads`.`id`=`crm_leads_details`.`leads_id`
SET ".$maindata." WHERE ".$cond;
$query = $this->db->query($sql);
print_r($this->db->last_query()); die();
return $query;
}
This gives the following output UPDATE crm_leads LEFT JOIN crm_leads_details ON crm_leads.id=crm_leads_details.leads_id SET Array WHERE Array
Additionally, since crm_leads_details is a new table there are currently no records in it so not sure if LEFT JOIN crm_leads_details ON crm_leads.id=crm_leads_details.leads_id will work here
Write the query like this
$sql = "UPDATE `crm_leads` LEFT JOIN `crm_leads_details` ON `crm_leads`.`id`=`crm_leads_details`.`leads_id`
SET ";
$i = 0;
foreach($maindata as $key=>$value) {
$i++;
$sql .= $key." = ".$value;
$sql .= ($i == count($maindata)) ? "" : " , " ;
}
$sql .= " WHERE ".$cond;
I have the following code
<?php
require_once('db_connection.php');
$return_arr = array();
$param = $_GET["term"];
$query = "SELECT *
FROM exp_weblog_data,exp_weblog_titles WHERE field_id_5
LIKE '%". $param ."%'
LIMIT 50";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
/* Retrieve and store in array the results of the query.*/
while ($row = $result->fetch_assoc()) {
$row_array['jItemCode'] = $row['field_id_5'];
$row_array['jItemDesc'] = $row['title'];
/* $row_array['jItemWholesale'] = $row['itemWholesale'];
$row_array['jItemRetail'] = $row['itemRetail'];
$row_array['jItemPrice'] = $row['itemPrice'];
$row_array['jQtyOnHand'] = $row['qtyOnHand'];*/
array_push( $return_arr, $row_array );
}
$result->free_result();
$mysqli->close();
/* Toss back results as json encoded array. */
echo json_encode($return_arr);
I have two tables. "exp_weblog_data" and "exp_weblog_titles". Each table has "entry_id". When I use "field_id_5" from "exp_weblog_data" to start the autosuggest I need to pull additional information from the "exp_weblog_titles" table
This is for an auto complete query. I need to pull related data from "title" in another table in the same database can someone please help I know the problem lies with my query but I have tried all kinds of syntax with JOINS and UNIONS and LEFT JOINS what have you. Can someone please help me
I got it to work this way
$query = "SELECT field_id_5, exp_weblog_titles.title, field_id_57
FROM exp_weblog_data, exp_weblog_titles
WHERE exp_weblog_titles.entry_id = exp_weblog_data.entry_id AND field_id_5
LIKE '%". $param ."%'
LIMIT 10";
Thanks for all the help guys!
Use a JOIN clause. You can with one request get related data from 2 or more tables.
http://dev.mysql.com/doc/refman/5.0/en/join.html
I have some PHP/MySQL code that pulls data from multiple different tables (using Inner Joins). It looks something like this:
$query = "SELECT * FROM table1 INNER JOIN table2 USING (key)";
$data = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($data)) {
echo $row[1];
}
So the code is simple enough, but what I want to do is echo the table each row is in inside of that while loop, since it could be in one of 2 tables.
I saw there was some old mysql functions like mysql_field_table and mysql_tablename that would do the trick, but they all seem to be deprecated.
Would appreciate any advice on how to accomplish this.
You could select the data with a special identifier for each table instead of using *
select table1.row1 as t1r1, table2.row1 as t2r1,..... from .....
And inside php you could look for the strings t1 and t2 and do stuff accordingly.
What you can do is echo more than one field from your result table (which hopefully now contains information from both the tables.
$query = "SELECT * FROM table1 INNER JOIN table2 USING (key)";
$data = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($data))
{
echo $row[1] . " " . $row[2];
}
...and then $row[3] etc...
Or access the column name/field by the column name or alias provided by AS.
$query = "SELECT * FROM table1 INNER JOIN table2 USING (key)";
$data = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_assoc($data))
{
echo $row["c_name1"] . " " . $row["c_name2"];
}
Where "c_name1" and "c_name2" are your column names.
Use an AS keyword to rename all columns.
$select_clause = '';
$tables = array('tblA', 'tblB');
foreach($tables as $tbl) {
mysql_query('SHOW COLUMNS FROM ' . $tbl);
while ($row = mysql_fetch_assoc())
$select_clause .= '`'.$tbl.'`.`'.$row['Field'].'` AS `'.$tbl
.'_'.$row['Field'].'`,';
}
$select_clause = substr($select_clause, 0, -1);
mysql_query('SELECT '.$select_clause.' FROM /*...*/ ');
Im trying to call all users from a database with the same interests as the current, logged in user on my website.
I have the following
// Get Session USER interest
$interestsquery = "SELECT `interest` FROM `user_interests` WHERE `user_id` = " . $usersClass->userID();
$result = mysql_query($interestsquery);
$interests = array();
while(list($interest) = mysql_fetch_array($result))
$interests[] = $interest;
$interest1 = $interests['1'];
$interest2 = $interests['2'];
$interest3 = $interests['0'];
// END INTERESTS
//USers with Same Interests
$interests_query = "SELECT * FROM produgg_users
join user_interests on produgg_users.id = user_interests.user_id
where interest = '$interest1' and produgg_users.id != '".$usersClass->userID()."'";
$interests_result = mysql_query($interests_query) or die(mysql_error());
if($interests_result != 0) {
while($interests_row = mysql_fetch_array($interests_result, MYSQL_ASSOC))
{
echo $interests_row['user_id'];
}
}
else
{
print "No users to display!";
}
//END SAME INTERESTS
which doesnt bring back any data, yet if I add (beneath //USers with Same Interests)
$interest1 = 'footy';
the interests_query seems to work, can anybody see where im going wrong?
My problem seems to lie here...
$interest1 = $interests['1'];
$interest2 = $interests['2'];
$interest3 = $interests['0'];
// END INTERESTS
//USers with Same Interests
$interest1 = 'footy';
If I manually assign a value to $interest variable it works, but i need to get use the value from the array above, does this make sense?
If your code brings back the correct data when you add $interest1 = 'footy'; line, that would imply that there is something wrong with the value of that variable when you don't. Have you tried var_dump($interest1); right under //Users with Same Interests line to see what kind of input you get from your interestsquery?
I would expect the var_dump to not return a valid string (since if it would, the query would work following the $interest1 = 'footy'; assumption), so you would have to look at what interestsquery returns wrong.
Looks like you querying user_id from user_interests as number, but from produgg_users as string. Maybe there's a problem
You can do it with one query:
$userID = mysql_real_escape_string($usersClass->userID());
$sql = "
SELECT * FROM user_interests AS ui1
JOIN LEFT user_interests AS ui2 ON ui1.id = ui2.id
JOIN LEFT produgg_users AS pu ON ui2.user_id = pu.id
WHERE ui.user_id = " . userID ;
I used this code to connect to a database and fetch results. This worked perfectly until i tried to work in another query to the images table to get associated images. I'm not very experienced with OO programming. So hopefully someone can see where ive gone wrong and help me out.
<?php
global $__CMS_CONN__;
$sql = "SELECT * FROM ecom_products";
$stmt = $__CMS_CONN__->prepare($sql);
$stmt->execute(array($id));
while ($row = $stmt->fetchObject()) {
$imagesql = "SELECT * FROM ecom_product_images where id = $row->id && where primaryImage = '1'";
$imagestmt = $__CMS_CONN__->prepare($sql);
$imagestmt->execute(array($id));
$imageName = $imagestmt->fetchObject();
echo ''.$row->productNm.''.$imageName;
}
?>
Modify your SQL query:
$imagesql = 'SELECT * FROM ecom_product_images where id = ' . $row->id . ' AND primaryImage = "1"';
Have you declared before $id variable ?
Have you got any errors with your code ?
If your primaryImage column is int type then skip apostrophes in query
primaryImage = 1
if enum then it is ok.
You need not OO programming, but SQL one.
SELECT p.*, imageName
FROM ecom_products p, ecom_product_images i
WHERE p.id = i.id AND primaryImage = 1;