tags:

views:

111

answers:

4

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 = mysql_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.

A: 

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.

cletus
I have updated my code with the `$connection` var.
Brian Ojeda
I want to thank you for your help.
Brian Ojeda
+2  A: 

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.

Ignacio Vazquez-Abrams
You might be right because some of my other user defined functions weren't working neither. I stripped them before I posted code. Can you connect another page, like functions.php, with require_once() function when using jQuery AJAX? Meaning... one page will ajax post to another page, that requires 4 other pages that have predefined functions, and that page will return data.
Brian Ojeda
AJAX just initiates another request to the web server. There really isn't all that much different between it and a normal hit from a browser.
Ignacio Vazquez-Abrams
If that is the case then I have defined var $connection. It is the same one I use account all my pages. At my question, I have add the code to define $connection.
Brian Ojeda
Your creation of `$connection` is failing. Find out why.
Ignacio Vazquez-Abrams
Look at the above code I have posted. I have no database errors.
Brian Ojeda
Brian Ojeda
A: 

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).";
bobeagan
I have updated my code with the `$connection` var. Also check the reply to Frank's comment. I do escape my code, just that I removed the call for the function when I posted it here.
Brian Ojeda
Try doing `var_dump($q, $connection);` right before you run your query. That should show you what each of the variables that you are passing into your mysql_query() actually are.
bobeagan
I want to thank you for your help.
Brian Ojeda
A: 

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.

Brian Ojeda