Query data from a MySql table with REGEXP

0

The first thing I do is capture the variable of a form and through a PHP file I make the query. But in this way I only capture the IDs that do not have asterisks, example 123456789, I need to show the information of the IDs in this way 123 * 456 * 789 or only 123 * 456.

This is the code that exists in PHP, but only captures numeric values.

$ id = $ _POST ['id'];

    $query = "SELECT * FROM h_vida WHERE h_vida.id= '$id'";

These are examples of existing IDs in the h_vida table

The form where the data to be searched is entered is not more than an input text of html like the following:

The structure of the table is as follows:

I know that REGEXP can possibly work for me, but I do not know how to apply it to call me exactly the data according to the ID that I type, with everything and asterisks, regardless of the number of groups that it may be.

What should I do so that the PHP code through the SQL query (REGEXP) shows me the data of the ID table with asterisk (s) typed?

    
asked by Oscar Celis 20.08.2018 в 20:00
source

1 answer

0

You have to make a function that every three characters insert the asterisk, I give you an example that I have been testing and it may be worth you. It is important to treat the id as a string and not as a number to avoid type conversion errors.

<?php

function formateaID($id){
   $id = (string) $id;
   $formatId = null;
   $j=1;
   for($i=0; $i<strlen($id); $i++) 
   { 
     $formatId .= $id[$i];
     if($j==3){
        $j=0;     
        $formatId .= '*';
     }
     $j++;
   }
   return substr($formatId, 0, -1);
}

Then you call her wherever you want by passing her id:

$id = formateaID($_POST ['id']);

I hope you serve, greetings.

    
answered by 20.08.2018 в 20:53