开发者

Why do I get "mysql_query(): supplied argument is not a valid"

Why do I get a "mysql_query(): supplied argument is not a valid" for the first...

$r = mysql_query($q, $connection);

In the following code...

$bId    = trim($_POST['bId']);
$title  = trim($_POST['title']);
$story  = trim($_POST['story']);

$q  = "SELECT * ";
$q .= "FROM " . DB_NAME . ".`blog` ";
$q .= "WHERE `blog`.`id` = {$bId}";
$r = mysql_query($q, $connection);
//confirm_query($r);
if (mysql_num_rows($r) == 1) {      
    $q  = "UPDATE " . DB_NAME . ".`blog` SET
                        `title` = '{$title}',
                        `story` = '{$story}'
                    WHERE `id` = {$bId}";
    $r = mysql_query($q, $connection);
    if (mysql_affected_rows() == 1) {
        //Successful
        $data['success'] = true;
        $date['errors']  = false;
        $date['message'] = "You are the Greatest!";
    } else {
        //Fail
        $data['success'] = false;
        $data['error']   = true;
        $date['message'] = "You can't do it fool!";
    }        
}

I also get an "mysql_num_rows(): supplied argument is not a valid MySQL result resource" error too.

Side notes: I am using 1&1 Hosting (worst hosting ever), custom .htaccess file with one line text to enable PHP 5.2 (only way with 1&1 Hosting).


Extra stuff add after the questions was posted...

Here is how $connection is defined. It is on its own page called connection.php that is called up using the require_once function. It it is called up on every page that require a database connection including the one in question...

$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
    if (!$connection) {
        die("Database Connection Failed: </br>" . mysql_error());
    }
    $db_select = mys开发者_开发技巧ql_select_db(DB_NAME,$connection);
    if (!$db_select) {
        die("Database Selection Failed: </br>" . mysql_error());
    }

... I know it is working because this the same connect that I use for the page I have and I have no problems with it. I havent testing on my home server yet, but I am going to later to see if it is related to a 1&1 Hosting issue.

UPDATE: I am in the process of moving from 1&1 Hosting to HostMoster. 1&1 runs a PHP as CGI and runs PHP4 instead of PHP5 (you can make a custom .htaccess file to make it run PHP5). I will update you later.


The first would be because it's not a connection, and the second would be because it's not a query result because it wasn't a connection. Use mysql_error() to figure out what went wrong in the connection.


My guess is that $connection has not been properly opened. You should have a line like:

$connection = mysql_error($server, $user, $pass);

Also check mysql_error() to see the reason why it's failing.


I think that cletus meant to suggest that you need to have a mysql_connect() instead of mysql_error() before you attempt to perform a mysql_query(). It will probably look like this (with the exception that 1and1 may have given you a specific hostname to connect to for your database which you should use in place of localhost below):

//Make sure this goes before any of your other mysql_* functions
$connection = mysql_connect('localhost', 'yourUserName', 'yourPassword');

Passing $connection around for each of your queries isn't really necessary unless you have multiple database connections open concurrently that you are using and need to make sure to differentiate between the two when making query calls. Otherwise, mysql_query assumes that you are using the last database that you connected to.

Also, for security purposes as mentioned by Frank you should use mysql_real_escape_string() like so:

$q  = "SELECT * ";
$q .= "FROM " . DB_NAME . ".`blog` ";
$q .= "WHERE `blog`.`id` = ".mysql_real_escape_string($bId).";


Tested on my local system (via WAMP), I had no problems. At the time when I had problems, I was using 1&1 Hosting. 1&1 Hosting run PHP as CGI, which probably cause my problems. I couldnt handle allof the crap with 1&1 and switch to HostMonster. Now, I don't any issues. Plus, I rather use cPanel over 1&1's admin panel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