mysqli_affected_rows problem

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Using PHP 5.2.9 and MySQL 5.0.77, I have the following code to add a line
to table "newz":

    $query="INSERT INTO newz (field1, field2,...) VALUES

    $q1=mysqli_query($dbc, $query);

I have a database connection file included (stored outside public_html)
that sets the connection to $dbc by using mysqli_connect().

The code works fine other than the final line setting $test.  The insert
occurs without a hitch and shows up correctly when the database is
subsequently queried by SELECT.  (I have run the INSERT both directly in
MySQL and from the PHP program, without any problem.)

However, the value of $test returns -1, indicating an error.  

The Manual says:
 "Procedural style:
int mysqli_affected_rows ( mysqli $link )

Returns the number of rows affected by the last INSERT, UPDATE, REPLACE or
DELETE query. "
*  *  *

    Procedural style only: A link identifier returned by mysqli_connect()
or mysqli_init() "
*   *    *

"Return Values

An integer greater than zero indicates the number of rows affected or
retrieved. Zero indicates that no records where updated for an UPDATE
statement, no rows matched the WHERE clause in the query or that no query
has yet been executed. -1 indicates that the query returned an error. "

What am I doing wrong?  mysqli_affected_rows should return the integer 1,
since one row has, in fact, been successfully inserted using link
identifier $dbc.

Re: mysqli_affected_rows problem

Mason Barge wrote:
Quoted text here. Click to load it

Your problem is:


$q1 is not a valid SQL statement, therefore an error results.
mysqli_affected_rows() returns the result from the last query.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: mysqli_affected_rows problem

On Mon, 01 Mar 2010 12:06:37 -0500, Jerry Stuckle

Quoted text here. Click to load it


Not even sure where that came from (I suspect I copied the section from
another similar script I did and it must have just been a ghost).  Thanks
and sorry for the dumb question.

Re: mysqli_affected_rows problem

El 01/03/2010 17:43, Mason Barge escribió/wrote:
Quoted text here. Click to load it

The manual also says:

mysqli::query -- mysqli_query — Performs a query on the database

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or
EXPLAIN queries mysqli_query() will return a result object. For other
successful queries mysqli_query() will return TRUE.

Your code does not check this so it takes for granted that the query can
never fail.

-- - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web:
-- Mi web de humor satinado:

Site Timeline