简体   繁体   中英

How do i get record by id in php

Here is my database view

在此处输入图片说明

I need to get title by using student id 232. But student id is stored in array like 192,229,232. How can i retrive 232 records only?

Please guide me..

Thanks

you use IN clause for get record from array

<?php
$sql = 'SELECT * 
          FROM `table` 
         WHERE `id` IN (' . implode(',', array_map('232', $array)) . ')';

Use FIND_IN_SET

Eg.

SELECT * FROM event WHERE FIND_IN_SET(232,student);

You can use LIKE condition to fetch results that contain this student id:

SELECT * FROM `event` WHERE student LIKE '%232%';

This will return you all rows that contain '232' in student column.

But if you have an opportunity to change your table structure I would suggest you to extract student column to a pivot table.

You could create event_student table that would contain just two columns: event and student . And each row would link particular student to a particular event . The data could be easily extracted with a simple JOIN:

SELECT * FROM `event` e
    INNER JOIN `event_student` es ON e.eventID=es.event
    WHERE es.student=232;

If you store connections between student and events this way you will gain flexibility in your queries and avoid possible mistakes that may occur when using the answer suggested above. Imagine that you have a student with id 2324 or 12323. Both of them contain desired '232' string and both will match the '%232%' pattern and lead to returning you wrong data.

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