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
Related
I have an issue with PHP script not connecting to MySQL database. Below code always generates error:
$dbconn = #mysql_connect('SFDC1', '<username>', '<password>');
if (!$dbconn)
{
die('Error connecting to DB!');
}
There is no issue if I connect to the database using MySQL workbench with same credentials. Issue only occurs during the communication between PHP and MySQL.
Any help on debug of this issue?
Try this one;
$dbhost = "localhost";
$dbuser = "username";
$dbpass = "pass";
$dbname = "DBname";
if(!#mysql_connect($dbhost,$dbuser,$dbpass)) {
echo "Cannot connect";
die();
} else
mysql_select_db($dbname);// or report();
Use this code to connect mysql.
<?php
$dbconn = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$dbconn ) {
die('Could not connect: ' . mysql_error());
}
?>
Hi I am making a login/register database. I am not hosting the database on the same server so how do I make it point to my separate domain? Also if you can tell me what the root is pointing to... Thx
Here are my files:
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());
}
?>
Links (my scripts and original scripts): https://docs.google.com/document/d/1u60X_mKd9z548qrh_VjdyTx3hziiZiYPLUa_rJX11mQ/edit?usp=sharing
For starters, DON'T USE mysql_connect()! Use either mysqli or PDO. Second, don't have your application log in to MySQL as root; create an application-specific unprivileged user (with a password!) for this purpose.
You'll need to create your unprivileged user with connect privilege from the web server's IP or DNS address, and instead of connecting to localhost your PHP will need to connect to the DB server's IP or DNS address, like so:
$dsn = "mysql:host=mysqlserver.mynetwork.com;dbname=register";
try
{
$conn = new PDO($dsn, $appuser, $apppasswd);
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
You could try the following example
$server = "192.168.1.1"; //server name or IP Address
$username = "root"; //database username
$password = "password"; //database password
$database = "mydatabase"; //database to connect to
$connection = mysqli_connect( $server, $username, $password, $database);
if (!$connection){
die("Database Connection Failed" . mysqli_error());
}
else{
//sql statement
}
mysqli_close($connection);
Thanks
$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();
}
Based on suggestions and a better understanding of what I need I have changed my question:
My site is protected with .htaccess. Im trying to get the current user and a few other variables and save them to a MySQL database. Everything works except the current user part.
Heres my code:
<?php
$_SERVER["PHP_AUTH_USER"] = "rep";
$hostname = "hostname";
$username = "username";
$dbname = "dbname";
$password = "password";
$usertable = "usageStats";
$yourfield = "your_field";
$con = mysql_connect($hostname, $username, $password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname, $con);
$sql="INSERT INTO usageStats (Bid, PRid, User)
VALUES
('$_POST[BID]','$_POST[branding]','$_POST[rep]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?>
I know that the below are both wrong. How should the current user be passed instead of $_POST[rep]?
Wong:
$_SERVER["PHP_AUTH_USER"] = "rep";
and
'$_POST[rep]'
If you are using Basic Authentication you do not need to send the username explicitly. The browser will send it and you can access it using the PHP_AUTH_USER server variable:
$_SERVER['PHP_AUTH_USER']
Also checkout the HTTP Authentication article in the documentation.
<?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());
}