On PHP, about using mysql or mysqli - php

I'm running MySQL server version 5.0.77 on Linux (Rocks Cluster Distro), and I'm trying to connect to the same database with both MySQL and MySQLi PHP functions. The problem is that when the MySQLi code fragment is executed it does work, but the MySQL fragment doesn't (in case it matters; not even on separate PHP files).
As shown in the code below I'm attempting a connection to the same database on the same host, using the same user and password but the MySQL function version fails.
(If it helps I've already tried the same code on MySQL server 5.6.20 running in Windows with the same details ((local)host, database, user, password) and both functions work).
What might be the problem? Any help is appreciated.
The output and code fragment is the following:
Code:
<?PHP
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tedDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Error [mysqli]: ".$conn->connect_error);
} else {
echo "Success [mysqli]";
}
echo "<br><br>";
$link = mysql_connect($servername, $username, $password)
or die("Error [mysql_connect]: ".mysql_error());
echo "Success [mysql_connect]";
?>
Output (LINUX):
Success [mysqli]
Error: Access denied for user 'root'#'localhost' (using password: NO)
Output (Windows):
Success [mysqli]
Success [mysql_connect]

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

Error 500 when I try to connect MySql with php's mysqli

I have written below php code to connect MySQL DB:
<?php
// Connect to MySQL
$servername = "localhost";
$username = "user";
$password = "A11b22c33&";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
However this code fails to make any connection. All I am getting is below error:
currently unable to handle this request.
HTTP ERROR 500
However if I remove the connection portion of my code then my php file working perfectly, i.e. I am getting echo as 'Connected successfully'
<?php
// Connect to MySQL
$servername = "localhost";
$username = "user";
$password = "A11b22c33&";
echo "Connected successfully";
?>
Can somebody help me to understand what went wrong? I have checked username and password, and they are perfect
I just installed mysqlnd using apt-get install php-mysqlnd, and it works fine.

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);

Which username/password do I use to connect to mySQL

So, I'm SUPER new to mySQL. Having issues connecting to my database with PHP. Hoping someone can point me in the right direction.
I can login to our Cpanel using my username/password. Using the web gui I was able to create a database.
Now when I try to connect to the database (or even just the server for that matter) using PHP I get an error:
Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'username'#'servername' (using password: YES) in /home/ropepart/public_html/techportal/sandbox/mysqltest.php on line 8
Connection failed: Access denied for user 'username'#'servername' (using password: YES)
To me, it seems like this is a username/password error. But I an using the same username/password that I use to login to the web gui. Why can I successfully login there, but can't login with PHP?
Here's my code (taken pretty much directly from W3Schools):
<?php
$servername = "servername";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
(Hopefully it's obvious that I've changed my servername/username/password for the purposes of this post)
Check the details correctly, By deafult
host = localhost
username = root
password = (No password leave it as empty)
database = (your database)
<?php
$connection = new mysqli('host','username','password','your_database');
if($connection->connect_error || $connection->error){
echo "error";
}else{
echo "done";
}
?>
you should add database name.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "demo"
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
//if you are using xampp you shoul locate this file you get information from this
passwords.txt
MySQL database username/password is not necessarily same as cPanel username & password.
Here is what you should do:
Login to your cPanel
From dashboard click "MySQL® Databases"
Add a new user from there (https://prnt.sc/kpiby9). Just fill username
and password (note down the password).
Add that user with your database by selecting both from the dropdown
(https://prnt.sc/kpidqx)
Now, use this username & password to connect with the database.
mysqli_connect(serverhostname,username,password,dbname);

mysql_connect() not working from class

Just moved my site to new hosting. On the new server mysql_connect() works fine if called without using classes or inside of the method but not from the _construct. When connecting from the _construct the following error displays:
Access denied for user 'root'#'localhost' (using password: NO)...my configuration defines the user not as root but as USERNAME and does provides a password.
PHP 5.6, MySQL 5.5, Linux
Class File:
function __construct(){
error_reporting(E_ERROR);
$dbhost = 'localhost';
$dbuser = 'USERNAME';
$dbpass = 'PASSWORD';
$dbname = 'DATABASENAME';
$this->urlAppPath = "http://www.myurl.com/CD/";
// connect to the database
$this->db = mysql_connect($dbhost, $dbuser, $dbpass) or die("Connection Error: " . $SQL);
mysql_select_db($dbname);
}
function validatePerson($personID) {
$SQL = "SELECT *,COUNT(*) AS total FROM zen_customers WHERE customers_classware_id = '$personID'";
$result = mysql_query( $SQL ) or die("Could not execute query 1 . ".$SQL." ".mysql_error()." & ". $dbhost ." ".$dbuser ." ".$dbpass." ".$dbname);
}
Call Method File:
require_once('./clsCart.php');
$myclass = new clsCart();
$result2 = $myclass->validatePerson($personID);
Note
If I define and call the connection within the file that I have been trying to call the method...that is instead of calling the method it works. If I define and call the connection within the method it works...but if I define and call the connection within the construct() it does not work...error "Access denied for user 'root'#'localhost' (using password: NO)". This seems to be specific to some servers...regardless of using PHP 5.6 or lower & MySQL 5.6 or lower.
So, if I use :
$dbhost = 'localhost';
$dbuser = 'USERNAME';
$dbpass = 'PASSWORD';
$dbname = 'DATABASENAME';
// connect to the database
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("Connection Error: " . $SQL);
mysql_select_db($dbname);
Outside of the class or within the method then it works...but, I should (as I have in the past) be able to connect within the construct().
This seems to be related to a server issue. From the __contruct() only the Database default user information is being recognized. Everywhere else in the applications uses the User created for the database, but the construct will only connect when using the default database user on this server. Other servers are not having this issue, such that any database user with all privileges work.
You have to use mysqli_connect. Please never use mysql_connect it deprecated in PHP 5.5.0 and onwards. If still you face issue please let us know.
Following code for mysqli_connect :
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Definition and Usage
The mysqli_connect() function opens a new connection to the MySQL server.
Syntax
mysqli_connect(host,username,password,dbname,port,socket);
Parameter Description
port Optional. Specifies the port number to attempt to connect to the MySQL server.
socket Optional. Specifies the socket or named pipe to be used.

Categories