How to show only the records exactly the same as those typed?

Asked

Viewed 69 times

0

I have this code that shows all records as long as the initial number or letter is the same, but I wanted to show only the records that are exactly the same as those typed in the search field, or else show at most 5 records.

<?php 
include 'conn.php';

$letter = $_GET["letter"];


$sql = "SELECT id,palavra from interpretacao_cpp where pronta != 0 AND palavra LIKE '".$letter."%'" ;

$result = $conn->query($sql);


$sql2 = "SELECT DISTINCT(SUBSTR(`palavra`, 1, 1)) As abc from interpretacao_cpp where pronta != 0 ORDER BY abc" ;

$result2 = $conn->query($sql2);

?>

2 answers

2


Taking as an example one of your queries:

$sql = "SELECT id,palavra from interpretacao_cpp where pronta != 0 AND palavra LIKE '".$letter."%'" ;

Placing limit of 5 results, using the limit:

$sql = "SELECT id,palavra from interpretacao_cpp where pronta != 0 AND palavra LIKE '".$letter."%' limit 5" ;

Or exactly like the term typed:

$sql = "SELECT id,palavra from interpretacao_cpp where pronta != 0 AND palavra = '".$letter."'" ;

In case you don’t know, I advise you to take a look at sql Injection.

  • OK Benilson, I opted for the limit 5 function, but the data appear in descending form (article 107, article 11, article 1), when it should be article 1, article 11, article 107... etc.

  • Usa order by together with limit, "order by desired field_asc limit 5"

  • Okay it’s already fixed, I thank you.

0

The correct code to answer the question is as follows, limiting the number of records shown and sorting them by one of the table fields:

$sql = "SELECT id,word from interpretacao_cpp Where pronto != 0 AND word LIKE '". $Letter." %' ORDER BY id asc limit 5" ;

  • To improve this question, how to show only the records with a capital letter associated with the main record? The code above shows the records that include the initial number that was typed up to the limit of 5 (1, 11, 19, 107, 115...). Since the field to be searched has articles numbered with the word "Article" as default (Article 107 -, Article 107-A - ...), how to show only the records with the number typed, for example 107, and those with a capital letter and keeping the limit of 5 (example: Article 107 -, Article 107a -, Article 107b -, Article 107c -, Article 107a)?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.