-1

I'm trying to check if my SQL query returned a result, and if everything is working well..

I'm doing that to validate one INSERT Query.

    $insert = "INSERT INTO offers (status, description logo) VALUES 
                '" . safe($add['status'][$key]) . "', 
                '" . safe($add['description'][$key]) . "',
                '" . safe($add['logo'][$key]) . "')";

$send = db_query($insert, '+');

The db_query($insert, '+') comes from a switch statement that i created, to not repeat the same thing over and over again:

    case '+':
    while ($resultset = mysql_fetch_row($q)) {
        $val[] = $resultset[0];
    }
    break;

To validate my Query, i use this:

    if(!$send){
     echo 'something went wrong';
   } else {
    echo 'everything is ok';
}

But it show me the same message "everything is ok", even when i put something wrong on my Query:

$insert = "INSET INTO offers (statOs, description logo) VALUES 
            '" . safe($add['status'][$key]) . "', 
            '" . safe($add['description'][$key]) . "',
            '" . safe($add['logo'][$key]) . "')";

How can i fix that?

  • An `INSERT` statement doesn't return rows. You can check, how many rows were affected. – VMai May 25 '14 at 13:52
  • You are also missing a comma between description and logo, and mis-spelt status in the second code. – Andy G May 25 '14 at 13:53
  • `INSET INTO offers`? No, don't tell me you're actually using this. A typo for sure, right? `INSERT INTO offers` or is that part of the intentional? – Funk Forty Niner May 25 '14 at 13:54
  • Add error reporting to the top of your file(s) `error_reporting(E_ALL); ini_set('display_errors', 1);` during development if you want to check for errors. – Funk Forty Niner May 25 '14 at 13:55
  • Of course it was intentional – user3647731 May 25 '14 at 13:55
  • That's what I thought. Just (double) checking ;-) – Funk Forty Niner May 25 '14 at 13:56
  • It makes not much sense to ask about code that you don't show and which is not obvious. And if you don't want to repeat yourself too much, use a more modern database client API than `mysql_*` which does not offer resultset iteration. – hakre May 25 '14 at 14:13

1 Answers1

0

An INSERT statement doesn't return rows. You can check, how many rows were affected. You can use

$rows = mysql_affected_rows();

See PHP manual, mysql_affected_rows() and please look at the red box and stop using the old mysql_* functions. Consider moving to mysqli or PDO.

VMai
  • 10,156
  • 9
  • 25
  • 34