0

I have created an android application,by using this function in php i am creating a
a new user in database mysql.

function signUp($sName, $sMobile, $sAddress, $sEmail, $sPwd)
    {
    $sql = "insert into customers (name,mobile,address,email,pwd) values ('$sName','$sMobile','$sAddress','$sEmail','$sPwd')";
    $run = $this->query($sql);
    if ($this->result <= 0) {
        return false;
    } else {
        return $this->json('DATA');
    }
}

With the below function i am querying in database and returning the response in json format

function query($sql){
$query = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($query)){
$this->result[] = $row;
}
return $this;
}

but the response i am getting has an error i tried surpessing the warnings by using

@mysql_fetch_assoc($query)

It gave a proper response in browser but android gets null as response

Error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in

Warning: Cannot modify header information - headers already sent by (output started at

Help required New to PHP. Thanks in advance

  • 4
    don't suppress the error if you know there is. you have to solve it. by the way, stop using `mysql_query()` , use MySQLi or PDO instead – Raptor Apr 04 '13 at 11:25

3 Answers3

1

INSERT QUERY does not return RESULT SET

Only SELECT query returns result set

Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • Not Only SELECT return result set. At least these will return result set too: SELECT, SHOW, DESCRIBE, EXPLAIN – dpitkevics Apr 04 '13 at 11:26
  • So what should i do to return a result in json for android?. – user1208523 Apr 04 '13 at 11:26
  • Yep - an option would be to do a SELECT to get the latest inserted data: `SELECT name, mobile, address, email FROM CUSTOMERS ORDER id DESC LIMIT 1` (assuming your primary key is `id`). A more proper (but more complicated) way to do it is documented here: http://stackoverflow.com/questions/1367018/get-updated-row – alexpls Apr 04 '13 at 11:27
  • @user1208523 Return `true` when data is inserted in database and `false` when it does not insert data in database. – Yogesh Suthar Apr 04 '13 at 11:29
0

Well, the thing is that Your query is INSERT query and, according, to PHP Manual, in successful query it return true, not mysql object. So function "mysql_fetch_assoc($query)" cannot be ran because it requests mysql object as parameter not only TRUE.
Also, I recommend to start using MySQLi or PDO extensions, instead of mysql_* functions.

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.

dpitkevics
  • 1,238
  • 8
  • 12
0

You can't get result by

Insert Query
but you can get id of current inserted user by
mysql_insert_id()
and then you can use "Select Query" against that id.
Mubeen Ali
  • 2,150
  • 2
  • 18
  • 26