The following is my scenario
i have the following databases
employee(e_no,e_name,e_contact,e_mail)
project(p_no,e_no,p_name)
project_assigned(id,pno,eno)
task(id,pno,tasks)
Now I have two particular files called employee_data_sheet.php and addtask.php
I want to accomplish the following:
When I post a task to addtask.php I want to view which employee has got the task in which project in employee_data_sheet.php
Following is the code snippet:
(my addtask.php is working fine error in employee_data_sheet)
$query34="SELECT p_no FROM project_assign WHERE e_no='$empno'";
$res34= mysql_query($query34);
while($row34= mysql_fetch_array($res34))
{
$pnum=array();
$pnum=$row34['p_no'];
$query35="SELECT p_name FROM project WHERE p_no='$pnum'";
$res35=mysql_query($query35);
while($row35= mysql_fetch_array($res35))
{
$prname1=array();
$prname1=$row35['p_name'];
}
}
The problem is that I am not able to store the result of query to an array with comma separation.
Please help me to store the result of a query to an array with comma separation
You are using deprecated database access functions. However I believe you are trying to do this
$prname1=array();
while($row35= mysql_fetch_array($res35))
{
$prname1[]=$row35['p_name'];
}
$string = implode(",", $prname1);
Related
I've been spent hours trying to figure out how I'm supposed to get around this error in my scenario. I'm trying to run a few queries in sequence.
I have read: codeigniter : Commands out of sync; you can't run this command now, however I am not able to update /system/database/drivers/mysqli/mysqli_result.php
I've tried:
$this->db->reset_query();
$this->db->close();
$this->db->initialize();
$this->db->reconnect();
mysqli_next_result( $this->db->conn_id );
$query->free_result();
But they either give me the same error, or different errors which I will detail in the comments of my code.
The way my code is organized- I have a make_query method that takes a bunch of search options and figures out which tables to join and fields to search based on those. Sometimes, I just want to count results, sometimes I want all of the resulting data, sometimes I just want distinct values for certain fields or to group by certain fields. So I call make_query with my options, and then decide what to select afterwards. I have also been saving my query text to report, so that users can see what query is being run.
One interesting thing I have noted is that when I have no options set (ie there are no WHERE clauses in my SQL), I do not get this error and the multiple queries are able to run with no problem!
Here is my code:
//Get rows from tblPlots based on criteria set in options array
public function get_plot_data($options=array()){
$this->db->save_queries = TRUE;
log_message('debug','before make query 1 get plot data');
$this->make_query($options,'plot');
log_message('debug','after make query 1 get plot data');
$this->db->distinct();
//Now, set select to return only requested fields
if (!empty($options['fields']) and $options['fields']!="all" ){
//Because other tables could be joined, add table name to each select
array_walk($options['fields'], function(&$value, $key) { $value = 'tblPlots.'.$value;} );
$this->db->select($options['fields']);
}
if (!empty($options['limit']) and $options['limit']>0){
$this->db->limit($options['limit'], $options['offset']);
}
//Get the resulting data
$result=$this->db->get('tblProgram')->result();
$query_text = $this->db->last_query(); //tried removing this but didn't help
log_message('debug','query text '.$query_text);
$this->db->save_queries = FALSE;
//get the number of rows
//$this->db->reset_query();
//$this->db->close();
//$this->db->initialize();
//$this->db->reconnect();
//mysqli_next_result( $this->db->conn_id );
//$this->db->free_result(); //Call to undefined method CI_DB_mysqli_driver::free_result()
//$result->free_result(); //Call to a member function free_result() on array
log_message('debug','before make query 2');
$this->make_query($options,"plot");
$this->db->select('pkProgramID');
log_message('debug','before count results');
//this is where my code errors out trying to do the next step:
$count=$this->db->count_all_results('tblProgram');
}
I'm not including the code for make_query because it is long and calls other functions, but it runs successfully the first time and outputs the SQL query I would expect. If it would be helpful, I can include that code as well.
I'm not sure how to correctly call free_result() given that the CI documentation only gives an example using query('SQL QUERY') and I am building the query using Active Record, so I'm not sure how to use free result in my scenario? Maybe that's the issue?
$query2 = $this->db->query('SELECT name FROM some_table');
$query2->free_result();
Thank you for any help!
I ended up posting on a CI forum that suggested https://www.youtube.com/watch?v=yPiBhg6r5B0 which worked! Also I ended up having to join my tables differently to avoid timeouts (I think I was having trouble because I was using AJAX to call many queries asynchronously and one of them was timing out). Hope this helps someone!
First post on stackoverflow. I have been following this site for a long time, and usually find what im looking for. But this has me perplexed.
Let me set the stage. I am developing a web driven program. I have Wordpress, with the Divi theme from Elegant Themes. and I am using shortcodes to insert into the modules. I am a newbie (this says it all.)
Here is my problem. I have run a wpdb query that returns a single row of results.
$editresult = $wpdb->get_results ("SELECT `serialnumber`, `batttype`, `cells`, `fullvolts` FROM listbattery WHERE serialnumber = '$serialnumber'", ARRAY_A);
When I vardump this, i get the following.
array(1) {[0]=>array(4) {["serialnumber"]=>string(10)"battery #2" ["batttype"]=>string(5) "NiCad" ["cells"]=>string(1) "8"["fullvolts"]=>string(6)"12.125"}}
So with that being said, I know that the query is working fine. I know that I am receiving the information. What I can't for the life of me figure out, is how to turn the results from each column into individual variables, so that I can insert each variable randomly throughout my page.
I have tried about 8 different methods so far. I hope you guys can help! thanks!!!
You can loop through the result:
foreach($editresult as $result) {
$serialnumber = $result['serialnumber'];
$batttype = $result['batttype'];
$cells = $result['cells'];
$fullvolts = $result['fullvolts'];
}
If only one row is expected to be returned, you can do the following
$editresult = $wpdb->get_row("SELECT `serialnumber`, `batttype`, `cells`, `fullvolts` FROM listbattery WHERE serialnumber = '$serialnumber'", ARRAY_A);
Then you can access returned values like
$editresult['serialnumber']
$editresult['batttype']
$editresult['cells']
$editresult['fullvolts']
or if you change ARRAY_A to OBJECT, you will be able to access these values like so
$editresult->serialnumber
$editresult->batttype
$editresult->cells
$editresult->fullvolts
There is no need in get_results and foreach like shown in #nanodanger's answer if you always expect to get only 1 row
I found someone do it on wireless from github. I want to insert another column using arduino via ethernet shield to php apache server into mysql database. In the arduino sketch, I add another coloumndata as shown below:
String yourdatacolumn1="yourdata1=";
String yourdata1;
int yourarduinodata1 = 55555555;
yourdata1 = yourdatacolumn1 + yourarduinodata1;
In the mysql table, I have a column call yourdata1. and in the insert_sql.php. I have modified the code a little bit. But when I run the sketch. On php side, no data is recorded. If I get rid of the new column yourdata1. that works. But when inserting another column , it doesn't work. (I have also created column into mysql called yourdata1)
foreach ($_REQUEST as $key => $value)
{
if ($key == "yourdata") {
$yourdata = $value;
}
if ($key =="yourdata1"){
$yourdata1=$value;
}
}
There are three steps to achieve what you want:
Create a column named yourdata1 in your mysql table: (You have already done as per your question but I am reiterating)I am guessing you followed the steps mentioned in the README of this particular github project to create the columns: yourdata and timestamp. For your problem, you need to create another column, yourdata1 similarly. Or alternatively you can login to the mysql client and issue the following command:
ALTER TABLE your-table-name ADD COLUMN yourdata1 varchar(100)
Edit your insert_mysql.php to start adding data to your new column: Apart from the edit you have shown in the question, you also need to edit the line 23 of the script which inserts data into the table. The syntax for doing so can be found here.I am mentioning it here so that you could use it
INSERT INTO your-table-name (yourdata,yourdata1,timestamp) VALUES ($yourdata,$yourdata1,now());
(Make sure that both $yourdata and $yourdata1 php variables exist, you can initialize both of them as empty, $yourdata='' and $yourdata1='' just before the for loop)
You need to make sure that arduino passes this new yourdata1 to your insert_php script. I guess you have already figured it out. You just need to add another client.println() statement.
I'm working on a web application project which displays a list of results to the customer, with pictures, description and some other info. I want to optimize the routine responsible for displaying the results.
Let's say that we have a total of 200 results (products). For each one of them a function is called (let's call it DisplayResults), that fetches some pictures, description, and some other stuff.
DisplayResults currently is within a foreach loop, so as I already mentioned, for each one of the results we have a new function call.
I'd like to ask: how can I optimize this sequence? If I fetch all the info needed to display with a function OUTSIDE the foreach loop (for all the results at once), will I see some difference?
Thank you in advance
EDIT: I'm already using pagination. The data have been already fetched from the database and are stored in an array, so no need for SQL optimization at this point, in my opinion...
EDIT #2: This is how foreach-loop looks like:
foreach ($total['product_data'] as $product_data) {
$tags['$$searchProductList'] .= DisplayResults($product_data);
}
And this is how DisplayResults looks like:
function DisplayResults ($data) {
$tags['$$Description'] = substr($data['description'],0,70)." ...";
$tags['$$FeaturesList'] = getFeatures($data['features']);
$tags['$$PhotosList'] = getPhotos($data['photos']);
$tags['$$Offers'] = getOffers($data['offers']);
.
.
.
.
}
The $data variable contains all the information fetched from the DB for the current result.
I have a mySQL database table setup called site.
Rather than qet the column headings to produce a PHP object of values I want to produce data from the rows.
TABLE: site
:::field:::::value:::::
url www.example.com
name example site
etc Example Etcetera
So I want to be able to get this information from the server by calling the column name I want and the row I am after. I want to do this for all fields in site as I don't want to do multiple calls for the various different rows in site; I'd rather store all the information from the beginning in the object:
eg. <? echo $site['url']; ?>
I created this put it appears to be causing an error:.
$sql = "SELECT * FROM `site`";
$site[];
foreach($sodh->query($sql) as $sitefield){
$site[$sitefield['field']] = $sitefield['value'];
}
Obviously I've missed something. ¿Any idea as to what?
$site[];
should be
$site = array();