MySQL returns only one row

I have this simple PHP code:

$query = mysql_query("SELECT `title`, `url_title` FROM `fastsearch` WHERE `tags` LIKE '%$q%' LIMIT 5");
    $query2 = mysql_fetch_assoc($quer);
    print_r($query2);

It returns only this:

Array ( [title] => Kill Bill Vol 1. [url_title] => kill_bill_vol_1 )

I have 3500+ rows in a table, and running SQL in PhpMyAdmin works fine.

+3
source share
5 answers
$query = mysql_query("SELECT `title`,
                             `url_title`
                        FROM `fastsearch`
                       WHERE `tags`
                            LIKE '%$q%'
                       LIMIT 5");

while ($row = mysql_fetch_assoc($query)) {
    print_r($row);
}
  • You made a mistake $queryin your example
  • mysql_fetch_assoc()will return a string on every call, and FALSE- from strings. Take advantage of this by assigning it a variable in state. The loop while() $rowwill be the current line.
+7
source

Correctly, you incorrectly estimate results.

mysql_fetch_assoc()returns only one row at a time. Use a loop to read all the lines.

$query = mysql_query("SELECT `title`, `url_title` FROM `fastsearch` WHERE `tags` LIKE '%$q%' LIMIT 5");

$resultSet = array();
while ($cRecord = mysql_fetch_assoc($query)) {
  $resultSet[] = $cRecord;
}
+2
source

http://php.net/manual/en/function.mysql-fetch-assoc.php :

mysql_fetch_assoc — Fetch a result row as an associative array

, , , :

while ($row = mysql_fetch_assoc($result)) {
    echo $row["title"];
    echo $row["url_title"];       
}
+2

fetch_assoc() ,

+1

mysqli_fetch_assoc() - , print_r().

$query = "SELECT `title`, `url_title` FROM `fastsearch` WHERE `tags` LIKE '%$q%' LIMIT 5";
$result = mysqli_query($conn,$query);
while(null ! == ($query = mysqli_fetch_assoc($result))){
     print_r($query);
}
0

Source: https://habr.com/ru/post/1778868/


All Articles