Warning: mysql_connect(): Access denied for user - php

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.

Related

Warning: pg_query() expects parameter 1 to be resource, object given in C:\xampp\htdocs\grafig\read.php on line 6

What's wrong in the code??
<?php
$host = "localhost";
$dbuser = "tesdb";
$dbpass = "123456";
$dbname = "tesdb";
// script koneksi php postgree
$dbcon = new PDO("pgsql:dbname=$dbname;host=$host", $dbuser, $dbpass);
//$query ="SELECT * FROM air_tanah.pembayaran";
$query ="select * from air_tanah.pembayaran";
$result = pg_query($dbcon, $query) or die('Query failed');
// output result
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo " Denda: " . $line['denda'] ." Penyimpan: " . $line['Penyimpan'] . "<br/>";
}
// free result
pg_free_result($result);
// close connection
pg_close($dbcon);
?>
and error like this
Warning: pg_query() expects parameter 1 to be resource, object given in C:\xampp\htdocs\grafig\read.php on line 12
Query failed
The code which you have entered does not allow us to really answer to your problem, we would need to know what is assigned to $dbcon variable.
pg_query expects there to be an instance of a Resource which holds the connection. Such resource is created using pg_connect or pg_pconnect method so we would need to see the contents of your db_con.php file: be sure to remove any credentials (hide them with * symbols for example).
It seems that something else actively sets the variable: are you sure that you have created a connection using pg_connect and not PDO for example (which would answer why you have there an object and not resource).

PHP to access to a .db file

I am new to PHP and trying to create a search field that searches through my database file. I am putting my database file rc3.db inside the same folder that contains my PHP file and trying to connect it with mysqlite, however I
Tried $con = mysqli_connect("localhost:8888","user","password","rc3.db"); but errors,I don't have a user nor a password. Also tried $con = mysqli_connect(); it works but I'm not sure if which database it is connecting to. I also did the single arguement with rc3.db as below, but it is probably mistaking that for the hostname, which is the the first parameter that the method takes in.
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
<?php
//connection to the database
$con = mysqli_connect("rc3.db");
echo "Connected to MySQL<br>";
?>
</body>
</html>
Warning: mysqli_connect(): (HY000/2005): Unknown MySQL server host 'rc3.db' (22) in /Applications/MAMP/htdocs/searchform.php on line 9
Call Stack
# Time Memory Function Location
1 0.0005 229848 {main}( ) ../searchform.php:0
2 0.0005 230072 mysqli_connect ( ) ../searchform.php:9
As I understand your database is not a mysql db but a sqlLite database.
If that so, you can use the sample code bellow to access the data.
$db = new PDO('sqlite:rc3.db');
$query = $db->prepare("Your sql query here");
$query->execute();
while($row = $query->fetchObject())
{
//do your staff
}
Update 1
As #Sean pointed correctly in the comments as an alternative you can use sqlite_open
$con = sqlite_open('rc3.db', 0666, $error))
The connection to the database using the extension mysqli.
$link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link));
use PDO like in code below. And read this https://renenyffenegger.ch/notes/development/web/php/snippets/sqlite/index
It really helped me
<?php
$db = new PDO("sqlite:tg.db"); // file tg.db in local folder
$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $db -> query('SELECT * FROM tgdata'); // select all data from my table 'tgdata'
foreach ($query as $row) {
var_dump($row);
}

PHP Connection issues on one page, but not the previous (same connect code)

would appreciate if somebody could help me figure out this error. On my first page, let's call it "index.php", I have code at the start of my body to connect to my database and there is no problem. Connection is made and a variable from the database is displayed on the page. I have another page which uses pretty identical code for connecting, however I am receiving this error on my page:
Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /var/sites/s/ssangar.com/public_html/PMWebsite/projects.php on line 36
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /var/sites/s/ssangar.com/public_html/PMWebsite/projects.php on line 36
Warning: mysql_numrows() expects parameter 1 to be resource, boolean given in /var/sites/s/ssangar.com/public_html/PMWebsite/projects.php on line 37
My php code:
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
...
</head>
<body>
<?php
$connect = mysqli_connect("server", "username", "password", "database");
$user = $_SESSION['username'];
$query="SELECT * FROM Projects";
$result=mysql_query($query);
$num=mysql_numrows($result);
?>
...
<?php
while($row = mysqli_fetch_array($result)){
echo "<div class='col-xs-6 col-sm-3 placeholder'>";
echo "<h4>". $row['title'] . "</h4><br/>";
echo "". $row['description'] ."<br/>";
echo "Due: ". $row['deadline'] ."";
echo "<span class='text-muted'></span>";
echo " </div>";
}
mysqli_close($connect);
?>
</div>
</body>
</html>
I also receive an error for the code near the bottom to display the contents of the database:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /var/sites/s/ssangar.com/public_html/PMWebsite/projects.php on line 83
Worked out by getting rid of the $num=.... line, it worked perfectly!
new code:$connect = mysqli_connect("server", "username", "pw", "");
$user = $_SESSION['username'];
$query = "SELECT * FROM Projects";
$result = mysqli_query($connect, $query);

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

looping through php table is not working [Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given]

I am getting this error on my index.php page where i have also included the code
<?pho
require 'connect.php';
?>
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given
<?php
$sql = "SELECT * FROM 'menulinks'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
echo '<li>', $row['linktitle'], '<li>';
}
?>
Here is my connect function
<?php
$con = mysql_connect('localhost', 'root', '') or die('Sorry, we could not connect');
mysql_select_db('philipsnewsite', $con) or die('Sorry, we could not connect');
?>
You have quoted your table name in single-quote characters, which MySQL interprets as a string literal. You should either use backticks, or no quote characters at all:
$sql = "SELECT * FROM `menulinks`";
This syntax error caused the mysql_query() function to return false, which you can test as follows:
$result = mysql_query($sql) or die(mysql_error());
Also note, as documented in a big red box at the top of the manual page for mysql_query():
Suggested alternatives
Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
mysqli_query()
PDO::query()

Categories