Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I search for a pattern in a serial number?
    primarykey
    data
    text
    <p>I have a database which contains the following :</p> <pre><code>pnid partnumber desc code ==== ========== ==== ==== 1 123-12345 hdd CMYK </code></pre> <p>So, the <code>code</code> field contains the pattern of the serial number I am looking for. What I would like to do is that I would like to fetch all the codes from my database, then check if a user input matches the code and display the part number of the matching code.</p> <p>So saying that my input is <code>ECMYK123456</code> then the function would return <code>123-12345</code>.</p> <p>I am really at lost. All I have is my basic PDO connection.</p> <pre><code>&lt;?php function checkPattern($inputString) { $dbuser = "root"; $dbpass = "123456"; $dbconn = new PDO('mysql:host=localhost;dbname=3parsfdb',$dbuser,$dbpass); $dbconn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbconn-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $query = $dbconn-&gt;query("SELECT * FROM `partnumber` WHERE '%$inputString%' LIKE `ctcode` LIMIT 1"); foreach ($query as $row) { return $row['partnumber']; } } echo checkPattern("ECMYK12345"); ?&gt; </code></pre> <p>When I try the function, it returns nothing. What is wrong with the code?</p> <p>Final Code :</p> <pre><code>&lt;?php function checkPattern($inputString) { $dbuser = "root"; $dbpass = "123456"; $dbconn = new PDO('mysql:host=localhost;dbname=3parsfdb',$dbuser,$dbpass); $dbconn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbconn-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $query = $dbconn-&gt;query("SELECT * FROM `partnumber` WHERE '$inputString' like concat('%', ctcode, '%') LIMIT 1"); foreach ($query as $row) { return $row['partnumber']; } } echo checkPattern("ECMYK12345"); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload