简体   繁体   中英

How do I get specific data from a specific column in MySQL?

My data is like this:

在此处输入图片说明

I want to take all the data in the column dokumen_name which contains A12345.

This is my query:

<?php 
        require "init.php";
        $sql = 'SELECT Id, dokumen_name, 
               tanggal, aksi, lokasi
               FROM log WHERE dokumen_name LIKE "A12345"';

        $result=mysqli_query($connection,$sql);

        if(! $result ) {
            die('Could not get data: ' . mysql_error());
        }

        while($row=mysqli_fetch_array($result)){
        ?>
      <tr>
        <td><?php echo $row["Id"];  ?></td>
        <td><?php echo $row["dokumen_name"];  ?></td>
        <td><?php echo $row["tanggal"];  ?></td>
        <td><?php echo $row["lokasi"];  ?></td>
        <td><?php echo $row["aksi"];  ?></td>
      </tr>      
      <?php
        }
      ?>

It does not show anything. What did I miss?

If the LIKE statement is provided a simple string, such as A12345 , then it is equivalent to saying dokumen_name = 'A12345' . To avoid this, you can use wildcards % to query fields similar to, but not equal to, your search term. For instance:

$sql = 'SELECT Id, dokumen_name, 
           tanggal, aksi, lokasi
           FROM log WHERE dokumen_name LIKE "%A12345%"';

试试这个$sql = 'SELECT * FROM log WHERE dokumen_name LIKE "%A12345%"';

Learn like pattern

Create A sql query As Below. The percent sign and the underscore can also be used in combinations!

SELECT Id, dokumen_name,tanggal, aksi, lokasi
FROM log 
WHERE dokumen_name LIKE "%A12345%" ';

Tip: You can also combine any number of conditions using AND or OR operators.

Change Your sql query like below:

$sql = 'SELECT Id, dokumen_name, 
           tanggal, aksi, lokasi
           FROM log WHERE dokumen_name LIKE "%A12345%"';

For Like Condition you need to specify % with the string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM