Trouble Connecting to the MySQL server - php

I'm trying to connect to the MySQL server using MySQLi. It is enabled in the server end where this code runs. I am unable to connect to the server using the following information. Did I miss something? Any help would be appreciated. Thank you.
<?php
// DBInfo
$hostname = "localhost";
$username = "nc5ff";
$password = "Ni\$hant809472";
$dbname = "test";
// DB Connection
$link = mysqli_connect($hostname, $username, $password, $dbname);
/* check connection */
if (!$link) {
header("Connection Failure", true, 777);
}
mysqli_close($link);
?>

You have to escape the password as it contains special php characters (\ and $):
$password = "Ni\\\$hant809472";

Make sure you have all of the information right i don't see anything wrong with the code, but please make sure you have all the information right.

Related

How to fix error in dbconn file when upload php mysql project to free webhosting site?

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

Access denied or blank page with mysqli but not mysql

I've looked through StackOverflow for an answer to this and have so far come up empty handed. I know some have had a similar issue, but so far none of the responses to the issues have worked.
So I work on two sites, both of which are on the same host with the same PHP Admin set up (different accounts and domains, sites are not affiliated). One of them uses MySqli perfectly, without issues, while the other either gives a database selection failure, localhost access denied with password as "NO" or a blank page when I attempt to replace the MySql with MySqli.
The deprecated code:
$host = "LOCALHOST";
$usr = "USER";
$pwd = "PASSWORD";
$dbname = "DATABASE";
$connection = #mysql_connect($host,$usr,$pwd);
if(!$connection){
die("No connection.");
}
mysql_select_db($dbname,$connection);
The MySqli I am attempting (identical to the site that MySqli works perfectly on):
$host = "LOCALHOST";
$usr = "USER";
$pwd = "PASSWORD";
$dbname = "DATABASE";
$connection = mysqli_connect($host,$usr,$pwd);
if (!$connection) {
die("No connection.");
}
$db_select = mysqli_select_db($connection, $dbname);
if (!$db_select) {
die("Database selection failed: " . mysqli_error($connection));
}
So the PHP error codes did not work at all and it had nothing to do with the local host or port, so I first checked to make sure mysqli_connect was on. Next, I ran a simple test where I echoed a random word before each mysqli command.
echo 'Passed.';
global $c_mysqli;
$conn = new mysqli($host, $usr, $pwd, $dbname);
echo '<br>Passed global mysqli.';
I found that my second pass was not working, and then proceeded to do an error message. An error showed up then. However, nothing I did fixed the issue... then I realized that one of my files, the header file, is the meat and potatoes and if it were to fail then everything else would fail too.
Sure enough, one of my lines of code contained a mysql connection and not mysqli. Long story short, double check and triple check to make sure that all
$statement = mysql_query("STATEMENT");
$query = mysql_fetch_array($statement);
Appear as
$statement = $conn->query("STATEMENT");
$query = mysqli_fetch_array($statement);

Keeping MYSQL connection open

Is it possible to keep MySQL connection open from the beginning of a php file and close it at the end of that particular script?
If yes please help me because I'm having problem with this:
Prevent clash in MYSQL database
Thank you all.
This is how I open and close connection with my database:
$host = "localhost";
$root = "myusername";
$pass = "mypassword";
$DBname = "mydatabase";
$link = mysqli_connect($host, $root, $pass, $DBname);
function close_connection($link) {
$link ->close();
}
I call the function close_connection() whenever I want to stop the connection with my database. Hope this helps!
-Ed

how to connect php with mysql

I am very new to php. I am trying to make a sign up page with php. So simple but I can't. I've searched across google how to connect it. They said below.
<?PHP
$user_name = "root";
$password = "";
$database = "user";
$server = "127.0.0.1";
mysql_connect($server, $user_name, $password);
print "Connection to the Server opened";
?>
I've successfully created my user database with full set of data in phpmyadmin. And running Apache and Mysql in control panel. I set the password in phpmyadmin and I changed it in $password="mypassword";. But there is no print on my web page. I think the above code is correct and I am having problems before this state. Such as the location of my database, I don't where to put it or just created on myadmin is fine. Thank you for reading my problem and kindly advice me for the beginner course.
<?php
$user_name = "root";
$password = "";
$database = "user";
$server = "127.0.0.1";
// Create connection
$con=mysqli_connect($server, $user_name, $password, $database );
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try mysqli instead of mysql and here is why
no no . . sorry . My html path . . on desktop
PHP, in this context, is a server side programming language. You must request the script from a server that will pass it through a PHP interpreter before sending it to the browser.
Type http://localhost/etc/etc into the browser's address bar. Don't open the PHP program directly in your browser (e.g. by double clicking the file in Windows Explorer).
If you are going to use procedural style function calls with the mysqli interface:
$con = mysqli_connect('localhost','myuser','mypassword','mydb')
or die("Error " . mysqli_error($con));
If you are going to use object oriented style with mysqli interface:
$mysqli = new mysqli('localhost', 'myuser', 'mypassword', 'mydb');
Reference: http://php.net/manual/en/mysqli.construct.php
If you are going to use PDO interface
$dbh = new PDO('mysql:host=localhost;dbname=mydb', $user, $pass);
Reference: http://php.net/manual/en/pdo.connections.php

Database code is not connecting in chat program using php

<?php
// MySQL database connection file
$SERVER = "127.0.0.1"; // MySQL SERVER
$USER = "root"; // MySQL USER
$PASSWORD = "admin"; // MySQL PASSWORD
$link = #mysql_connect($SERVER,$USER,$PASSWORD);
$db = mysql_select_db("website");
?>
This is a database connecting code for chat program but it not connecting,Can any one pls help me to correct this code?
Drop the # infront of mysql_connect, it's used to suppress error which you don't want.
Also you need to check the return value of mysql_connect which is there in $link and make sure that it is not false before you proceed and to a DB select. Calling the function mysql_error when an error occurs gives you the reason for the error.
$link = mysql_connect($SERVER,$USER,$PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}

Categories