0

These lines are from a php function on a web server, the client is an ios app, I get an error on the result2 query

$result = query("SELECT field1 FROM table1 WHERE field2='%s' limit 1", $value);
$row = mysql_fetch_array($result);
$result2 = query("SELECT field2 FROM table1 WHERE field3='%s' limit 1", $row['field1']);

on Xcode I get the error (in json):

{
    error = "The operation couldn't be completed. (Cocoa error 3840.)";
}

here is the definition of the function query

//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] = mysqli_real_escape_string($link, $args[$i]);
}

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

if ($debug) print $sql;

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

    $rows = array();

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

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

} else {

    //error
    return array('error'=>'Database error');
}
}

what's wrong with that query?

thanks

6
  • Did you define a custom function query()? If not, you have not called mysql_query(). Turn up error reporting and display errors on screen, always when developing code. error_reporting(E_ALL); ini_set('display_errors', 1); Commented May 1, 2014 at 14:19
  • The problem is not with the query; it's with your JSON being parsed. You probably have malformed JSON. Commented May 1, 2014 at 14:21
  • Also unless query() is a custom function that creates the sql command it looks like you are using prepared statements which is mysqli or pdo and you can't use mysql_fetch_array with it. Commented May 1, 2014 at 14:22
  • Just edited the question @michael-berkowski Commented May 1, 2014 at 14:24
  • yes @Pitchinnate it's mysqli Commented May 1, 2014 at 14:25

2 Answers 2

1

You are using mysqli_ functions in your query() function yet you are trying to use mysql_fetch_array() to get the results. You need to use: mysqli_fetch_array()

http://www.php.net/manual/en/mysqli-result.fetch-array.php

Actually it looks like your query() function does it for you. You shouldn't need to use a function at all. Look at this section of your query() function:

$rows = array();

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

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

However not sure why it says it is json cause it is just a normal array not a json array. So you should just use this in your code:

$row = $result['result'][0];

This will get the first row.

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

Comments

0

maybe you have to json_encode the result?

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

Comments

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.