D
D
dMarley2014-07-09 22:53:33
PHP
dMarley, 2014-07-09 22:53:33

Why is only the first match returned when using the LIKE operator in mysql?

Actually, the essence is this: there is a sql query, there is a database.
The request itself looks like this:

SELECT `service` FROM `service_synonyms` WHERE `synonym` LIKE '%$query%'

The data is fetched like this (mysqli is used):
$sql = "SELECT `service` FROM `service_synonyms` WHERE `synonym` LIKE '%$query%' ";
$result = $this->_dbconn->query($sql)->fetch_array(MYSQLI_ASSOC);

When using PHP's object-oriented model, the first match is returned and that's it. But, if done procedurally, it returns all matches.
How to be and what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
svd71, 2014-07-09
@dMarley

Because there are two functions: fetch and fetchAll. After the first one, you need to move over the cursor using the next function. The second one will return all rows from the cursor in one call.

F
FanatPHP, 2014-07-10
@FanatPHP

The bottom line, in fact , is that
First: porting his code from the "procedural model" to the "object-oriented" author managed not to notice the cycle that was in the first, but disappeared in the second. But the OOP PHP model was to blame.
The second and most important thing: porting his old shit code with mysql_*, the author mastered only adding the letter i and arrows, leaving everything else as it is. Having received in the end exactly the same govnokod as before.
For reference:
The point of migrating from mysql to mysqli is not to add a dot wand. It's about using prepared expressions .
The point of OOP is not to write sticks with angle brackets. And that the object encapsulates within itself all the data processing work, returning the desired result immediately. As a result, the code that actually uses OOP should look something like this:

$sql = "SELECT `service` FROM `service_synonyms` WHERE `synonym` LIKE ?";
$result = $this->_dbconn->getAll($sql, "%$query%");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question