Hi I am making a login/register database. I am not hosting the database on the same server so how do I make it point to my separate domain? Also if you can tell me what the root is pointing to... Thx
Here are my files:
db.php
<?php
$connection = mysql_connect('localhost', 'root', '');
if (!$connection){
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db('register');
if (!$select_db){
die("Database Selection Failed" . mysql_error());
}
?>
Links (my scripts and original scripts): https://docs.google.com/document/d/1u60X_mKd9z548qrh_VjdyTx3hziiZiYPLUa_rJX11mQ/edit?usp=sharing
For starters, DON'T USE mysql_connect()! Use either mysqli or PDO. Second, don't have your application log in to MySQL as root; create an application-specific unprivileged user (with a password!) for this purpose.
You'll need to create your unprivileged user with connect privilege from the web server's IP or DNS address, and instead of connecting to localhost your PHP will need to connect to the DB server's IP or DNS address, like so:
$dsn = "mysql:host=mysqlserver.mynetwork.com;dbname=register";
try
{
$conn = new PDO($dsn, $appuser, $apppasswd);
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
You could try the following example
$server = "192.168.1.1"; //server name or IP Address
$username = "root"; //database username
$password = "password"; //database password
$database = "mydatabase"; //database to connect to
$connection = mysqli_connect( $server, $username, $password, $database);
if (!$connection){
die("Database Connection Failed" . mysqli_error());
}
else{
//sql statement
}
mysqli_close($connection);
Thanks
Related
I am working on a payroll system. Everything is working fine on localhost (xampp) but when I upload it to ionos hosting, it is loading the admin login page fine but when i enter the username & password it is giving "Connection failed: No such file or directory".
DB name: dbs4868780
and here is the code:
<?php
$conn = new mysqli('localhost', 'root', '', 'xxxxxxxxx');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
EDIT:
I tried it but it is still giving the same error.
I also tried to connect DB with PHP code provided by ionos but it is still giving "Connection failed. File or Directory not found"
Here is the php code from ionos:
<?php
$host_name = 'db5005797255.hosting-data.io';
$database = 'dbs4868780';
$user_name = 'xxxxxxxxx';
$password = 'xxxxxxxxxxxxxxxxxxxx';
$link = new mysqli($host_name, $user_name, $password, $database);
if ($link->connect_error) {
die('<p>Failed to connect to MySQL: '. $link->connect_error .'</p>');
} else {
echo '<p>Connection to MySQL server successfully established.</p>';
}
?>
See attached fake database example to get your data
I am getting SNAT Port issue. I don't know what to do I am using PHP 7.0 let me show my code
this is my db code
function db_connect() {
$server = 'P:servername'; // this may be an ip address instead
$user = 'username';
$pass = 'pasword';
$database = 'test'; // name of your database
// Create connection
$conn = mysqli_connect($server, $user, $pass, $database);
return $conn;
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
}
I am getting SNAT port issue what should I do?
Using Persistent Connections
If mysqli is used with mysqlnd, when a persistent connection is created it generates a COM_CHANGE_USER (mysql_change_user()) call on the server. This ensures that the re-authentication of the connection takes place.
https://www.php.net/manual/en/mysqlnd.persist.php
I am trying to upload a file from my machine (client side) to the FileZilla server (server side) I have for storing the web page files.
When trying to connect to FileZilla through PHP I receive the following error message:
Connection failed: Connection refused
Usually, I would expect the error when the login credentials are incorrect, however, in this case, they are correct.
My question: Can you connect to FileZilla via PHP? I am sure the answer must be 'Yes' however due to the technical difficulties currently I would not be surprised otherwise.
Potentially there is an error in the formatting of the connection function.
var $host = "xx.xx.xx.xx";
var $user = "xx";
var $pass = "xx";
var $connect;
function serverConnection()
{
$conection = mysqli_connect($this->host, $this->user, $this->pass) or die
("Connection failed: " . mysqli_connect_error());
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$this->connect = $conection;
}
return $this->connect;
}
The goal is to be able to upload files from a form to FileZilla, to act as users profile pictures.
Currently, you're using mysqli_connect which connects to a database server, you need to connect to an FTP server, so you should use the FTP functions. You should first connect and then login.
It should look something like this:
$server = 'myServer';
$user = 'myUsername';
$pass = 'myPassword';
$connect = ftp_connect($server) or die('Could not connect to FTP-server.');
if(ftp_login($connect, $user, $pass)) {
echo 'Connected to FTP-server.';
}
I've been trying to upload my PHP MySQL(in Dreamweaver) project to a free web-hosting site.
When I logged in, there is an error that appear in dbconn.php file.
The error is shown below:
and here's the code in my dbconn.php file:
<?php
/* php& mysqldb connection file */
$user = 1350048; //mysqlusername to db
$pass = "password"; //mysqlpassword to db
$host = "eskl.freeoda.com"; //server name or ipaddress
$dbname= 1350048; // db name in server freeoda
$dbconn= mysql_connect($host, $user, $pass);
if(isset($dbconn)){
mysql_select_db($dbname, $dbconn) or die("<center>Error: " . mysql_error() . "</center>");
}
else{
echo "<center>Error: Could not connect to the database.</center>";
}
?>
I would really appreciate if anyone can teach me how to solve this.. thanks in advance!
As Kerbholz already stated, don't use mysql_* functions, they are really outdated.
Instead use mysqli:
$servername = "eskl.freeoda.com";
$username = "1350048";
$password = "password";
$database = "1350048";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
For your error it got mostly something to do your host doesn't allow remote connections. Try to change the serverhost to localhost or 127.0.0.1
I have the following code:
<?php
//Step1
$db = mysqli_connect('localhost','root','[mypassword]','users')
or die('Error connecting to MySQL server.');
?>
<html>
<head>
</head>
<body>
<h1>PHP connect to MySQL</h1>
</body>
</html>
I am just trying to connect to my MySQL database. It is administered using phpMyAdmin. I am very unfamiliar with MySQL and I have never used it before. [mypassword] is the password I use to successfully connect to mySQL from the Mac terminal. "users" is a name of a table I have created in phpMyAdmin. I am using cPanel. I keep on getting the error:
Error connecting to mySQL server.
In phpMyAdmin it says Server: localhost:3306. I have tried for a very long time to fix this problem but with no results. What am I doing wrong?
I have also tried the following:
<?php
$servername = "localhost";
$username = "root";
$password = "[myPassword]";
try {
$conn = new PDO("mysql:host=$servername;dbname=users", $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();
}
?>
After visiting the webpage it says
Connection failed: SQLSTATE[28000] [1045] Access denied for user
'root'#'localhost' (using password: YES)
Be sure that your mySql instance is up. You can download sequelpro and use that to connect to your mySQL. If that doesn't work then its a mysql setting/config that is wrong.
You can try this code
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
How to Connect to mysql using php
Use a try catch to handle errors on the connection proccess, but i strongly advise you to use PDO by alternative from mysqli PDO Book
Use something like this:
<?php
try
{
if ($db = mysqli_connect($hostname_db, $username_db, $password_db))
{
//do something
}
else
{
throw new Exception('Unable to connect');
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
And check if the code print some connection error.
Reference: how to use throw exception in mysql database connect