Convert a search query into 1’s and 0’s and use as a variable

  mysql, php

Hello everyone and thanks for the time you will spend on this question.

Problem

I am trying to convert my search query into 1’s and 0’s to check for a match inside my database.

Basically, I fill my form input with this query: SF12345

The code checks for each chars if it’s numeric. If it is, it outputs a 1 if not it outputs a 0. So my code (SO’s code ๐Ÿ™‚ ) so far will output 0011111. So far, so good:

            $s = "SF12345";
        
            for ($i = 0; $i < strlen($s); $i++){
                
               $char = $s[$i];
               
               if (is_numeric($char)) {
                  echo "1";
               } else {
                  echo "0";
               }
            }

Question

How do I "implode" or "regroup" all these echo’s to use as one variable inside my search query below

$dbConn->query("SELECT * FROM products WHERE code = '".$s.'");

I want it to look like this

$dbConn->query("SELECT * FROM products WHERE code = '0011111'");

If anyone has any idea on how to approach or solve this issue, you’ll make one (very) happy man.

If it is important to know why I want to convert it to 1’s and 0’s I’ll be glad to explain.

Thank you very much, all help is much appreciated.

Source: Ask PHP

LEAVE A COMMENT