Jump to content

receive an warning of mysql_real_escape_string


mark103

Recommended Posts

Hi guys,

 

I am having a problem of deleting the rows in the database. I just receive two warnings of mysql_real_escape_string which they are:

 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'myusername'@'localhost' (using password: NO) in /home/username/public_html/mysite.com/delete.php on line 11

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in  /home/username/public_html/mysite.com/delete.php on line 11 failed

 

The error are jumping on this line:

    return mysql_real_escape_string($value); 

 

 

Here it the full code:

 

<?php
session_start();
    define('DB_HOST', 'localhost');
    define('DB_USER', 'username');
    define('DB_PASSWORD', 'password');
    define('DB_DATABASE', 'databasename');


function clean($value)
{ 
    return mysql_real_escape_string($value); 
} 

$id = clean($_GET['id']);

if ($id != NULL)
{
$query = @mysql_db_query(_DB,"DELETE FROM table1 WHERE $id = 'id");
$deleted = @mysql_affected_rows();

if($deleted > 0) {
    echo("worked");
    } else {
        echo("failed");
}
}else{
echo("failed");
}
@mysql_close($link);
?>

 

I have input the correct password, so what's wrong??

Link to comment
Share on other sites

if you would remove all the @'s you put in your code to hide the php errors that are occurring on each one of those mysql_ instructions, you would probably learn that you are not making a connection to the database server.

 

Also, mysql_db_query() is depreciated and should be replaced with a call to mysql_select_db() and you should use mysql_query() to perform queries.

Link to comment
Share on other sites

Thanks for your help, I have make some change:

 

<?php
session_start();
    define('DB_HOST', 'localhost');
    define('DB_USER', 'username');
    define('DB_PASSWORD', 'password');
    define('DB_DATABASE', 'databasename');

function clean($value)
{ 
    return mysql_real_escape_string($value); 
} 

$id = clean($_GET['id']);

if ($id != NULL)
{
$query = mysql_select_db("DELETE FROM table1 WHERE id = '$id'");
$deleted = @mysql_affected_rows();

if($deleted > 0) {
    echo("worked");
    } else {
  echo("failed");
}
}else{
echo("failed");
}
@mysql_close($link);
?>

 

 

However, I still getting the same errors. Any idea?

Link to comment
Share on other sites

You have defined your database connection info, but have not actually connected to your database!

 

You still haven't connected to the database.

 

Ken

 

I know that I still haven't connected to the database. In which line I need to change it with?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.