Connecting php and mysql database post production in web development - php

I wrote a simple user registration system that runs on my local machine using apache. (Localhost at port 8080).
My php file that tries to connect to mysql database looks like :
<?php
$con = mysqli_connect("localhost","root","","register");
//localhost will have to change to host name if hosted on different server... I think.
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Everything works and I can register/sign in a user on my local machine.
I know that I have to change $con = mysqli_connect("localhost","root","","register"); once I am ready to upload my site to host it in a free server like surge.sh or github pages.
I pushed my site to github pages from my repository and when I click "Register" / "Sign in" it doesn't load the page instead it acts as if I want to download the .php file. How should I make the changes? I need some help and suggestions.

Related

Connect remotely to cpanel Database with php

I am trying to access to the database of my live website from localhost. My website is working on cpanel hosting.
I created database and added user to that database but can not connect to this database from my localhost .
this is the code of database information
<?php
$conf['dbuser']='admin_root';
$conf['dbpass']='rootroot!##$';
$conf['dbname']='bsaiiian_os';
$conf['dbhost']='example.com';
$conf["title"]='DEMO';
?>
I think the problem is from $conf['dbhost'] variable where I'm putting the url of my site and not sure if that is right.
When I'm uploading my script to the hosting and put
$conf['dbhost']='localhost';
the connection is established .
this is error message:
Connection failed: No connection could be made because the target machine actively refused it.
Due to security reasons cPanel's MySQL requires that you allow your IP in order to have access.
This is done through:
cPanel -> Remote MySQL Connection
You need to navigate there and allow your local computer's IP in order to make the connection.
Side Note: This stands true for all IPs, even the server's one that your account is hosted on. You will see your cPanel account's IP already present in there - this is to allow access to MySQL for any files from your cPanel account

How to make database connection from different website

I have a database in a website called booksiders.com and then I have a new website called kryptotech.co.in in which i am looking to use the database of the first website. So how can connect with the database which exists on another hosting server.
This is the code i am using in kryptotech.co.in for connecting to the database of booksiders.com from kryptotech.co.in which is hosted on another server.
<?php
$con = mysql_connect("booksiders.com","bookside","******");
if(!mysql_select_db("bookside_kryptoDB",$con))
echo "Unable to connect to database";
?>
Please tell me what hostname should be used for doing this.
You will have to allow your server IP in allow remote host access list on remote server and ask them to allow port 3306 in their firewall so that you can connect mysql databases from your hosting server.

Failing to connect remotely to MySQL after setting everything up correctly

I have an issue getting to connect remotely to a MySQL database.
My current set-up is as follows: I have a windows 2008 Server at work, placed behind a router (fixed IP). The router has DMZ configured to the server, which works perfectly fine for everything else I have tested including web server, VPN, FTP, etc... The firewall of the Server is opened to the necessary services on the corresponding ports. I can telnet to all these ports decently.
The only thing that I cannot get to work is remotely connecting to a MySQL database. I have done all of the following:
Forward port 3306 to the Server (through DMZ);
Configure MySQL to bind to 0.0.0.0:3306 (it automatically does that when not supplying a bind-address);
Configure a user account in MySQL with access from localhost;
Configure the very same user account (same name, password, rights) with access from '%';
Open port 3306 in the Windows Server's firewall.
I am subsequently trying to execute a PHP script from a hosted web server (elsewhere on the internet):
<?php
// Create connection
$conn = new mysqli(<host>, <username>, <password>);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
with <host>, <username> and <password> being the corresponding and correct credentials.
This script simply times out without reply and gives the expected error Connection failed: Can't connect to MySQL server on '<host>' (110). I can also not telnet on port 3306. Connecting from localhost works fine.
I don't know what else I have forgotten or done wrong. What am I missing?
I need this connection in order to keep my 'backup system' centralized on the hosted web server. I could run my 'backup system' locally on the mysql host, but that makes it harder to manage it from the central point. Beside that, I'd like to get it to work to actually know what's going on. I hate not getting this kind of thing to work.
Thanks for anyone who has ideas on what could be wrong.
Kenneth

Database is there but Wordpress cannot establishing a Database Connection

I am having a server running on my machine and I am calling it over the network. The Server is running fine and there have not been any problems with apache or php yet.
My database is not running on the same machine as apache but in the same local network on a server with the domain "sql.local.network.com".
Now i copied a Wordpress site onto my webserver which has been on another server in the same network, so it should work fine, since the database server is still accessable by both these webservers.
The problem now is, that Wordpress just shows the message Error establishing a database connection.
What I did was to access the database with phpmyadmin which I quickly set up on the server. This worked fine, and I was able to access the database.
Also I found a short skript:
<?php
$link = mysql_connect('sql.local.network.com', 'user', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
This echoed Connected successfully, which means a simple mysql_connect works fine as well.
Also I am able to ping sql.local.network.com over my console and get the response 192.168.0.101.
Everything works, but the Wordpress-page.
Is there a simple solution to fix the Wordpress connection problem I am missing?
Check the Privileges tab in phpmyadmin and look at the host column. If it's set to "localhost", then that dataabase user can only access the database from the same server as the database. To access remotely, change it to either "%" for any server or to the IP of the server you're trying to connect from.

What is my host name for MySQL?

This sounds like a stupid question, but I'm beginning in PHP and MySQL and failing at the first step, trying to connect to the database:
<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect("localhost", "root", "root", "ASOIAF.odb");
if (mysqli_connect_errno()) {
echo "Failed to connect to database";
} else {
echo "Connected to database";
}
mysqli_close($con);
?>
</body>
</html>
I don't understand the first host parameter. A yahoo help page said that the host should be "mysql", but when I used this it failed to connect as well. The database, ASOIAF, is in OpenDocument database format (odb), on LibreOffice Base. I'm using Uniserver to run PHP and MySQL; both are currently running. The web application is stored in Uniserver's www folder, and run on Google Chrome through localhost.
What should I be inserting in the host portion of the connect statement? Or have I made a more basic syntax error in the PHP code that is preventing a connection from being made?
What is my host name for MySQL?
We don't know what you have called the computer you are running MySQL on.
A yahoo help page said that the host should be "mysql"
That is specific to Yahoo Web Hosting. It doesn't sound like you are using their service, so that is inapplicable.
I'm using Uniserver to run PHP and MySQL
That suggests the computer running the webserver running PHP and the computer running the MySQL server are the same machine. That makes localhost appropriate.
localhost is the standard name given to a computer on the private network that only it connects to. For MySQL is has a special meaning and causes the connection to be made via a socket instead of TCP/IP (which is more efficient).
What should I be inserting in the host portion of the connect statement?
localhost if the above is correct.
Or have I made a more basic syntax error in the PHP code that is preventing a connection from being made?
I can't see any syntax errors. I suspect the problem may be down to your database configuration. Have you told the MySQL server about ASOIAF.odb? Is that what the database is actually called inside MySQL (as opposed to just being the file that stores the configuration for LibreOffice to connection to that database).
Your biggest mistake is not using mysqli_error to find out what problems PHP is reporting.
if (mysqli_connect_errno()) {
echo "Failed to connect to database";
echo mysqli_error();
use
mysqli_connect("localhost", "root");
Hope it will work

Categories