Adding data to MySQL from PHP - php

I want to add info to my data base MySQL from my iPhone app, through the use of the PHP code that follows:
<?php
if (isset($_GET["var1"]) && isset($_GET["var2"])){
$User = $_GET["var1"];
$Password = $_GET["var2"];
$conexion = conectdb();
$result = insertUser($User, $Password, $conexion);
}
function conectdb(){
$host=“http://myExample.com";
$bd=“pruebabd”;
$usuario=“StackOverFlow”;
$password=“thisIsMyPassword”;
$conexion = mysql_connect($host, $usuario, $password);
mysql_select_db($bd, $conexion);
return $conexion;
}
function insertUser($User, $Password, $conexion) {
mysql_query("INSERT INTO pruebabd (User, Password) VALUES ('{$User}’,’{$Password}’)”, $conexion);
mysql_close($conexion);
echo 1;
}
?>
My hosting works as a Plesk Hosting.
When I run my project, it doesn't work.
For the field $host, should I write the name of my domain? What should I write in $host?
I called to the enterprise who sold me the domain and hosting, but they told me that it is not possible to make this operation because it works as "Plesk Hosting". They told me that I need to change my hosting to CPanel Linux. Is it true?

Related

Verify if connection to the mySQL exists or let Apache do it

I created a function to connect to the db in php:
function fn_connect($is_write = false)
{
$host = '127.0.0.1';
$db = 'name_db';
if ($is_write) {
$user = 'user_write';
$pwd = 'password_write';
} else {
$user = 'user_read';
$pwd = 'password_read';
}
$conn = new mysqli($host, $user, $pwd, $db);
if ($conn->connect_error) {
die('The database is not available. Please, try again later.');
}
return $conn;
}
When I need to connect, im calling it (and closing it) like this
$conn = fn_connect(true);
$stmt = $conn->prepare($q);
$stmt->execute();
....
$stmt->close();
$conn->close();
I thought it will be a good idea to verify if the connection exists, that way, I guess, I save connecting to the db every time for nothing, like this:
if (!isset($conn)) $conn = fn_connect(true);
$stmt = $conn->prepare($q);
$stmt->execute();
....
$stmt->close();
if (isset($conn)) $conn->close();
Is this a good idea, a good practice? Should I jut connect normally and let Apache/PHP do the rest (no need to verify nothing)?
It is good practiose and good style to check the connection, before letting php try to get or send data.
What is not good style is to use die in your connection, because it leaves a broken page.
Better is to design the page so that page still works when the connection is broken.

I am unable to fetch the data in my application from my Website(dashboard in PHP)

I have developed an project for News application and I have both Dashboard and Android Application ready!
Now, I have to host my server side code (Dashboard) on hosting website. I chose 000webhost for testing purposes and hosted on that. I am able to fetch the data in app and able to post from dashboard when hosted on 000webhost, but when I have chose another services like BlueHost, InfinityFree then I am unable to fetch the data in my application!
Now the problem is-
Since I have developed my application using local servers so my config.php look likes this
<?php
//database configuration
$host = "localhost";
$user = "epiz_24504438";
$pass = "******";
$database = "epiz_24504438_myapp_db";
$connect = new mysqli($host, $user, $pass, $database);
if (!$connect) {
die ("connection failed: " . mysqli_connect_error());
} else {
$connect->set_charset('utf8');
}
$GLOBALS['config'] = $connect;
$ENABLE_RTL_MODE = 'false';
?>
But when I have to upload it on hosting services websites the have a forced SQL host-name like
<?php
//database configuration
$host = "sql211.epizy.com";
$user = "epiz_24504438";
$pass = "******";
$database = "epiz_24504438_myapp_db";
$connect = new mysqli($host, $user, $pass, $database);
if (!$connect) {
die ("connection failed: " . mysqli_connect_error());
} else {
$connect->set_charset('utf8');
}
$GLOBALS['config'] = $connect;
$ENABLE_RTL_MODE = 'false';
?>
Due to sql211.epizy.com I am able to access my Dashboard(no data in application) and when I am changing it to localhost then I am not able to access both dashboard and data in application.
While on 000webhost they have SQL host as locahost so it working fine there on both application and dashboard but due to limitations of free hosting I don't want to use it!
So please help me out!! What should I do?
$connect = new PDO(('mysql:host= ;dbname= ', ' ', ' ');
Example:
$connect = new PDO(('mysql:host= hosturl;dbname= ', 'user', 'password');

Trouble connecting to my database and inserting info via PHP script

This script works through a free webhosting service, but not the paid hosting service I have via iFastNet. I use cpanel to create databases and etc.
Here's my script for registering new users in my database. It is communicating with files in my android studio project.
<?php
$con = mysqli_connect("localhost", "my_username", "my_password", "userdatabase_name");
$email = $_POST["email"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "SELECT * FROM users WHERE email = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $email, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $id, $username, $email, $password);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["username"] = $email;
$response["password"] = $password;
}
echo json_encode($response);
?>
Not sure if any of you have experience with this. I'm assuming it's the web hosting service. Hopefully one of you have had this problem, and can provide a simple solution.
It's important to bring up that I can connect to the database via MYSQL Workbench, and I'm able to insert data into my table there. I just can't insert data via the simple user registration I created for an android application. It worked via a free web hosting service though.
-Also, before anyone mentions it, my user has all privileges for the database on the paid web hosting service.

Connect Firebird localhost DB with php

i have firebird server and firebird database installed on windows server. port on this db is 8095. On this server i have installed PHP, and i need connect on this DB with php, here is my code which i tried but not success:
<?php
$host = 'localhost:D:\path\to\Database.FDB';
$username = 'user';
$password = 'pass';
$dbh = ibase_connect($host, $username, $password);
$stmt = 'SELECT * FROM StoreCards';
$sth = ibase_query($dbh, $stmt);
while ($row = ibase_fetch_object($sth)) {
echo $row->Code, "\n";
}
ibase_free_result($sth);
ibase_close($dbh);
?>
can you help me please?
thanks a lot
EDIT :
working with this code :
$dbh = ibase_pconnect("ipaddr:path-to-db.FDB", "user", "pass") or die('die message');
$q = ibase_query($dbh, "select * from StoreCards");
while ($r = ibase_fetch_object($q)) {
$some_value = $r->CODE;
echo $some_value;
}
As long as you said the Firebird runs on a non standard 8095 port (usually it is 3050), you should specify it in ibase_connect. Something like ibase_(p)connect('host/port:path_or_alias', ... Also check if the port is accessible, no block firewall rules, etc..
So:
$host = 'localhost/8095:D:\path\to\Database.FDB';

php local connection mysql database

I try to make a simple IOS app that can connect to mysql database and read one table. But my php code does't work and really have no idea why, it's seems correct to me. The database is in a raspberry phpmyadmin server and the server works great.
I will put my code here and please tell me what's wrong.
<?php
$host = "192.168.2.193";
$db = "produtos";
$user = "root";
$pass = "1234";
$connection = mysql_connect($host, $user, $pass);
if(!$connection)
{
die("Database server connection failed.");
}
else
{
//attempt to select the database
$dbconnect = mysql_select_db($db, $connection);
//check to see if we could select the database
if(!dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$query = "SELECT * FROM produtos";
$resultset = mysql_query($query, $connection);
$records = array();
//loop throught all our records and add them to our array
while ($r = mysql_fetch_assoc($resultset))
{
$records[] = $r;
}
echo json_ecode($records);
echo $resultset;
}
}
?>
Based on the question:
use mysqli_connect rather than mysql_connect because mysql_connect is deprecated and will not work someday. Also what is the the error you are getting? change your die() statement to something more helpful die(mysqli_error($connection));
Based on your comment:
That error would suggest that you either A) don't have the right IP address or B) there is a network issue between your host server and the SQL server, is this code running on the same server that is hosting the SQL database? if so then you can probably just use localhost for your $host

Categories