0

I am having an ios app and I am calling a script from my online server, in order to insert a value into a table.

In this code:

//try to register the user
$result = query("INSERT INTO login(username, pass) VALUES('%s','%s')", $user, $pass);
if (!$result['error']) {
    //success
    login($user, $pass);
} else {
    //error
    errorJson($result['error']);
}

I always get into the else clause and it is printed Database error.

Note: errorJson is a function that makes a json output from a string in order to send it back to the iphone app.

A normal select from where plays normally, so there is no chance that I am not connected to the DB.

The whole code is here:

function register($user, $pass) {
    //check if username exists
    $login = query("SELECT username FROM login WHERE username='%s' limit 1", $user);
    if (count($login['result'])>0) {
        errorJson('Username already exists');
    }
       //try to register the user
       $result = query("INSERT INTO login(username, pass) VALUES('%s','%s')", $user, $pass); 
        if (! $result['error']) {
        //success
        login($user, $pass);
        } else {
    //error
              errorJson($result['error']);
        }
}

If the username exists the NSLog above is printed normally, so I guess I can connect to the DB.

and this is my query function:

//executes a given sql query with the params and returns an array as result
function query() {
    global $link;
    $debug = false;

    //get the sql query
    $args = func_get_args();
    $sql = array_shift($args);

    //secure the input
    for ($i=0;$i<count($args);$i++) {
        $args[$i] = urldecode($args[$i]);
        $args[$i] = mysql_real_escape_string($link, $args[$i]);
    }

    //build the final query
    $sql = vsprintf($sql, $args);

    if ($debug) print $sql;

    //execute and fetch the results
    $result = mysql_query($link, $sql);
    if (mysql_errno($link)==0 && $result) {

        $rows = array();

        if ($result!==true)
        while ($d = mysql_fetch_assoc($result)) {
            array_push($rows,$d);
        }

        //return json
        return array('result'=>$rows);

    } else {

        //error
        return array('error'=>'Database error');
    }
}
1
  • How about changing the error case to return $debug ? mysql_error($link) : 'Database error' Commented Aug 17, 2013 at 9:21

1 Answer 1

4

Your syntax is wrong, should be:

mysql_query($sql, $link):

Right syntax: mysql_query ( string $query [, resource $link_identifier = NULL ] )

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! You saved me so many hours!! I had taken this code from a tutorial and never paid attention to this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.