$link = mysql_connect('localhost', $username, $password);
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
I get this error:
Could not connect: Access denied for user 'www-data'#'localhost' (using password: NO)
Why?
Edit:
$username="root";
$password="root";
$database="test";
function Save($name)
{
$link = mysql_connect('localhost', $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
#mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO test (name)" .
"VALUES ('" . $name . "')";
mysql_query($query);
mysql_close();
}
Did you give $username and $password values? Something like this?
$username="root";
$password="";
Your $password variable is empty ((using password: NO)) then you trying log into www-data user without password, and server result is access denied. Propably this user have setted password.
From your edit, the issue appears to be a scoping one.
You're attempting to make a connection inside a function where the $username, $password and $database variables are defined outside that function.
I suggest you read the Variable Scope section of the manual and turn up your error reporting as #netcoder suggests in the question comments.
I think the problem is that, you are trying to connect to the database twice.
Somewhere in your code, you are already trying to connect to the database with username as "www-data"...... and again you are connecting to the database with username "root".
Also the password you are providing with the username "www-data" is wrong, thats y you getting the error message.
$username="root";
$password="root";
$database="test";
function Save($name)
{
global $username;
global $password;
global $database;
$link = mysql_connect('localhost', $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
#mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO test (name)" .
"VALUES ('" . $name . "')";
mysql_query($query);
mysql_close();
}
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();
I have a script where I need to connect to MySQL db. But somehow the connection is failed. Can anyone help me to check the problem? Thanks ahead.
$username = "root";
$password = "";
$hostname = "localhost";
$dbname = "vti_ctes_demo";
$con = mysql_connect($hostname, $username, $password);
if (!$con) {
die('Could not connect: ' . mysql_error());
}else
{
echo 'connected';
}
// make foo the current db
$db_selected = mysql_select_db($dbname, $con);
if (!$db_selected) {
die ('Can\'t use vti_ctes_demo : ' . mysql_error());
}else
{
echo 'connected';
}
The moment I run the query, I get this error:
Can't use vti_ctes_demo : Access denied for user ''#'localhost' to database 'vti_ctes_demo'
I have set the username as 'root', but it seems like it can't receive the username. Btw, first connection is successful. Just that, the moment when it's connected to the db, then the error appeared.
You need grant privileges to the user "root" if you want external access to database vti_ctes_demo.
Use this to grant permission to the user
Hey i am using Mamp on imac and my problem is that when i hit the submit button (on a post form) to enter the data then nothing shows up and the database remains empty.
Here is my code :
<?php
define('DB_NAME', 'demob');
define('DB_USER','brom');
define('DB_PASSWORD','****');
Define('DB_HOST','localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect : ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('cant use' . DB.NAME . ' : ' .mysql_error());
}
$value = $_POST['input1'];
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_close();
?>
You are not executing a query.
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_query($sql);
You should know that, the method you are using to connect to mysql is deprecated now. please read up about PDO or mysqli
Keep getting same error message
(Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/00/7882800/html/Connections/dueslogin.php on line 27
Unable to connect to database! Please try again later.)
<?php
//Variables for connecting to your database.
//These variable values come from your hosting account.
$hostname = "TarHeelsDues.db.7882800.hostedresource.com";
$username = "TarHeelsDues";
$dbname = "TarHeelsDues";
//These variable values need to be changed by you before deploying
$password = "************";
$usertable = "DuesLogin";
$yourfield = "your_field";
//Connecting to your database
mysql_connect($hostname, $dbname, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($TarHeelsDues);
//Fetching from your database table.
$query = "SELECT * FROM $usertable";
$result = mysql_query($query);
if ($result) {
while($row = mysql_fetch_array($result)) {
$name = $row["$yourfield"];
echo "Name: $name<br>";
}
}
?>
change the mysql_connect line to
mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname);
A little mistake on there, you have to use $username not $dbname in mysql_connect.
mysql_connect($hostname, $username , $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($TarHeelsDues);
The correct syntax should be like this:-
mysql_connect($hostname, $username , $password) OR die ("Unable to connect with the server");
mysql_select_db($TarHeelsDues) or die('Cannot connect with the database!');
Ok, Ive been playing around with PHP and databases. Note, Im doing all my work on a webserver hosted by bluehost.
So I went into the cpanel and set up a database called gagalugc_stocks.
I also setup a username (test) and a password (password) and added them to be able to control the databse. Below is my script in an attempt to connect to it, not it is also on the server.
<?php
$connect = mysql_connect('localhost','test','password');
if(!$connect){die('Could not reach database!');}
mysql_select_db("gagalugc_stocks", $connect)
?>
-Thanks
NOTE: The problem is that it can never reach the database.
<?php
$connection = mysql_connect ('localhost', 'test', 'password')
or die ('<b>Could not connect: </b>' . mysql_error());
$result = mysql_select_db("gagalugc_stocks")
or die ('<b>Could not connect: </b>' . mysql_error());
?>
Didn't check for port when adding database make sure to specify host and port
(localhost:2083)
How about
<?php
$connect = mysql_connect('localhost:2083','test','password');
if(!$connect){die('Could not reach database!');}
mysql_select_db("gagalugc_stocks", $connect)
?>
For local system:-
$con = mysql_connect("localhost","root","");
if (!$con){
die('Unable to connect to the server ' . mysql_error());
}
mysql_select_db("databasename", $con) or die(mysql_error());
For website or remote server:-
$con = mysql_connect("localhost","username","password");
if (!$con){
die('Unable to connect to the server ' . mysql_error());
}
mysql_select_db("databasename", $con) or die(mysql_error());
for more please visit : Algosoftwares