I have created a php connection file as explorecalirfornia.php content as below.
#FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_explorecalirfornia = "localhost";
$database_explorecalirfornia = "explorecalirfornia";
$username_explorecalirfornia = "root";
$password_explorecalirfornia = "";
$explorecalirfornia = mysql_pconnect($hostname_explorecalirfornia, $username_explorecalirfornia, $password_explorecalirfornia) or trigger_error(mysql_error(),E_USER_ERROR);
I have include this php file in my page using command
<?php require_once('Connections/explorecalifornia.php'); ?>
when I test the php code using local host following error is throwing.
Please help me to complete my tutorial.
Either #mysql_pconnect($hostname_explorecalirfornia, $username_explorecalirfornia, $password_explorecalirfornia) or trigger_error(mysql_error(),E_USER_ERROR); //# infront of mysql_pconnect or use below written code
$mysqli = new mysqli(HOST, USERNAME, PASSWORD, DBNAME);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
mysql_ *functions are deprecated and will no longer be used in the future. Use mysqli_* functions or PDO instead. Befriend The PHP Manual.
$explorecalirfornia = new mysqli($hostname_explorecalirfornia, $username_explorecalirfornia, $password_explorecalirfornia);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
Related
I have a script and when I go to the website link(http://kodle.co.uk/login.php) the script comes back with this:
Warning: mysql_connect(): Access denied for user 'root'#'localhost' (using >password: NO) in /home/hkode/public_html/dbconnect.php on line 12
Warning: mysql_select_db(): Access denied for user 'hkode'#'localhost' (using >password: NO) in /home/hkode/public_html/dbconnect.php on line 13
Warning: mysql_select_db(): A link to the server could not be established in >/home/hkode/public_html/dbconnect.php on line 13
Connection failed : Access denied for user 'hkode'#'localhost' (using password: >NO)
The DBCONNECT.php script is as follows:
<?php
// this will avoid mysql_connect() deprecation error.
error_reporting( ~E_DEPRECATED & ~E_NOTICE );
// but I strongly suggest you to use PDO or MySQLi.
define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', '');
define('DBNAME', 'login_base');
$conn = mysql_connect("localhost", "root", "");
$dbcon = mysql_select_db(DBNAME);
if ( !$conn ) {
die("Connection failed : " . mysql_error());
}
if ( !$dbcon ) {
die("Database Connection failed : " . mysql_error());
}
Go to your database and check if user root actually does not require a password. If it does then insert it as a third parameter in the empty single quotes below. Now use the code below and you will be just fine. Happy new year.
<?php
$con = mysql_connect('REPLACE_THIS_WITH_IP_ADDRESS_OF_YOUR_SERVER', 'root', '');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("login_base", $con);
?>
define('DBPASS', '');
You want to replace '' with 'youractualpassword'
Then change
mysql_connect("localhost", "root", "");
to
mysql_connect(DBHOST, DBUSER, DBPASS);
On a further note the comment
// this will avoid mysql_connect() deprecation error.
error_reporting( ~E_DEPRECATED & ~E_NOTICE );
// but I strongly suggest you to use PDO or MySQLi.
Suggest you don't use these methods. You should only do this if you absolutely know what you are doing, which seems like you do not.
I highly suggest using something like laracasts to learn php.
Good luck
It seems like the password is not right for user root also i would suggest you to use the constants for connecting to mysql which u have already defined.
i would rewrite connection statement and subsequent statement something like this
$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
if ( !$conn ) {
die("Connection failed : " . mysql_error());
}else{ // connection succesful
$dbcon = mysql_select_db(DBNAME);
}
Try this
$conn = mysql_connect(DBHOST, DBUSER);
$dbcon = mysql_select_db(DBNAME, $conn);
Edited Check all passwords
mysql is deprecated you have to use mysqli or pdo. example of mysqli is ...
$hostname= "localhost";
$username = "root";
$password = "";
$dbname = "your_database_name";
$con = new mysqli($hostname, $username, $password,$dbname);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$sql = "your_query";
if ($con->query($sql) === TRUE) {
echo "On query result is successfull";
} else {
echo "Error: " . $sql . "<br>" . $con->error;
}
$con->close();
Newish to php. I have been trying to query a database, and I keep getting the exception thrown that the query could not be completed. I checked to make sure I was connecting to the database, and everything looked fine, until I dug deeper. It appears that the my code tells me that I am connecting to the database regardless of what I put in for a password, username, or even if I do not have this data defined. I don't get it. Originally I had the following code in a function, but I put it no its own page to debug:
<?php
echo'this is working so far <br>';
/*$db = 'fake';
$host = 'localhost';
$password = 'wrong';
$user = 'root';
*/
$result = new mysqli($host, $user, $password, $db);
if(!$result){
echo 'did not connect to database';
throw new Exception('Could not connect to database');
}
else{
echo'connected to database';
return $result;
}
It always tells me I am connected to the database..
Because you are mixing Object oriented style with Procedural style To check database connection
Procedural style
<?php
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($link) . "\n";
mysqli_close($link);
?>
Object oriented style
<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
?>
Read http://php.net/manual/en/mysqli.construct.php
Note:
OO syntax only: If a connection fails an object is still returned. To check if the connection failed then use either the mysqli_connect_error() function or the mysqli->connect_error property as in the preceding examples.
Source
That means if($result) check is always true no matter what. So no, you don't have that database connection but you are verifying it incorrectly leading you to believe you do.
Your check should be
if($result->connect_error)
// no luck
else
// game on
You should check connect_errno property which stores the error code from last connect call.
$mysqli = new mysqli($host, $user, $password, $db);
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
This question already has an answer here:
The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead [duplicate]
(1 answer)
Closed 7 years ago.
Im developed PHP user login and registration system, i have a displayed always this error in my local host , how to fix it?
Deprecated: mysql_connect(): The mysql extension is deprecated and
will be removed in the future: use mysqli or PDO instead
(C:\wamp\www\registerations\db.php on line 9)
this is my code
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());
}
?>
<?php
$con = mysqli_connect("localhost","root","","register");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
// try this..
In case if you're still struggling, this is what you should be looking into:
<?php
$mysqli = new mysqli('localhost', 'your_username_here', 'your_password_here', 'your_db_name_here');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
PHP Manual
I have some problem
code line
$servername="us--------03.cleardb.net";
$user="be9-------0";
$pass="f9-------";
$db="ad_50f-------fa6a";
$con = mysql_connect($servername, $user, $pass, $db);
mysql_select_db($db);
My Requirement
I implement this line code could not be working than how should be connect database in ibm bluemix!!
plz given me solution..
mysql_connect is deprecated.
Use this code as an example:
$mysqli = new mysqli($mysql_server_name, $mysql_username, $mysql_password, $mysql_database);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
die();
}
Your composer.json needs to have:
{
"require": {
"ext-mysqli": "*"
}
}
https://github.com/IBM-Bluemix/php-mysql See: db.php and composer.json
To view the logs, use this command:
cf logs <yourappname> --recent
I'm following a tutorial on the net on PHP and MySQL.
I'm using Linux. I'm trying to establish a connection to a database but it is not working.
I have my database:
create database test;
create table user(name text, pass text);
insert into user values('john', '123');
and then my php:
<?php
$_host = "localhost";
$_dbuser = "root"
$_dbpass = "";
$_dbname = "test";
#mysql_connect("$_host", "$_dbuser", "$_dbpass") or die("could not connect");
#mysql_select_db("$_dbname") or die("no database");
echo "connection stablished";
?>
And the output of my file is just a blank tab on the browser.
What should I do to solve this? What am I doing wrong?
Thank you in advance. I'm very new to web programming.
$_host = "localhost";
$_dbuser = "root"
$_dbpass = "";
$_dbname = "test";
$link = mysqli_connect($_host, $_dbuser, $_dbpass, $_dbname);
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo "connection stablished";
Now...
Use mysqli_* functions as mysql_* are deprecated.
You will need $link variable later for queries.
As you're newbie read this: How can I prevent SQL injection in PHP?
Prepared statements FTW! Remember!