Forum Moderators: coopster

Message Too Old, No Replies

mysql_affected_rows() returning -1

         

createErrorMsg

9:36 pm on Nov 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mysql_affected_rows() in my script is returning a value of -1. I know that this means the query was invalid, but I cannot for the life of me figure out what is invalid about my query. I was hoping someone might be willing to take a look...

Here's the query code:

$query = "INSERT INTO " . $table . " (" . $field_names . ") VALUES(" . $field_values . ")";
$result = mysql_query($query);
print(mysql_affected_rows());

The query string is constructed from strings stored in the mentioned variable, and it was the details of those strings I thought might be to blame for the problem, but if I use print($query);, this is the output I get:

INSERT INTO book_review ('meta_keywords', 'meta_description', 'title', 'author', 'illustrator', 'publisher', 'synopsis', 'review', 'questions', 'writing', 'phonemic', 'phonics', 'fun') VALUES('$meta_keywords', '$meta_description', '$title', '$author', '$illustrator', '$publisher', '$synopsis', '$review', '$questions', '$writing', '$phonemic', '$phonics', '$fun')

Which looks FINE to me (long, but fine). But then maybe I'm so close to it right now that I'm overlooking the obvious. Anyone see anything wrong with this query? If not, can you think of potential causes for this problem?

Thanks in advance for any input.

cEM

dreamcatcher

10:50 am on Nov 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi createErrorMsg,

In your INSERT query, you have the table names in between apostrophes:

For example:

INSERT INTO book_review ('meta_keywords', 'meta_description' etc etc

Only the values should be between apostrophes, NOT the table names.

INSERT INTO book_review (meta_keywords, meta_description, etc etc

Hope that helps.

:)

coopster

2:05 pm on Nov 22, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yep, you don't need to quote table names or column names in SQL. Only if they are reserved words or contain special characters, which really isn't a good practice anyway, in my humble opinion.

In the MySQL GRANT and REVOKE Syntax [dev.mysql.com] pages we find this...


When specifying quoted values, (you should) quote database, table, or column names as identifiers, using backticks (``'). Quote hostnames, usernames, or passwords as strings, using apostrophes (`'').

Basically, if a database, table, or column name as identifier, is not legal as an unquoted identifier, refer to it as a quoted identifier using backticks. But, as I said before, in order that you won't have to worry about it, don't create these values as anything otherwise ;)

Resource:
[dev.mysql.com...]