mysql - create event and load data infile to Server automatically - php

I need to know if it's possible to Create an Event in MySQL with its code statement using Load Data In file syntax.
Here's my current situation.
I can get the database (MS Access) using PHP from a Local Biometric Database.
I have updated the database in my Local Database Server which is MySQL now.
I can update my target Online Database Server via Exporting it first then Importing the database in the Online Database Server.
My Main Question is how I can manage this without any user interaction
such as MySQL Event Features?
I am trying to upload a data from my PC (Local server) into our Domain (Web Server)

Already done this guys, What i did is I've built a local application to my local server at our office then that application is doing a FTP service to upload the updated database from our Local Biometric Database into the online web server.
Once all this is done, I've created a php script which will load the .SQL file using a Cron Job. You all know cron job are time scheduled events for controlling scripts and doing other linux command.
I'll share my code of php file so everyone who encounter this can use this in the future.
date_default_timezone_set('Asia/Taipei');
$CurTime = date('Y-m-d H:i:s');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link){
die('Failed to connect to server: ' . mysqli_error());
}
//Select database
$db = mysqli_select_db($link,DB_DATABASE);
if(!$db){
//Do auto download of database in the Server automatically
if(file_exists('Database/'.DB_DATABASE.'.sql')){
mysqli_query($link,"CREATE DATABASE ".DB_DATABASE);
$db = mysqli_select_db($link,DB_DATABASE);
$readContent = file_get_contents("Database/".DB_DATABASE.".sql");
$res = mysqli_multi_query($link,$readContent) or die(mysqli_error($link));
mysqli_close($link);
} else {
die("Unable to select database, Please call your system administrator. Thank you.");
}
}
function Reload_Database($DBLink){
if(file_exists($DBLink)){
//mysqli_query($link,"DROP DATABASE ".DB_DATABASE);
//mysqli_query($link,"CREATE DATABASE ".DB_DATABASE);
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
$db = mysqli_select_db($link,DB_DATABASE);
$readContent = file_get_contents($DBLink);
$res = mysqli_multi_query($link,$readContent) or die(mysqli_error($link));
mysqli_close($link);
return 'Database Reloaded';
} else {
die("Unable to reload database, Please call your system administrator. Thank you.");
}
}
echo Reload_Database('../employee/database/'.DB_DATABASE.'.sql');
This is basic script that will reload the database of the uploaded database given from my local application.
From here the cron job will do its part. Expect that my local application is also a service that will just do the uploading of the updated database of the Local Biometric Database.

Related

connecting a mysql database using php to a host

Okay I'm having trouble connecting my database to my host..
Im using myPHPAdmin and I made a database on my host
the problem is in my php code where I define my connection to the host
everything worked fine when I did this on the localhost.
But now that I want to use it on my personal domain it wont work
it doesnt access the data in the database.
Im new at this so dont shoot me ;)
$connect = mysql_connect("host", "user", "pass");
so for my host/domain I enter my domain the-bloodgod.com ?
what do I enter for user and pass? is it just the login I normally use to access the Cpanel? Or do I have to create special permissions in myphpadmin?
also on my hosts myphpadmin it shows that it stores all created tables in the_bloodgod_com database collection
so would this be correct if I put it the code bellow?
mysql_select_db("the_bloodgod_com");
$sql="SELECT * FROM tablename";
MOST hosting companies you have to use "localhost" as the host name for your database connection code. PHP:
$link = mysqli_connect("localhost","username","password","database") or die("Error " . mysqli_error($link));

Having Trouble Connecting to MySQL Database

The following code successfully connects me to my mySQL database.
$conn = mysql_connect("localhost", "root", "") or die("Failed to Connect!");
$db = mysql_select_db("MyDB");
I have been experimenting on localhost using XAMPP and phpmyadmin, and everything works correctly. However, today I uploaded my website to my domain (I have bought a domain and web hosting through GoDaddy) and I keep getting "Failed to Connect!" whenever this code runs. The HTML/CSS work correctly, but I cannot connect to mySQL. How can I connect to my database on the server?
You'll need to change your connection information here:
mysql_connect("localhost", "root", "")
to include your GoDaddy database details instead. Contact GoDaddy for more information on what to use for your account.
You have not mentioned anything about MySQL database hosting.
If you are hosting the database also in godaddy, you should be able to get the connection string and host name from information mentioned here
First, You need to create database in your hosting server (phpmyadmin cpanel) and supply details in your database connect file.
**Looking at your database credentials I can say that you haven't created one yet since
the username is definitely not 'root' and password will be required.**
Sample db_connect file:
<?php
$hostname = 'e.g: internal-ra.db-server-123';
$username = 'e.g: serverone1234d4';
$password = 'e.g: your_own_password';
try {
$pdo = new PDO("mysql:host=$hostname;dbname=database_name_here", $username, $password);
/*** echo a message saying we have connected ***/
}
catch(PDOException $e){
echo 'Exception -> ';
var_dump($e->getMessage());
}
?>
And try to use so less as possible the key DIE()

How to connect to remote MYSQL database via php

