0

For some reason my sql query is not executing and the error message is not printing in php. Here is the code:

    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    $sql = "DELETE FROM data WHERE id='".$_GET['id']."'";
    echo $sql;
    $result=mysql_db_query($sql);
    if(!$result) {
        $msg = "ERROR: ". mysql_error();
        echo $msg;
    }

I know its vulnerable for sql injection right now but im going to fix that after i get it working. Also, if i copy what $sql prints and paste it into phpmyadmin it works and it does go into the if statement.

8
  • Do you have PHP error-reporting enabled? Commented Aug 15, 2011 at 21:38
  • 1
    mysql_db_query() is a depreciated function. Commented Aug 15, 2011 at 21:39
  • Have you activated display_errors and ajusted your error_reporting level? Commented Aug 15, 2011 at 21:39
  • 1
    also, the first argument of mysql_db_query is supposed to be the database connection, not the query. Commented Aug 15, 2011 at 21:40
  • foo.php?id=' OR 1=1 -- I just deleted all records in your data table. Please read up on SQL injection, then go learn PDO. Commented Aug 15, 2011 at 21:43

3 Answers 3

2

You want to use mysql_query(), not mysql_db_query().

And why do you enclose all your variables in quotes?

Sign up to request clarification or add additional context in comments.

Comments

1

Use mysql_query() instead of mysql_db_query() which is deprecated.

1 Comment

i was using mysql_query() and it wasn't working so i tried out mysql_db_query() out of desperation and just forgot to swith it back before posting.
0

http://www.php.net/manual/en/function.mysql-db-query.php

mysql_db_query

This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

instead use mysql_query

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.