mysql_query() Access denied for user - php

I'm making a PHP and MySQL RPG game, and my changeEquipment page is not working. I'm getting these errors:
Warning: mysql_query() [function.mysql-query]: Access denied for user
'devinfa1'#'localhost' (using password: NO) in
/home/devinfa1/public_html/spellsword/changeEquipment.php on line 21
Warning: mysql_query() [function.mysql-query]: A link to the server
could not be established in
/home/devinfa1/public_html/spellsword/changeEquipment.php on line 21
Warning: mysql_query() [function.mysql-query]: Access denied for user
'devinfa1'#'localhost' (using password: NO) in
/home/devinfa1/public_html/spellsword/changeEquipment.php on line 27
Warning: mysql_query() [function.mysql-query]: A link to the server
could not be established in
/home/devinfa1/public_html/spellsword/changeEquipment.php on line 27
Here's my code:
<?php
// Get commonFunctions.php
require_once('commonFunctions.php');
// Start the session and connect to the database
session_start();
$con = mysql_connect("localhost","devinfa1_user","dbpass");
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("devinfa1_spellsword", $con);
// If logged in
if(isset($_SESSION['account_name'])) {
// If form is submitted
if(isset($_POST['submit']) || isset($_POST['submit_x'])) {
// If the hero has more than one weapon
if(hero_has_multiple_weapons()) {
// Set new weapon
mysql_query("UPDATE Hero_Weapons SET active_weapon = TRUE WHERE HW_hero_id =
(SELECT hero_id FROM Heroes WHERE hero_name = '$_SESSION[hero_name]')
AND HW_item_id = (SELECT item_id FROM Items WHERE item_name = '$_POST[weapon]')");
// Unset previous weapon
if(isset($_SESSION['hero_name'])) {
mysql_query("UPDATE Hero_Weapons SET active_weapon = FALSE WHERE HW_hero_id =
(SELECT hero_id FROM Heroes WHERE hero_name = '$_SESSION[hero_name]')
AND HW_item_id = (SELECT item_id FROM Items WHERE item_name = '$_SESSION[weapon_name]')");
}
}
// Close the database connection
mysql_close($con);
?>
So, the PHP should be connecting to my databse using the devinfa1_user username and dbpass password, but it's apparently trying to use the devinfa1 username with no password.
I have no idea why this is the case, because I have the exact same connection code and an update statement on my changeHero page and it works perfectly. Any ideas?

Pass the connection in with the query, per the manual : The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.
see if that works, it looks as if is overriding the last connection (ie in the call to hero_has_multiple_weapons() a db connection is opened) or your last connection didnt really connect

Related

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'usr'#'localhost' (using password: YES)

Warning: mysqli_connect(): (HY000/1045): Access denied for user
'usr'#'localhost' (using password: YES) in
/storage/emulated/0/htdocs/includes/condb.php on line 9 Failed to
connect to MySql: Access denied for user 'usr'#'localhost' (using
password: YES) Warning: mysqli_query() expects parameter 1 to be
mysqli, boolean given in /storage/emulated/0/htdocs/includes/condb.php
on line 19
Warning: mysqli_error() expects parameter 1 to be mysqli, boolean
given in /storage/emulated/0/htdocs/includes/condb.php on line 20
Unable to connect
Im trying to connect to the database using this code but im an unable to. I dont know what i actually did wrong. Ive tried googling and reading some articles but none of those work.
UPDATED CODE still getting the error message above.
$server = "127.0.0.1"; $username = "usr"; $password = "123"; $database
= "contents";
$conn = mysqli_connect($server,$username,$password,$database);
if(mysqli_connect_errno($conn)) {
echo "Failed to connect to MySql: ".mysqli_connect_error(); }
$sql = "INSERT INTO contents('subj','article','day') VALUES
('".$_POST['This is the subj']."', '".$_POST['This is the
content']."', '".$_POST['2019-02-01 10:15:59']."')";
if(!mysqli_query($conn,$sql)) {
die("Unable to connect".mysqli_error($conn)); }else {
echo "Connected"; }
?>
Still getting the same error above unfortunately it was not a typo error. Still couldn't get the solution to this problem it has been days. Can someone with problem solving skills help me.
You don't have to pass $conn inside mysqli_connect_errno()
remove $conn
$conn = mysqli_connect($server,$username,$password,$database);
if(mysqli_connect_errno()) {
echo "Failed to connect to MySql: ".mysqli_connect_error();
}
You can fix this error by changing your mysql username and password. There are many tutorials for changing mysql user and password with a little search

Warning: mysql_connect(): Access denied for user

I am a beginner .
I copied a simple php page on the server web http://www.barman-team.ir/tt.php
and i import sql data base and make user & and pass on data base (in cpanel)
this page work on local host( xamp) but dont run on server
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body dir="rtl">
<?php
$link=mysql_connect("localhost","uname","pass");
mysql_select_db("barmante_ebram",$link);
$q="SELECT * FROM site_sug";//
$r=mysql_query($q,$link);
$line= mysql_fetch_array($r);
//while($line== $result->fetch_assoc()){
echo $line['site_suggestion_id'].$line['site_suggestion']."<br>";
//echo $row["site_suggestion_id"].$row["site_suggestion_name"].$row["site_suggestion_date"].$row["site_suggestion"]."<br>";
//}
?>
</body>
</html>
error log is:
Warning: mysql_connect(): Access denied for user 'barmante_ebram_u'#'localhost' (using password: YES) in /home/barmante/public_html/tt.php on line 11
Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/barmante/public_html/tt.php on line 12
Warning: mysql_query() expects parameter 2 to be resource, boolean given in /home/barmante/public_html/tt.php on line 14
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/barmante/public_html/tt.php on line 15
Use this kind of debugging to trace error.
I guess your all warning is related to USERNAME or PASSWARD. use correct username and password. and it will resolve problem.
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
Use mysql_error() with each query so that you will get to know what is causing problem ( useful for beginner )
The first error is saying that your password to the username your supplied meaning that it can't connect to your mysql and the other error are saying that you should have provide resource to the 2nd parameter. You just interchanged the sequence of the parameters. Use the code below
<?php
$link=mysql_connect("localhost","uname","pass");
mysql_select_db($link,"barmante_ebram");
$q="SELECT * FROM site_sug";//
$r=mysql_query($link);
$line= mysql_fetch_array($r);
//while($line== $result->fetch_assoc()){
echo $line['site_suggestion_id'].$line['site_suggestion']."<br>";
//echo $row["site_suggestion_id"].$row["site_suggestion_name"].$row["site_suggestion_date"].$row["site_suggestion"]."<br>";
//}
?>
Note*: Don't use mysql_* functions, it is deprecated . Use mysqli or PDO prepared statements
Hope this helps you
Assuming that you have correct permissions for the user, the following code should work. It will tell you whether you have error in connecting to the sql server, or there is any error in your query.
<?php
$db_hostname = 'localhost';
$db_database = 'barmante_ebram';
$db_username = 'uname';
$db_password = 'pass';
$db_link= new MySQLi($db_hostname, $db_username, $db_password, $db_database);
if(! $db_link) //if($db_link->connect_errno > 0)
die("Could not connect to database server\n" + $db_link->connect_error);
$query = "SELECT * FROM site_sug";
$result = $db_link->query($query);
if(!result)
die("Error in query".$db_link->error);
$row = $result->fetch_assoc();
echo $row['site_suggestion_id'].$line['site_suggestion'];
?>
Note: Please always do error checking (if( !successful) die(" ")). Your code does not have any. Its a good practice to follow.

PHP Connection Error; Not allowed to connect to MySQL Server

I am trying to make a registration page where it adds the information to a SQL Table. Here is my PHP code to do this... Can anyone tell me what the error with this is?
<?php
$con=mysqli_connect("Correct Website is here","Correct Username","Correct Pass","Correct DB");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$DBsql = mysql_query("SELECT username, email from Profiles");
$sql = mysql_fetch_array($DBsql);
if(!($_POST['username'] == $sql['username']) && ($_POST['email'] == $sql['email'])) {
if ($_POST['password'] == $_POST['password2']){
$sql="INSERT INTO Profiles (firstName, lastName, username, email, password, region, group, verified)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[username]','$_POST[email]','$_POST[password]','$_POST[region]','member','no')";
}else{
echo '<script type="text/javascript">'
, 'window.alert("The passwords do not match.")'
, '</script>';
}
} else {
echo '<script type="text/javascript">'
, 'window.alert("That username or email is already in use.")'
, '</script>';
}
mysqli_close($con);
?>
It's giving these errors:
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/1130): Host '31.170.161.96' is not allowed to connect to this MySQL server in /home/a5349216/public_html/register.php on line 2
Failed to connect to MySQL: Host 'ip' is not allowed to connect to this MySQL server
Warning: mysql_query() [function.mysql-query]: Access denied for user 'username'#'localhost' (using password: NO) in /home/username/public_html/register.php on line 8
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/username/public_html/register.php on line 8
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/username/public_html/register.php on line 9
Warning: mysqli_close() expects parameter 1 to be mysqli, boolean given in /home/username/public_html/register.php on line 27
If your mysql database is not on the same server(localhost) you need to give it access from a remote server for that mysql user/database for your server. See this article for some steps that might help you. It sounds like it might be remote.
https://support.rackspace.com/how-to/mysql-connect-to-your-database-remotely/
Create new php file, just test with below syntax. let me know your error.
$con=mysqli_connect("Correct Website is here","Correct Username","Correct Pass","Correct DB");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

Having issues with my database? I am trying to have a section in my site where you can add a new neighborhood?

I am trying to add a section in my site where you can fill out a small form and it will add the neighborhood in for you. When I try to fill in the form and add the new neighborhood, I get the error below. Please let me know what I am doing wrong and why I can't get these to be added into the database? Thanks for any help!
Code:
require_once('db.php');
//set the registration variables
$name = addslashes($_POST['name']);
$builder = addslashes($_POST['builder']);
$builderURL = $_POST['builderURL'];
//connect to database
$link = mysql_connect($dbhost, $dbuser, $dbpwd);
if (!$link) {
die(strip_tags('Could not connect: '.mysql_error()));
}
//add this contest to the database
$stmt = "INSERT INTO hf_neighborhoods (name, builder, builder_url, created_on) VALUES " .
"('$name', '$builder', '$builderURL', NOW())";
$result = mysql_query($dbname, $stmt);
if(!$result){
die(strip_tags('Error Adding Neighborhood: '.mysql_error()));
}
//if we made it this far, return a success
echo 'success';
?>
DB Call:
<?php
$dbhost = 'localhost';
$dbname = 'delsur_2011_dev';
$dbuser = 'delsur2011';
$dbpwd = 'newsite!';
?>
Error:
<br />
<b>Warning</b>: mysql_query() expects parameter 2 to be resource, string given in <b>/var/www/vhosts/delsurliving.com/httpdocs/hf/php/addNeighborhood.php</b> on line <b>21</b><br />
Error Adding Neighborhood:
Now I am receiving this error when I try to change it to that new code? Is it just not connecting to the database at all now? I am lost, I wasn't the one that set this file up, I was just told to go and try to fix it so that is what I am trying to do now? Thanks for your help
Warning: mysql_real_escape_string(): Access denied for user 'apache'#'localhost' (using password: NO) in /var/www/vhosts/delsurliving.com/httpdocs/hf/php/addNeighborhood.php on line 8
Warning: mysql_real_escape_string(): A link to the server could not be established in /var/www/vhosts/delsurliving.com/httpdocs/hf/php/addNeighborhood.php on line 8
Warning: mysql_real_escape_string(): Access denied for user 'apache'#'localhost' (using password: NO) in /var/www/vhosts/delsurliving.com/httpdocs/hf/php/addNeighborhood.php on line 9
Warning: mysql_real_escape_string(): A link to the server could not be established in /var/www/vhosts/delsurliving.com/httpdocs/hf/php/addNeighborhood.php on line 9
Error Adding Neighborhood: No database selected
You have to change
$result = mysql_query($dbname, $stmt);
with
$result = mysql_query($stmt, $link);
However, using mysql_* functions is deprecated and its now considered a bad practice.
Instead you should be using PDO or MySQLi!
And, dont use addslashes, at least use mysql_real_escape_string
EDIT: You should be calling mysql_real_escape_String() after you connect to the database, and not before. And also, dont forget to call mysql_select_db()!
//connect to database
$link = mysql_connect($dbhost, $dbuser, $dbpwd);
if (!$link) {
die(strip_tags('Could not connect: '.mysql_error()));
}
mysql_select_db($dbname, $link);
// Sanitize your input at least!
$name = mysql_real_escape_string($_POST['name'], $link);
$builder = mysql_real_escape_string($_POST['builder'], $link);
$builderUrl = mysql_real_escape_string($_POST['builderUrl'], $link);
//add this contest to the database
$stmt = "INSERT INTO hf_neighborhoods (name, builder, builder_url, created_on) VALUES " .
"('$name', '$builder', '$builderURL', NOW())";
$result = mysql_query($stmt, $link);
...
...
...

having problems connecting to mysql db with php

I have a test db on godaddy, I have had a similar issue before but with the help of stackoverflow it was solved.. So I have actually used the same solution however now I am having a host of errors when I try to load my php file from the browser.
Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/97/8603797/html/countryQuery.php on line 14
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/content/97/8603797/html/countryQuery.php on line 14
Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/content/97/8603797/html/countryQuery.php on line 16
Warning: mysql_close(): no MySQL-Link resource supplied in /home/content/97/8603797/html/countryQuery.php on line 18
Database Output
I have read what the problem is and some of the things I have read suggest I am not connecting to the DB correctly.. but I am only doing what I have read/been told and dont really know what else I can do to solve the issue I am having...
this is my php code, if anyone has and suggestions or solutions I would greatly appreciate hearing from you.
<?
$hostname = 'mydatabasename.db.6666666.hostedresource.com';
$database = 'mydatabasename';
$username = 'myusername';
$password = 'secretsecret';
// establish a connection
$db = new PDO("mysql:host=$hostname;dbname=$database",$username,$password);
$query="SELECT * FROM `Countries`";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo "<b><center>Database Output</center></b><br><br>";
$i=0;
while ($i < $num) {
$country=mysql_result($result);
echo "<b>$country<br>";
$i++;
}
?>
the out put should just be a list of the country names I have in the countrys table of my database.. which has values in it.
You are mixing PDO and mysql_* functions.
Try something like this:
$db = new PDO("mysql:host=$hostname;dbname=$database",$username,$password);
$db->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql="SELECT * FROM `Countries`";
$numRows=$dbh->exec($sql);
echo "There were $numRows selected with:<b> $sql </b><br><br>";
$db=null;
unset($db);
When it comes to getting data from the PDO connection, I normally do something like this using the FETCH_INTO option:
$stmt = $db->query($sql);
$obj = $stmt->setFetchMode(PDO::FETCH_INTO, new userStructure);
// I do normally have a class matching the row I wan:
foreach($stmt as $userStructure)
{
$someObject->year=$userStructure->year;
$someObject->month=$userStructure->month;
unset($stmt);
}
Make sure that you include the port as well when using a PDO connection. This is looking like you are doing this in a hosted environment. Check the installation guide regarding the port used.

Categories