I have to build an android app which connects to a remote MYSQL webserver and retrieve information for display purpose.
So I did research coz I had no idea where to start. I read an article it mentions the better approach for that is to use php script. And didn’t know anything about how server, database and php works, so I studied php from here “http://www.homeandlearn.co.uk/php/php12p1.html” to understand and downloaded WhampServer to do some testing. On local machine everything worked fine.
But main thing I don’t understand is “HOW TO CONNECT TO REMOTE SERVER/DATABASE”.
It’s obvious that I’m doing something really stupid, I just need help to find out what am I doing wrong.
When we test php script on local machine in webrowse we use "localhost/some.php." But when I want to test same php script on remoter server from my local machine then what and how should I do? Do I need to make some changes in configuration file on server side?
I have done more research before asking this question here to understand remote server connection in php but I still don’t understand. I have gone through almost all the pages within the link below:
https://www.google.co.uk/search?q=connect+to+remote+mysql+webserver+php&rlz=1C1AVSX_enGB447GB448&oq=connect+to+remote+mysql+webserver+php&aqs=chrome.0.69i57j69i62l3.19724j0&sourceid=chrome&ie=UTF-8#fp=5a2359cf96dc5f79&q=how+to+connect+to+remote+mysql+web+server+php
And help would be much appreciated.
If my question is not clear please let me know I'll try to explain more. or think of it as if you have to connect to a remote MySQL server how would you do , means what is the process and steps involved in that.
Thanks everyone.
Edit
I have created a database "dealt3_raj_test" on remote server. and when I type "examplewebserver.CO.UK/myphpscriptname" in my web browser.
It gives me error "An error occurred , You have reached the error page"
<?PHP
$user_name = "dealt3_raj";
$password = "5dN5nh&eMd(vCR$dzk";
$database = "dealt3_raj_test";
$server = "examplewebserver.CO.UK";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if($db_handle)
{
print "Connected";
}
else
{
print "Can not connect to server";
}
if ($db_found)
{
print "DataBase found";
}
else
{
print "DataBase not found";
}
?>
Adding onto #user4035's comment, after opening the connection, use JDBC in your Android/Java code to interact with the database.
That said, it is not good practice. Rather create a web service
Your application may experience latency/connectivity issues. This will impact performance.
Your MySQL server will have to be open to remote connections which is strongly advised against
If your Android App is intended for public usage, it means the database username and password of your MySQL server reside on everyone's phone using your app. Encrypted or not this makes your database server a "step less" secure
Well answered here on SO (JDBC vs Web Service for Android)
use this code to connect with data base
$username = "database user name";
$password = "DB password";
$hostname = "hostname";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
//select a database to work with
$selected = mysql_select_db("data base name",$dbhandle) or die("Could not select examples");

Connect Oracle Database server in PHP

I'm developing a website in PHP that interacts with Oracle10g remote server database. I've googled on this topic a lot and couldn't find a solution, though i got some idea on tnsnames.ora file. I've installed WAMP in my machine. What are steps to connect to Oracle remote database?? Can any one explain it step by step??
I created a site which connects to MySQL datebase a year ago which wasn't this much tough.
$con = oci_connect('username', 'password', '//server ip:port/service name');
It's throwing "Call to undefined function oci_connect()" error.
First of all , you must install and configure OCI8.
To do this please, follow this link http://antoine.hordez.fr/2012/09/30/howto-install-oracle-oci8-on-rhel-centos-fedora/
To connect to database :
$Conexion_ID =oci_connect($OracleUser, $OraclePassw, $OracleIP);
To launch a query
$sql="SELECT ...";
$id_sentence = oci_parse($Conexion_ID, $sql);
if (!$id_sentence)
{
return false;
}
$results = oci_execute($id_sentence, OCI_DEFAULT);
To view results:
while ($row = oci_fetch_array($id_sentence, OCI_ASSOC+OCI_RETURN_LOBS))
{
....
}

ms-access database to mysql database by php

How we can convert a ms-access database to mysql database by php…
or
How we can access a ms-access database by php..
For accessing a MS Access database on Windows through ODBC, see here.
How we can convert a ms-access
database to mysql database by php
You can export via an ODBC connector or (if you don't have too many tables) you can export your data to a text file and then import it into MySQL (after creating the tables manually) via LOAD DATA. Right-click on the tables and choose Export for available options.
For more detailed info on migrating from MS Access to MySQL, check out this great article from the MySQL Dev Team:
http://dev.mysql.com/doc/mysql/en/LOAD_DATA.html
How we can access a ms-access database
by php
You can do this easily right through PDO.
You can use this simple code to connect to Access database. I tried this code in PHP,working on Windows XP with XAMPP's Apache server, and using Access 2007 file as a database. Just create your access file and try this :
First go to Start menu > Control Panel > Administrative Tools > Data Sources(ODBC) > System DSN > Add.. > Microsoft Access Driver(.mdb,.accdb) and show your access file. Give name to the connection.
Then write in your *.php file this code:
`
<?php
$host= "host_name";
$user= "user_name";
$pass= "password";
$db_connect=odbc_connect($host,$user,$pass); //connect to access file as database
if (!$db) //In case if you didn't connect , you'll get this error message
{
echo "Can't connect";
exit;
}
$query = "SELECT * FROM table_name"; //pulling data form Access file
$row = odbc_exec($db, $query);
while(odbc_fetch_row($row)
{
$row1 = odbc_result($row,1);
$row2 = odbc_result($row,2);
$row3 = odbc_result($row,3);
echo $row1." ".$row2." ".$row3."<br>"; //watching if data is taken correctly
}
?>
And then you can insert that rows into sql database by adding this code into the while loop :
<?php
$db="MySQLdatabaseName";
$db_connect= mysql_connect($host,$user,$pass);
mysql_select_db($db, $db_connect);
$insert_into_MySQL = "INSERT INTO table_name($row1,$row2,$row3)
VALUES('".$row1."', '".$row2."', '".$row3."'); ";\\These are 2 lines to be
mysql_query($insert_into_MySQL ); \\added to the while loop
?>

Categories