I am trying to connect to my MySQL database using the following PHP code
<?php
require "credentials.php";
$conn = mysqli_connect($hostname, $username, $password, $databaseName);
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
And I am getting the following output:
Failed to connect to MySQL: Connection refused
What could be the reasons that this is happening, and how can I fix it?
Thanks in advance.
from your code it seems that you are using undefined variables to connect to database. if you load them from external file, than use something like this in that file:
define("DBPASSWORD", "database_pass");
define("DBUSERNAME", "databaseusername");
define("DBNAME", "database");
define("DBHOST", "database_host");
and than use in file you shown us:
$conn = mysqli_connect(DBHOST, DBUSERNAME, DBPASSWORD, DBNAME);
OR another way, the way I can't recomend is using globals.
WARNING: I discourage using globals, when not needed.
Your credentials.php should look like that:
global $databaseName;
global $password;
global $username;
global $username;
$databaseName= "db_name";
$password= "db_password";
$username= "db_username";
$hostname= "db_host";
and than in other places, first you need to call the globals before usage.
require "credentials.php";
global $databaseName;
global $password;
global $username;
global $username;
$conn = mysqli_connect($hostname, $username, $password, $databaseName);
Related
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'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 am trying to connect to a MySQL database through a php script. It gives me this error from mysql_error(): Access denied for user '#localhost' to database 'userinfo'
userinfo is the database.
my script is this
<?php
$servername = "localhost";
$username = "root";
$password = "'mm'";
$database = "userinfo";
$conn = mysqli_connect($servername, $username, $password);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully<br>";
mysql_select_db($database) or die(mysql_error());
echo "connected successfully to db:" . $database . "<br>";
?>
you are connecting using
mysqli_
function
and selecting data with
mysql_
avoid using both at the same time. since they're incompatible.
use the mysqli_ alternative instead
mysqli_select_db($conn, $database);
and
mysqli_error($conn)
Please keep in mind this isn't the safest way. But since you have said your learning this it is a start.
<?php
$servername = "localhost";
$username = "root";
$password = "mm";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
http://www.w3schools.com/php/php_mysql_connect.asp
To select data from the database
http://www.w3schools.com/php/php_mysql_select.asp
It appeared you where combining the old mysql in php with the new mysqli
Trying to global database connection in all functions in a .php file.
sql.php:
<?php
$servername = "XXXXX";
$username = "XXXXX";
$password = "XXXXX";
$dbname = "XXXXX";
$conn = new mysqli($servername, $username, $password, $dbname);
mysqli_set_charset($conn, "utf8");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
save.php
$conn = require_once('../path/to/sql.php');
function save()
{
global $conn;
$sql = "INSERT INTO `content` (title, text, cat)
VALUES ('".$_POST["title"]."', '".$_POST["text"]."', '".$_POST["category"]."')";
if ($conn->query($sql) === TRUE) {
show();
}
$conn->close();
}
well i want to get sql.php with require_once and then make it global then put this in save() function.
error is:
Fatal error: Call to a member function query() on a non-object in
/homepages/3/xxxxxxxx/htdocs/xxxxxxxx/xxxxxxxx/content.php on line 37
actually i don't know this way to make it global is correct or not, so please help me to choose best way to make a global database connection inside the functions.
Try
define('DATABASE_USERNAME', 'root');
define('DATABASE_PASSWORD', 'password');
define('DATABASE_HOST', 'localhost');
define('DATABASE_DBNAME', 'db');
$conn = new mysqli(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_DBNAME);
Named constants are great for static global data.
Also you just need to do
require_once 'path/to/sql.php';
And not
$conn = require_once 'path/to/sql.php';
$conn is already being set in your require_once.
I am using Cpanel to develop my application. It gives the error
Fatal error: Call to a member function set_charset() on a non-object in /home/restaur /public_html/restaurant/includes/connect_database.php on line 3
when I access to "www.yyyyy.com/filename/"
And here is my connect_database script
<?php
include($_SERVER['DOCUMENT_ROOT'].'/YOUR_FOLDER/variables/variables.php');
$connect->set_charset('utf8');
?>
my close_database script
<?php
include($_SERVER['DOCUMENT_ROOT'].'/YOUR_FOLDER/variables/variables.php');
$connect->close();
?>
my variable script
<?php
// database configuration
$host ="db_host_name";
$user ="db_username";
$pass ="db_pasword";
$database = "database_name";
$connect = new mysqli($host, $user, $pass,$database) or die("Error : ".mysql_error());?>
it should be like
$host ="db_host_name";
$user ="db_username";
$pass ="db_pasword";
$database = "database_name";
// variables.php
$connect = new mysqli($host, $user, $pass, $database);
/* check connection */
if ($connect->connect_errno) {
printf("Connect failed: %s\n", $connect->connect_error);
exit();
}
Now in connect_database.php
// Make sure your included path is correct
include($_SERVER['DOCUMENT_ROOT'].'/YOUR_FOLDER/variables/variables.php');
global $connect;
$connect->set_charset('utf8');
this will help you to know more in depth global
I was having this exact issue, i was having encoding problems but adding
$connect->set_charset('utf8');
(my variable was actually $conn->set_charset('utf8');) just after
$conn = new mysqli($servername, $username, $password, $dbname);
and it worked beautifully.