I'm getting a connection timed out when I try to connect to an external (not on the same server as this code is on) database through the following code. I know the settings of the database is set up right, and the login info is good, 'cause I can login fine from my computer with HeidiSQL. If anyone can see a problem with this code, thanks.
function database_connect(){
$link = #mysql_connect("xx.xxx.xxx.xx:3306","root","pass");
$sql_error = mysql_error();
if (!$link) {
echo "Connection with the database couldn't be made.<br>";
echo "$sql_error";
exit;
}
if (!#mysql_select_db("databasename")) {
echo "The database couldn't be selected.";
exit;
}
return $link;
}
database_connect();
have you tried adding the IP address of where that script is hosted to "Remote Database Access Hosts"?
If you're on cPanel, you need to allow the IP address who can remotely access the datase(mysql).
If you're not, then this is just an idea of what to do.
Problem known now.
My webhost does not allow connections to an external database.
Related
I instal wamp server locally on my PC.I create a user registration form using php and mySQL database for pratice.I was able to connect to my mySQl data server and if i input data on the form and submit is work successfully.I normal experience at time when i access the user registration form it lost connection to my SQLdata server even though my wamp server is on.I will troubleshoot by changing some of the parameter by using mysqli_connect() to check if i will be prompted unsuccessful connection still nothing will show.When i check some other time it will work successfully.Please what may be causing this issue?
Run wampserver as administrator by right clicking wampserver.exe file. It may help.
Edit:
With the type of english you are using to produce the problem and giving no code is making it difficult to predict what the problem actually is. But I think there is something wrong with your mysqli connection file.
Add this code bit to the top of your registration.php file like this:
<?php
$connection = mysqli_connect('localhost','root','');
if(!$connection) {
die("Failed to connect" . mysqli_error($connection));
}
else {
echo "Connection Established";
}
$select_db = mysqli_select_db($connection, 'db2');
if(!$select_db) {
die("Database selection failed" . mysqli_error($connection));
}
else {
echo "Database selected";
}
?>
<?php
////rest of ur registration code goes here
Good day,
I have created an IFTTT receipe that if a "myfox" alarm system is armed, a php script is executed on my NAS (192.165.x.x). The php script is supposed to trigger a stored procedure in my mysql database.
The following PHP script has been tested by other means and I'm sure that it works :
<?php
/*
php_update_mode_armed.php
*****************************************************************************************
* This script updates the value of the components in the table tbl_eedomus_current_mode
* It calls the stored procedure 'sp_tbl_eedomus_current_mode_armed'
*****************************************************************************************
Version 1.00, 09.06.2017, Initial version of the script
*/
mainProcess();
function mainProcess()
{
$ServerIP = "192.165.x.x";
$sqlUser = "domoos";
$sqlDatabase = "domoos";
$pw = "myPass";
// Connect
$mysqli = new mysqli($ServerIP, $sqlUser, $pw, $sqlDatabase);
if(!$mysqli) {[![enter image description here][1]][1]
header('Location: error.php?error=DbConnectionFailure');
die();
}
// Call stored procedure sp_tbl_eedomus_current_mode_armed
if(!$mysqli->query("CALL sp_tbl_eedomus_current_mode_armed ()"))
{
echo "OK";
if($mysqli) $mysqli->close(); // Close DB connection
//header('Location: error.php?error=QueryFailure');
die();
}
if($mysqli) $mysqli->close(); // Close DB connection
}
?>
Below, also, a screen shot of my "then" part of the IFTTT receipe.
Am I doing something wrong here or is the use of IFTTT not fit for the purpose I'm trying to achieve here?
Many thanks for your help on this matter and have a great day.
Most likly IFTTT can't access your NAS from the internet due to home router firewall etc. Also the ip address used in IFTTT shouldn't be a local ip like 192.168.* but your public ip address. You can figure this out by googling for "whats my ip".
Best way to test your setup using your laptop is to disconnect from local wifi network, tether your phone to your laptop and try visiting the NAS ip url to see if it still works. You can use chrome postman app to send out POST requests.
If that's all fine, get PHP to log incoming connections by writing to a file. file_put_contents("log.txt", print_r($_REQUESTS, true));
I would suggest trying with a GET request first with default content type. Good luck!
Is there a way in PHP to upload database from localhost to cloud with just one click? What I am thinking if it is possible to have a button in my system for localhost (offline system) only. Then the code of it is: if there is a internet connection, the localhost database will update the cloud database. Or anyway for it to do it? TIA.
I'm not expert in that, but I think you can do that. For that you have to first check that you are connected or not by writing php script that checks your connect.
example :
<?php
function is_connected()
{
$connected = #fsockopen("www.example.com", 80);
//website, port (try 80 or 443)
if ($connected){
$is_conn = true; //action when connected
fclose($connected);
}else{
$is_conn = false; //action in connection failure
}
return $is_conn;
}
?>
And then you can write your update query with remote_db connection details(DB_user_name, db_password, db_name, etc) if connection = true. And in else part you can update your local_db.
And you can call this function on_click event.
Hope this answer will help you.
I have one file on one web serve and my phpmyadmin SQL databases on a seperate server and am currently trying to connect to my external serve but it doesnt seem to work.
I keep getting the error message:
'Access denied for user ''#'localhost' (using password: NO)'
and am not sure what this means.
Any help would be greatly appreciated or even an article to get started on figuring this out since I cant seem to find anything myself.
Thank you
EDIT --- PDO SCRIPT ---
My PDO Script is the generic one from w3, I have place holders for security reasons.
<?php
$servername = "externalIP";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=Reservations", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
you have to find your phpmyadmin page where you login using also a password.
Try this one :if you have not assign password into database for specific user then you just have to apply 'root' as user and leave blank password field and also set your hostname as 'localhost' or 127.0.0.1
also remember that you have to start first apache and mysql services before access.
If you are using remote server as database then not required.in that case you have to apply your remote Ip address and also remember that in that remote server if you have not specific user then leave password as blank. one another way to debug mysql connection into your remote server creating mysql connection file with its required paramter to check that your remote server ok or not.
This error will come most probably because of user permission issue. Make sure you have privilege to that user for that database.
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");