How to connect Firebase database to Php scripts? - php

I am making an android app with Firebase database, and I am stuck at point when I want to connect that Firebase database with my own hosted Php scripts in which I want to perform some action on specific date , on that date it should delete some data from Firebase. FIrebase data on schedule(On specific date) so I am stuck at how to perform this how can be make this connection to firebase database with Php.
Using connection of php, I want to made connection with firebase DB and want to retrieve and also delete some data from Firebase DB.
<?php
define('HOST','localhost');
define('USERNAME', 'root');
define('PASSWORD','');
define('DB','insert2');
$con = mysqli_connect(HOST,USERNAME,PASSWORD,DB);
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "insert into users (username, password) values ('$username','$password')";
if(mysqli_query($con, $sql)){
echo 'success';
}
?>
Please guide me. thanks

You can't connect to Firebase Database with the same way as MySQL.
You need to use one of these lib :
kreait/firebase-php
ktamas77/firebase-php
Or use the REST API.
Or create a Firebase Cloud Function which delete your wanted data and call it from the php with an HTTP trigger.

You can use this php code
$db = 'IP:C:/folders/files.FDB'; // Computer IP:Database file in drive and folders
$username = 'user';
$password = 'pass';
// Connect to database
$dbh = ibase_connect($db,$username,$password);
$sql = 'SELECT * FROM TABLE';
// Execute query
$rc = ibase_query($dbh, $sql);
// Get the result row by row as object
while ($row = ibase_fetch_object($rc)) {
echo $row->table_column;
}
// Release the handle associated with the result of the query
ibase_free_result($rc);
// Release the handle associated with the connection
ibase_close($dbh);
*And don't forget to install firebase extension to Web Server because it is not enabled by default.

Related

Connecting to MySQL database from a different network?

I've created a database using Microsoft Azure Portal (MySQL Database).
Currently I'm using PHP to connect to the database.
$servername = "server.mysql.database.azure.com";
$username = "username#server";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname, 3306);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
SQL queries to populate data in JSON format.
$sql = "SELECT id, username, score FROM ScoreTable";
$result = $conn->query($sql);
$dbdata = array();
while ( $row = $result->fetch_assoc()) {
$dbdata[]=$row;
}
echo json_encode($dbdata);
Result (localhost/jubakee.php OR http://192.168.1.7/jubakee.php (local IP) both produce the same results).
[{"id":"1","username":"Stacey","score":"500"},
{"id":"2","username":"SJ","score":"600"}]
The issue I am having is that I want to be able to access this database from different networks e.g on 3G/4G or my friend's house.
When trying to load http://192.168.1.7/jubakee.php on my android device it will only produce the results if I'm on the same network as my PC (Same Wi-Fi Network).
When I switch my network from Wi-Fi to Mobile Data (3G) I can no longer access the database and http://192.168.1.7/jubakee.php will not load.
The purpose of this is so I can create an online database which will interact with my C# android application. The database will store the scores of the users.
How can I allow users using different networks to connect to my database?
Thank you.
You can't not access the xamp server from your cellular data or different wifi networks. You have to upload your database and files in a live server like 000webhost then you will have a url like http://YOURWEBSITE.000webhost.com/jubakee.php. Now you can access this url from anywhere, anytime and in any applications.

Need idea for Two different remote MYSQL database connection in one PDO

iam developing a multi tenant application. And i need an idea. as iam in a dark tunnel with no light now
there will be two database involve from two different location.
Database (A) - Tenant List with tenant database (B) username,password,IP address
Database (B) - Specific tenant info base on selected tenant from database (A)
What i want to achieve is:
When user enter their web url with specific user_id in url, first it will query the tenant list in Database (A) based on the user_id from url.
then, it will straight make a persistent connection to Database (B) using the credential from query Database (A) result.
hope to get ideas and bring me some lights. thank you!
here is what i had try:
public function func_get_user($id){
$tenant = $this->client_info_func($id); // this is function to get Tenant credential from Database A for using in database B
if (!empty($tenant)) {
$pdo_client = $this->cliend_db_conn($tenant['client_db_name'],$tenant['client_db_username'],$tenant['client_db_pass'],$tenant['client_db_ip']); // this is the function to initiate PDO connection to Database B
$Q = "SELECT user_id,user_name,client_id FROM user WHERE client_id =:id";
$R = $pdo_client->prepare($Q);
$R->bindParam(':id', $id);
$R->execute();
$result = $R->fetch(PDO::FETCH_ASSOC);
return $result;
}else{
echo 'Sorry! you are not allowed tenant';
}
}
You haven't posted any code. But the logic is as follows:
Connect to database A using PDO as normal:
$pdo_db_A = new PDO(... ;dbname='database_A', 'db_A_username', 'db_A_password');
Then run this query on database A:
SELECT username, password FROM database_A_table WHERE user_id = ...
Get your data in PHP variables, such as an array:
$db['username'] = $row['username']; // $row being from the above query
$db['password'] = $row['password'];
Make a separate PDO connection to database B and pass in the details:
$pdo_db_B = new PDO(... ;dbname='database_B', $db['username'], $db['password']);

converting json to xml or not

I am using PHP files to provide my iOS app with JSON data. Now I am developing an Android app that should use the same PHP files to parse MySQL objects. The problem I have now is that I am creating the Android app following an internet tutorial and it needs the MySQL objects in XML format.
I kindly request your advice to continue working with my Android app.
Should I change my app to be able to use the same PHP files as the iOS app?
Or is there an easy way that after making a copy from the PHP files and then updating them to change the output type from JSON to XML would allow me to use them to be used by the Android app?
As example, here is my PHP to provide JSON objects to my iOS app:
<?php
$host = "localhost"; // host of MySQL server
$user = "hidden"; // MySQL user
$pwd = "hidden"; // MySQL user's password
$db = "hidden"; // database name
// Create connection
$con = mysqli_connect($host, $user, $pwd, $db);
mysql_query("SET NAMES 'utf8'");
// Check connection
if(mysqli_connect_errno($con)) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
// query the application data
$sql = "SELECT * FROM tbempresas";
$result = mysqli_query($con, $sql);
// an array to save the application data
$rows = array();
// iterate to query result and add every rows into array
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows[] = $row;
}
// close the database connection
mysqli_close($con);
// echo the application data in json format
echo json_encode($rows);
?>
You definitely should not change the data format from JSON to XML. Using XML as data format in mobile badly affects performance when it comes to speed with which data are sent.
Instead of changing data format use external lib for JSON parsing such as gson.
You can use json_decode() and PEAR::XML_Serializer for converting the output type to XML and then you can use it in the Android app.

