-3

I got the following data in MySQL's JSON-type column:

{"2": [2, 3], "3": [29], "71": "test"}

I need to search array value inside of attribute "2", which works just fine when variables are placed inside the query, but not when using PHP's PDO arrays.

$field_id = 2; 
$option_id = 2;
$query = "SELECT id FROM notes WHERE JSON_CONTAINS(data, '{\"$field_id\": $option_id }')";
try { 
    $stmt = $dbh->prepare($query); 
    $stmt->execute(); 
    $used_qty = $stmt->rowCount(); 
} catch(PDOException $ex) { 
    echo 'Query failed: ' . $e->getMessage(); 
    exit; 
}
// $used_qty returns 1 which is correct;

Binding through array returns 0:

$query = "SELECT id FROM notes WHERE JSON_CONTAINS(data, '?')";
try { 
    $stmt = $dbh->prepare($query); 
    $stmt->execute(array('{"' . $field_id . '": ' . $option_id . '}"')); 
    $used_qty = $stmt->rowCount(); 
} catch(PDOException $ex) { 
    echo 'Query failed: ' . $e->getMessage(); 
    exit; 
}

Can't figure out what I missed here. Please help. Thanks.

1
  • 1
    Hey Alex. Stop amending your question as you get answers. It makes the answers look like nonsence and is no use to anyone else that may find your question and be looking for a similiar answer.
    – RiggsFolly
    Sep 8, 2016 at 15:15

2 Answers 2

5

You quoted your placeholder:

$query = "SELECT id FROM notes WHERE JSON_CONTAINS(data, '?')";
                                                         ^-^--

which means it's NOT a placeholder, it's a string containing a question mark.

Remove those quotes.

11
  • Did you enable exceptions in PDO? If not, the try/catch is useless. PDO defaults to "fail by returning boolean false".
    – Marc B
    Sep 8, 2016 at 15:07
  • I swear you answered a very similiar question about an hour ago. It must be the current coursework somewhere
    – RiggsFolly
    Sep 8, 2016 at 15:08
  • 1
    @RiggsFolly: well, it is the start of the school year. it's never too early to start get others to do your homework/debugging for you...
    – Marc B
    Sep 8, 2016 at 15:09
  • I even remember unpacking the script on the other one in the same way as this. LOL
    – RiggsFolly
    Sep 8, 2016 at 15:10
  • 1
    wel,l that's because you have PDOException $ex, but are using $e. But still, you got an exception, therefore the query failed. But you can't get the message, because you're handling the exception wrong.
    – Marc B
    Sep 8, 2016 at 15:17
0

After enabling PDO exceptions:

$dbh = new PDO($dsn, $dsn_user, $dsn_password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

I got clear error message saying that only value can be accepted as a second parameter:

Query failed: SQLSTATE[22032]: <>: 3141 Invalid JSON text in argument 2 to function json_contains: "The document root must not follow by other values." at position 8. https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html#function_json-contains

But there is a third optional [path] parameter available: JSON_CONTAINS(json_doc, val[, path])

Which is exactly what I needed:

$query = "SELECT id FROM notes WHERE JSON_CONTAINS(data, ?, ?)";
try { $stmt = $dbh->prepare($query); $stmt->execute(array($option_id, '$."' . $field_id . '"')); $used_qty = $stmt->rowCount(); } catch(PDOException $ex) { echo 'Query failed: ' . $ex->getMessage(); exit; }

Thanks to Marc B.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.