This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
I am receiving the following errors
Warning: mysql_query()
[function.mysql-query]: Access denied
for user 'anticub1'#'localhost' (using
password: NO)
Warning: mysql_query()
[function.mysql-query]: A link to the
server could not be established
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
When I submit the form everything goes into the database so I am not sure why I am getting that error...
<?php
$submit=$_POST['submit'];
$text=$_POST['text'];
if ($submit) {
$connect = mysql_connect("localhost","anticub1_shout","root12")
or die("could not connect");
mysql_select_db("anticub1_shoutbox") or die("could not find the db");
mysql_query("INSERT INTO comments VALUES ('','$text')");}
$query = mysql_query("SELECT * FROM comments");
while ($row = mysql_fetch_assoc($query)) {
$post = $row["posts"];
echo "$post"."<br>";
}
?>
The warning:
Warning: mysql_query() [function.mysql-query]: Access denied for user 'anticub1'#'localhost' (using password: NO)
Is NOT produced by your example script! because you provide a password there and its an other username.
$connect = mysql_connect("localhost","anticub1_shout","root12")
Conclusion: You are searching at the wrong place. Search trough all your code for mysql_connect("localhost","anticub1")
Related
This question already has answers here:
Warning: mysql_query(): 3 is not a valid MySQL-Link resource
(4 answers)
Closed 9 years ago.
I am new to programming and following online tutorial.
I got an error please help.
Connected successfully
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/www/divitiae.net.co/make_my_tables.php on line 22
below is line 22
include_once("php_includes/db_connect.php");
$query = mysql_query($db_connect, $tbl_users);
if ($query === TRUE) {
echo "<h3>user table created OK :) </h3>";
} else {
echo "<h3>user table NOT created :( </h3>";
}
The error you are getting means that the $db_connect variable you are passing to the mysql_query function is not a MySQL connection object, as would be returned by the mysql_connect function.
Your php_includes/db_connect.php should contain something like:
$db_connect = mysql_connect('hostname', 'username', 'password');
mysql_select_db('databasename');
This will connect to the database on server hostname using the username and password provided. It then selects the database databasename as the database to perform operations on.
Warning!
You really, really, REALLY should not use the mysql_* set of functions because they are deprecated and you should use either the mysqli_* functions or implement PDO, as those are more flexible, safer, supported, and have better features.
I began learning to code a few days ago and I am having some issues with mysql_real_escape_string, specifically with a login.php.
The error messages:
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'elegant'#'localhost' (using password: NO) in /home/elegant/public_html/php/login.php on line 3
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/elegant/public_html/php/login.php on line 3
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'elegant'#'localhost' (using password: NO) in /home/elegant/public_html/php/login.php on line 4
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/elegant/public_html/php/login.php on line 4
Please enter a username and a password
Here is the code I have so far -- this code worked in localhost but once I put it online and imported the database tables, it gave me some issues:
<?php
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
if ($username&&$password)
{
$connect = mysql_connect("localhost","elegant_root","password;1") or die("Couldn't connect!");
mysql_select_db("elegant_ezworkstation") or die("Couldn't find database");
$query = mysql_query("SELECT * FROM users WHERE username=$username");
$numrows = mysql_numrows($query);
if ($numrows!=0)
{
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
if ($username==$dbusername&&$password==$dbpassword)
{
echo "You're in";
}
else
echo "Incorrect password!";
}
else
die("That user doesn't exist");
}
else
die("Please enter a username and a password");
?>
EDIT: I changed to mysqli and I got these errors:
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in /home/elegant/public_html/php/login.php on line 3
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in /home/elegant/public_html/php/login.php on line 4
Putting mysql_real_escape_string() after you connect to the db will work fine.
However, you should shift to mysqli or PDO. MySQL is deprecated now.
A few links to help you out
Moving from mysql to mysqli or pdo?
mysqli or PDO - what are the pros and cons?
The equivalent commands in mysqli and PDO for escaping would be mysqli_real_escape_string() and PDO::quote() respectively.
As people are pointing out, PDO is definitely the better alternative. Here is an answer I previously wrote comparing PDO with others.
PDO - real facts and best practice?
And another advantage of this will be that you don't need to use escaping functions if you use prepared statements with named parameters.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Warning: mysql_query(): 3 is not a valid MySQL-Link resource
I don't know why the $GUconnection resource isn't working on queries since I set it up as a global variable. The db connection is
$GUconnection = mysql_connect(serverip, username, password);
#mysql_select_db(dbname, $GUconnection) or die('Cannot connect to the database.');
The following query is located in an include file that is included in the file containing the mysql connection above:
global $GUconnection;
$GUresult = mysql_query("SELECT field FROM `tablename` WHERE field = 'hey' LIMIT 1", $GUconnection);
if(mysql_num_rows($GUresult)) {
$GUfile = mysql_fetch_assoc($GUresult);
}
The errors I got where
Warning: mysql_query(): 3 is not a valid MySQL-Link resource in /home/ on line 49
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/ on line 50
If I don't have the include and just paste the query directly underneath the connection then it works. What seems to be the problem?
Feel the power of google.
While writing your question on SO brings you no help, exact error message pasted into google's search box finds you complete solution. In much less time.
The connection may fail, so you should check for connection errors.
my code-
require 'database.php';
$title = mysql_real_escape_string($_POST['title']); //line 48
$cat = $_POST['cat'];
$txtart = mysql_real_escape_string($_POST['artbody']); //line 50
$date = date("d-m-y");
$q = "INSERT INTO tblarticle (art_title, art_cat, art_des, art_date) VALUES ('$title', '$cat', '$txtart', '$date')";
ERROR-->
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'#'localhost' (using password: NO) in C:\xampp\htdocs\shizin\admin\newArticle.php on line 48
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\xampp\htdocs\shizin\admin\newArticle.php on line 48
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'#'localhost' (using password: NO) in C:\xampp\htdocs\shizin\admin\newArticle.php on line 50
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\xampp\htdocs\shizin\admin\newArticle.php on line 50
But data is getting saved in DB but null titile and artbody fields
mysql_real_escape_string tries to connect to the local database, so it can fetch the settings it needs to escape the string correctly for that system.
You can tell the function which connection to use by passing in a link identifier as a second argument:
mysql_real_escape_string($string, $link)
Please check your MySQL username and password and make sure you're entering them properly in mysql_connect. According to the error you didn't run mysql_connect with a password, which may be causing these problems.
Check database.php connection strings, and the MySQL server is running as mysql_real_escape_string needs a valid open database connection to work.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
Here's my code:
<html>
<head>
</head>
<body>
<?php
$user = mysql_real_escape_string($_GET["u"]);
$pass = mysql_real_escape_string($_GET["p"]);
$query = "SELECT * FROM usario WHERE username = '$user' AND password = '$pass'";
mysql_connect(localhost, "root", "");
#mysql_select_db("multas") or die( "Unable to select database");
$result=mysql_query($query);
if(mysql_numrows($result) > 0){
echo 'si';
}
?>
</body>
</html>
And here's the error I get when I try to run it
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'#'localhost' (using password: NO) in C:\xampp\htdocs\useraccess.php on line 7
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\xampp\htdocs\useraccess.php on line 7
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'#'localhost' (using password: NO) in C:\xampp\htdocs\useraccess.php on line 8
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\xampp\htdocs\useraccess.php on line 8
Warning: mysql_numrows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\useraccess.php on line 16
You need to make your database connection before you call
mysql_real_escape_string
if you don't want to do that, use
mysql_escape_string
instead, since it doesn't care about the connection
Move mysql_connect(localhost, "root", ""); above $user = ...
You need to put single quotes around 'localhost':
mysql_connect('localhost', 'root', '');
Also, a blank root password? Really?