MySQL Functions Do Not Work

Thanks For Answers!
I got lots of feedback and I have an answer and I fixed it. Thanks!
I am working on making a user login system for my website. I want the updates to be faster so I moved the exact files to my local server, (Apache/2.2.22 (Ubuntu)), and it suddenly does not work. The Database is external so it does not need the hostname changed. Any ideas? I get no errors, it just does not parse the MySQL bit.
Connect File:
<?php
$host = "xxxxxxx.xxxxxxxxx.xx.xx";
$user = "sh0u_xxxxxx";
$pass = "xxxxx";
$db = "sh0u_1xxxxx_store";
mysql_connect($host,$user,$pass) or die("Unable To Connect To Database");
mysql_select_db($db)
?>
Login File:
<?php
session_start();
session_destroy();
include('connect.php');
$email = $_POST['email'];
$pass = md5($_POST['password']);
$query = mysql_query("SELECT * FROM users WHERE email='".$email."' AND password='".$pass."'");
$numrows = mysql_num_rows($query);
if($numrows === 1) {
session_start();
$_SESSION['sid'] = "".$email.":".$pass."";
header('Location:../index.php');
}else {
include('../login.php');
echo "<script>alert('Incorrect Username and/or Password');</script>";
}
?>
Most hosting providers do not allow external access to the databases they include with their plans. Not only that, most of them use localhost as a database server so as to force a socket connection (so that they can even disable network connections to their DBs altogether).
To test your script and site locally you will need to download a dump of your database and create a local version of it on your own.
Other issues with your code
As mentioned in comments you are:
You should be using the MySQL Improved Extension, instead of the old (and deprecated) MySQL extension
You are not sanitizing data you use for your queries (use prepared statements)
MD5 is not secure for passwords, you should be using the new password_hash instead

How to connect php to a database in vb application?

I have a VB application using a Microsoft SQLServer Database File on my computer. And I want to access the information from that database online using PHP. I already installed Apache and PHP on my computer.
The problem is I don't know how to connect my PHP to the database on my VB application.
First of all, take a look at this link concerning PHP's MS SQL (Microsoft SQL) library.
You need to use the MS SQL functions to connect to the database and retrieve the relevant information you require.
I'm not an expert with MS SQL but you'll need code similar to the following:
$server = "EDDYSPC\SQLEXPRESS"; // Server address
$user = "root";
$pass = "";
$db = "MyDatabase";
$msSQL = mssql_connect($serverm $user, $root);
if (!$msSQL) { // If there was an error connecting to the database
die("An error occurred.");
}
mssql_select_db($db); // Select the datbase to use
$query = mssql_query("SELECT name FROM users WHERE name = 'Eddy'");
while ($row = mssql_fetch_assoc($query)) { // Loop through each returned row
print($row['name'] . "<br />\n");
}
Hopefully this helps.

Categories