im trying to write a query to insert data to my remote database table from a CRON file in my remote server. db connection happens through webservice and all that is working fine but the database does not get filled. following is the code that i have written, will anyone of you be able to find why it does not get write to the database table
$link = DbConnection::getInstance('TDC')->connectMysql()
$attributeKey = "12345";
$query_new = "INSERT INTO redtable (message) VALUES ('$attributeKey')";
$result = mysql_query($query_new, $link);
Related
I want to update mongodb document on basis of selected data from mysql table inside the same php file. Kindly share how this could be achieved.
If i don't misunderstand you:
<?php
$mysqlDb = connectToMysql(); // fake code to connect to mysql
$mongoDb = connectToMongod(); // fake code to connect to mongodb
$data = $mysqlDb->getData(); // fake code to get data from mysql
$mongoDb->update($data); // fake code to update mongodb
I am trying to send database table from one server to another database table in different server. Here is the PHP code I have tried so far, but to no avail:
<?php
$dblink1=mysql_connect('server', 'user', 'pass'); // connect server 1
mysql_select_db('db1_name',$dblink1); // select database 1
$dblink1=mysql_connect('server', 'user', 'pass'); // connect server 2
mysql_select_db('db2_name',$dblink2); // select database 2
$table='output_table';#not sure if this is correct
$tableinfo = mysql_fetch_array(mysql_query("SHOW CREATE TABLE first_table ",$dblink1)); // get structure from table on server 1
echo $tableinfo;
mysql_query(" $tableinfo[1] ",$dblink2); // use found structure to make table on server 2
$result = mysql_query("SELECT * FROM first_table ",$dblink1); // select all content
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) ) {
mysql_query("INSERT INTO output_table (".implode(", ",array_keys($row)).") VALUES ('".implode("', '",array_values($row))."')",$dblink2); // insert one row into new table
}
mysql_close($dblink1);
mysql_close($dblink2);
echo "complete";
?>
I have tried the output_table with and without the same column names/amount as first_table, but it does not seem to be working.
Again, any help would be appreciated.
The problem might be that $dblink2 is undefined.
$dblink2=mysql_connect('server', 'user', 'pass'); // connect server 2
mysql_select_db('db2_name',$dblink2); // select database 2
Also consider using pdo methods instead because mysql- methods are now less favored to the PDO way of working with database objects.
This is most likely a firewall issue. Most 3rd party hosts don't allow you to access MySQL from 'the outside'.
You can do this if you manage the MySQL server yourself or if you have a host that doesn't deny you to access the database from another machine, but in regular web hosting, this is not common.
o to your phpMyAdmin installation and look for the file config.inc.php. Open that file, and check which hostname / ip is being used
My code is as follows, I have changed the values of the Mysql login.
$json_url = "https://www.bitstamp.net/api/ticker/";
$json_data = file_get_contents($json_url);
$json_feed = json_decode($json_data);
$bitstamp_btc_low = $json_feed->low;
$bitstamp_btc_high = $json_feed->high;
$bitstamp_btc_ask = $json_feed->ask;
$bitstamp_btc_volume = $json_feed->volume;
$timestamp = date('Y-m-d H:i:s');
$connection = mysqli_connect("localhost",'username','password','database');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($connection,
"INSERT INTO `database` (Timestamp, Low, High,
Ask, Volume)
VALUES ('$timestamp', '$bitstamp_btc_low',
'$bitstamp_btc_high', '$bitstamp_btc_ask',
'$bitstamp_btc_volume')");
mysqli_close($connection);
When I execute the code on my localhost server it works fine, inserts the data in to the database, in the format i require and its great. When i upload it to my server though, it does not. The only column it enters data into is the '$timestamp' column. (I have set an independent request for the date, away from the default Mysql entry.) So it makes me believe that this information is being received from the script. Its just the low, high, ask and volume that is returning a 0 value.
I have contacted the API provider and they have confirmed that my host IP has not been black listed and all is well.
Im baffled as to why this is not working online, but it is offline, and just to confirm, even though its not entering the data into the cells, it is creating a new row just with 0 values.
I have just run through the error log and i am getting these errors PHP Notice: Trying to get property of non-object in /home/wpdhcskl/public_html/cron-jobs/cron_bitstamp.php on line #
# being the lines with;
$bitstamp_btc_low = $json_feed->low;
$bitstamp_btc_high = $json_feed->high;
$bitstamp_btc_ask = $json_feed->ask;
$bitstamp_btc_volume = $json_feed->volume;
Many thanks
You've got a database called database? Okay....
Anyways try putting ` around the field names as sometimes different environmens have different setups...
INSERT INTO `database` (`Timestamp`, `Low`, `High`, `Ask`, `Volume`)
Also you should really escape your values and use something like PDO.
i am experiencing mysql server has gone away in response of storing API result in mysql database through php code is follow,
$api_response = sendmsg($group_detail['cell_no'], $SMS); //function to execute API
//update sms sent record
$sql = "INSERT INTO sms_sent_record (record_id, api_response)
VALUES (NULL, '$api_response')";
$result = mysql_query($sql) or die('Error, updating sms & group count
in sms_sent_count failed : ' . mysql_error());
i have tried many other options such as storing api response in array and then store it in mysql table, but in any way it throws the same error. The other code in function after this error will not executed by server.
i think it is due to delay in api response and timeout problem in mysql.
Is there any way to avoid this error and storing result in proper way.
Does it work if you omit the sendmsg and do:
$api_response = 'test';//sendmsg($group_detail['cell_no'], $SMS); //function to execute API
If so, does sendmsg() use some sort of mysql connection at any time?
If it does, you may want to store the link from when you connected to the database the first time and reuse it.
$link=mysql_connect();
..
..
mysql_query($sql,$link);
There are lots of reasons for the gone away message but I suspect your problem is that you open the connection to your DB before the API call (to send an SMS ?) and the API call causes the server to terminal your connection before the INSERT is run
Try moving your open db connection to just before the INSERT statement :
$api_response = sendmsg($group_detail['cell_no'], $SMS); //function to execute API
// open connection here
$sql = "INSERT INTO sms_sent_record (record_id, api_response)
VALUES (NULL, '$api_response')";
$result = mysql_query($sql) or die('Error, updating sms & group count
in sms_sent_count failed : ' . mysql_error());
Check your value set for wait_timeout this is
The number of seconds the server waits for activity on a
noninteractive connection before closing it.
If you have this value set very low the server will timeout your connection between opening your connection and running your query.
I have been learning how to program websites lately and the time has come for me to add a database. I have in fact already successfully created a MySQL database and interacted with it with PHP.
My problem is I can't seem to access a SQLite database file with it. I am using MAMP to host locally for now. Here is a snippet of the code I am using to access the db and find and print out a value stored on it.
<?php
$dbhandle = sqlite_open('/Applications/MAMP/db/sqlite/Users');
if ($dbhandle == false) die ('Unable to open database');
$dbquery = "SELECT * FROM usernames WHERE username=trevor";
$dbresult = sqlite_query($dbhandle, $dbquery);
echo sqlite_fetch_single($dbresult);
sqlite_close($dbhandle);
?>
As you have access to the database (your code doesn't die), I'd say there's got to be an error later ;-)
Looking at your SQL query, I see this :
SELECT * FROM usernames WHERE username=trevor
Are you sure you don't need to put quotes arround that string ?
Like this :
SELECT * FROM usernames WHERE username='trevor'
Also, notice that sqlite_fetch_single will only fetch the first row of your data -- which means you might need to use sqlite_fetch_array or sqlite_fetch_object if you want to access all the fields of your resulset.