简体   繁体   中英

How can I check column sql Belong to the same row

My database is called pica It has the following table called gallery

Example:

gallery==>

id   pic1
__  ___
1    picture1.jpg
2    picture2.jpg
3    picture3.jpg

How can I check column sql Belong to the same row in php

Example: I want to check that picture1.jpg Belong to id 1 and picture2.jpg Belong to id 2 After that I get a message Verified , It's that possible ?

You can achieve this with a query like this:

$row = $db->getOne("SELECT count(*) as count 
                    FROM pica WHERE id = 1 
                    AND pic1 = 'picture1.jpg'");

if ($row['count'] > 0) 
{
     print 'message Verified';
}

Assuming you want to check $php_pic and $id are in same row or not

Do it this way :

Method 1

 /*Fetch records*/
      $sql = 'SELECT id,pic1 from gallery where id = $id';
      $rs= $mysqli->query($sql);  /* fetch details */

      $id = $rs['id'];
      $pic1 = $rs['pic1'];

    /* match using php */
        if ($id == $php_id and $pic1 == $php_pic)
        {
           /* they are in same row*/
        }

Method 2

   /*Fetch records*/
      $sql = "SELECT id,pic1 from gallery where (id = $id and pic1=`$php_pic`)";
      $rs= $mysqli->query($sql);  /* fetch details */
      $row_cnt = $rs->num_rows;             


    /* match using php */
        if ($row_cnt >= 1)
        {
           /* they are in same row*/
        }

You can use a condition for this:

   IF EXISTS (SELECT * FROM pica WHERE id=1 AND pic1='picture1') 
    SELECT 'verified' 
    ELSE 
    SELECT 'failed'

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