Add another data coloumn from arduino sketch into mysql database - php

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.

Related

Codeigniter unknown column in where clause?

I am trying to create an update function in Codeigniter using a dropdown menu. Unfortunately, when I click submit I get an error that there is an "Unknown column 'stages.id' in my where clause. I've used the stages.id in other parts of my code to update other fields, and am not sure why this is happening here. Any assistance or thoughts on why this might be happening would be greatly appreciated!
function update_stage_name($id){
$stage_name=array(
'stage_name'=>$this->input->post('stage_name')
);
$this->db->where('stages.id', $id);
$this->db->update('stage_names', $stage_name);
}
This is what the database query looks like according to the error:
UPDATE `stage_names` SET `stage_name` = 'Holly' WHERE `stages`.`id` = '264'
I have several tables, and stages is sort of the main one. Stages each only have one stage_name but a stage_name can belong to multiple stages. Stage_name does not have a foreign key from stages, but stages has a stage_name_id.
EDIT: So it seems the issue was actually that I wanted to update the stages table, and not the stage_names table. But the problem is that the stages table has a stage_name_id but the input from my form is for stage_name. How do I get to the id in the same method? I'm still new to active record and codeigniter and i'm not sure how to proceed.
As the error says you dont have a column that can be referenced using stages.id. Mainly because its the wrong table..
Try..
UPDATE `stage_names` SET `stage_name` = 'Holly' WHERE `stage_names`.`id` = '264'
//Note changed the table name in the where
If you need to use the stages.id and (as you say in your comment) you dont have an FK then you will need to create one as the database isnt smart enough to guess it. So once you have a field in stages_names called stage_id (or similar) you can do..
UPDATE `stage_names` SET `stage_name` = 'Holly' WHERE `stage_id` = '264'
Try this:
function update_stage_name($id) {
$stage_name=array(
'stage_name'=> $this->input->post('stage_name')
);
$this->db->where('stage_names.id', (int)$id);
$this->db->update('stage_names', $stage_name);
}

I am trying to synchronise a Sage Line 50 Access database with Mysql via ODBC

I am trying to source the structure and data from a sage line 50 database but am having trouble with my update/create script.
Basically, I am writing a tool so that the local intranet site can display data sourced from sage to normal employees without using up a sage login (orders due in/stock levels etc). I am having trouble with this because it seems that the Sage50 database was developed by total morons. There are no Unique keys in this database, or, more accurately, very very few. The structure is really old school you can find the structure on pastebin HERE (bit too big for here). You'll notice that there are some tables with 300+ columns, which I think is a bit stupid. However, I have to work with this and so I need a solution.
There are a few problems syncing that I have encountered. Primarily it's the fact ODBC can't limit statements to 1 row so I can check data type, and secondly, with there being no IDs, I can't check if it's a duplicate when doing the insert. At the moment, this is what I have:
$rConn = odbc_connect("SageLine50", "user", "password");
if ($rConn == 0) {
die('Unable to connect to the Sage Line 50 V12 ODBC datasource.');
}
$result = odbc_tables($rConn);
$tables = array();
while (odbc_fetch_row($result)){
if(odbc_result($result,"TABLE_TYPE")=="TABLE") {
$tables[odbc_result($result,"TABLE_NAME")] = array();
}
}
This produces the first level of the list you see on pastebin.
A foreach statement is then run to produce the next level with the columns within the table
foreach($tables as $k=> $v) {
$query = "SELECT * FROM ".$k;
$rRes = odbc_exec($rConn,$query);
$rFields = odbc_num_fields ($rRes);
$i = 1;
while($i <= $rFields) {
$tables[$k][] = odbc_field_name($rRes, $i);
$i++;
}
CreateTableandRows($k,$tables[$k]);
}
At the moment, I then have a bodged together function to create each table (not that I like the way it does it).
Because I can't automatically grab back one row (or a few rows), to check the type of data with get_type() to then automatically set the row type, it means the only way I can figure out to do this is to set the row type as text and then change them retrospectively based upon a Mysql query.
Here is the function that's called for the table creation after the foreach above.
function CreateTableandRows($table,$rows) {
$db = array(
"host" => '10.0.0.200',
"user" => 'user',
"pass" => 'password',
"table" => 'ccl_sagedata'
);
$DB = new Database($db);
$LocSQL = "CREATE TABLE IF NOT EXISTS `".$table."` (
`id` int(11) unsigned NOT NULL auto_increment,
PRIMARY KEY (`id`),";
foreach($rows as $k=>$v) {
$LocSQL .= "
".$v." TEXT NOT NULL default '',";
}
$LocSQL = rtrim($LocSQL, ',');
$LocSQL .= "
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
echo '<pre>'.$LocSQL.'</pre>';
$DB->query($LocSQL);
}
I then need/want a function that takes each table at a time and synchronizes the data to the ccl_sagedata database. However, it needs to make sure it's not inserting duplicates, i.e. this script will be run to sync the sage database at the start or end of each day and without ID numbers INSERT REPLACE won't work. I am obviously implementing auto inc primary ID's for each new table in the ccl_sagedata db. But I needs to be able to reference something static in each table that I can identify through ODBC (I hope that makes sense). In my current function, it has to call the mysql database for each row on the sage database and see if there is a matching row.
function InsertDataFromSage($ODBCTable) {
$rConn = odbc_connect("SageLine50", "user", "pass");
$query = "SELECT * FROM ".$ODBCTable;
$rRes = odbc_exec($rConn,$query);
$rFields = odbc_num_fields ($rRes);
while( $row = odbc_fetch_array($rRes) ) {
$result[] = $row;
}
$DB = new Database($db);
foreach($result as $k => $v) {
$CHECKEXISTS = "SELECT * FROM ".$ODBCTable." WHERE";
$DB->query($CHECKEXISTS);
// HERE WOULD BE A PART THAT PUTS DATA INTO THE DATABASE IF IT DOESN'T ALREADY EXIST
}
}
The only other thing I can think to note is that the 'new Database' class is simply just a functionalised standard mysqli database class. It's not something I'm having problems with.
So to re-cap.
I am trying to create a synchronization script that creates (if not exists) tables within a mysql database and then imports/syncs the data.
ODBC Can't limit the output so I can't figure out the data types in the columns automatically (can't do it manually because it's a massive db with 80+ tables
I can't figure out how to stop the script creating duplicates because there are no IDs in the sage source database.
For those of you not in the UK, Sage is a useless accounting package that runs on water and coal.
The Sage database only provides data, it doesn't allow you to input data outside of csv files in the actual program.
I know this is a bit late but Im already doing the same thing but with MS SQL.
Ive used a DTS package that truncates known copies of the tables (ie AUDIT_JOURNAL) and then copies everything in daily.
I also hit a bit of a wall trying to handle updates of these tables hence the truncate and re-create. Sync time is seconds so its not a bad option. It may be a bit of a ball ache but I say design your sync tables manually.
As you rightly point out, sage is not very friendly to being poked, so id say don't try to sync it all either.
Presumably you'll need reports to present to users but you don't need that much to do this. I sync COMPANY,AUDIT_JOURNAL, AUDIT_USAGE, CAT_TITLE,CAT_TITLE_CUS, CHART_LIST,CHART_LIST_CUS, BANK,CATEGORY,CATEGORY_CUS,DEPARTMENT, NOMINAL_LEDGER,PURCHASE_LEDGER,SALES_LEDGER.
This allows recreation of all the main reports (balance sheet, trial balance, supplier balances, etc all with drill down). If you need more help this late on let me know. I have a web app called MIS that you could install locally but the sync is a combo of ODBC and the DTS.
OK you do not need to create a synchronisation script you can query ODBC in real time you can even do joins like you do in SQL to retrieve data from multiple tables. The only thing you cannot do is write data back to sage.

how to store values to array from mysql database in php

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);

PHP PDO get all row names as objects containing value

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();

sync two tables from two different PostgreSQL databases

I have two tables from two different databases and I want a php function to syncronize the data, so that the table number 2 can always verify the content on the table 1 and update it's information.
Anyone has one example on how to do that? Thanks in advance.
D.S.'s answer will get the job done.
You could also look into setting up an after insert/update trigger and using dblink. That way, they'll be kept in sync without you needing to worry about it in PHP.
As a side note, be very wary of what might happen on DB errors in either case. You can end up losing sync with either solution when DB errors occur, because the transactions will be autonomous.
this example will connect to both the databases, and for each of the first db's authors will update the destination db's author with the same id.
Of course you have to set up any necessary check, search and other details before perform and update (or an insert or replace if you prefer), but it fully depends on what you're going to do :)
<?php
if (false !== ($con1 = pg_connect("your source connection string"))) {
if (false !== ($con2 = pg_connect("your dest connection string"))) {
if (false !== ($result = pg_query($con1, "SELECT id, author, email FROM authors"))) {
while (false !== ($row = pg_fetch_assoc($result))) {
pg_query($con2, "UPDATE authors SET email=".pg_escape_string($con2, $row['email']).
'WHERE id='.pg_escape_string($con2, $row['id']));
}
pg_free_result($result);
}
pg_close($con2);
}
pg_close($con1);
}
?>
I hope it was useful. Please feel free to ask any question about it. Enjoy! :)
Create trigger on Insert, Update, Delete. When trigger procedure called store all the changes done in operation(Insert, update or delete) into database table(let's call this sync_table). Run some script which will copy data from sync_table to another database table. sync_table will store what data modified, inserted and deleted.

Categories