phpexplode() 来自 MySQL 数据库的变量 - 语法正确吗?

发布于 2025-01-02 21:57:03 字数 381 浏览 0 评论 0原文

我有一个表单,用户可以从工作清单(讲师、笔译员、口译员)中进行选择。结果存储为变量 ($employment) 并发送到 MySQL 数据库。它们由逗号空格分隔符分隔。

管理人员可以通过后端页面登录,并将其发送到列出不同作业的目录。单击“讲师”链接后,他们将进入一个页面,其中列出了所有单击“讲师”的用户。

为此,我使用了代码: $sql = "从 GDI_teacher WHERE $employment='讲师'中选择 *";

但是,如果用户选择了多个工作(例如,讲师和翻译),则该用户不会显示为使用上面的代码单击了讲师。

我相信我必须使用explode()函数,但不确定语法。我一直在猜测代码(来自其他论坛帖子),但没有成功。

我希望这一点很清楚。提前致谢!

I have a form where users choose from a checklist of jobs (Instructor, Translator, Interpreter). The results are stored as a variable ($employment) and sent to a MySQL database. They are separated by a comma-space delimiter.

Administration can login through a backend page and is sent to a directory where the different jobs are listed. When the Instructors link is clicked, they are taken to a page where all the users who clicked 'Instructors' are listed.

For this, I have used the code:
$sql = "select * from GDI_teacher WHERE $employment='Instructor'";

But if the user has selected more than one job (say, Instructor and Translator), this user does not show up as having clicked Instructor using the code above.

I believe I have to use the explode() function, but am unsure of the syntax. I have been guessing at the code (from other forum posts) but have been unsuccessful.

I hope this is clear. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何处潇湘 2025-01-09 21:57:04

explode() 仅在您运行查询并以 PHP 形式获得结果后才会为您提供帮助。您应该创建一个关系表来保存这些数据,但如果您想在 mysql 中获得所有正确的结果,您应该创建一个全文索引并针对该索引运行 MySQL 搜索。或者,您可以运行效率低得多的查询:

$sql = "select * from `GDI_teacher` WHERE `employment` LIKE '%Instructor%'";

在任一侧放置通配符将使字符串匹配:

'Instructor'
'[something],Instructor'
'Instructor,[something]'
'[something],Instructor,[something]'

但是!要非常小心...如果类别中可能存在文本重叠(例如,“讲师”和“科学讲师”),您需要创建一个更复杂的查询(如果您不打算( a)只需创建一个关系表(最佳解决方案)或(b)一个全文索引(好的解决方案))您的最后手段查询将如下所示:

$sql = "select * from `GDI_teacher` WHERE 
            `employment` LIKE 'Instructor'
            OR `employment` LIKE 'Instructor,%'
            OR `employment` LIKE '%,Instructor'
            OR `employment` LIKE '%,Instructor,%'"

如果您想尝试使用关系表,则需要设置上升一秒MySQL 表,因此您的结构将如下所示:

表 1

`UserID   |   UserName   |    UserEmail    |   etc`
..........................................................
1         |   John Doe   |   [email protected]  | [More]
2         |   Jane Doe   |   [email protected]  | [More]
3         |   Jake Doe   |   [email protected]  | [More]

表 2

UserID   |   Employment 
.........................
1         |   Instructor
1         |   Translator  
2         |   Instructor  
2         |   Translator  
2         |   Interpreter
3         |   Instructor

因此,每个用户都可以有多个可能的就业列表,由用户 ID 标识。现在,如果您想查看谁是讲师,您可以加入表格< /a> 或运行子查询。

只需确保 UserID 是表 1 上的自动递增主键,并且它在表 2 上建立了索引(但不是唯一的或主键)。根据表 2 的大小,您可能需要对两个 UserID 建立索引就业

explode() will only help you after you've run the query and have the results in PHP. You SHOULD create a relational table to hold this data, but if you want to get all the correct results in mysql you should create a fulltext index and run your MySQL search against that. Alternately, you could run the much less efficient query:

$sql = "select * from `GDI_teacher` WHERE `employment` LIKE '%Instructor%'";

Placing wildcards at either side will let the string match:

'Instructor'
'[something],Instructor'
'Instructor,[something]'
'[something],Instructor,[something]'

However! Be very careful... if there is any chance that there will be textual overlap in categories (for instance, 'Instructor' and 'Instructor of Science') you need to create a more complicated query (if you're not going to (a) Just create a relational table (BEST SOLUTION) or (b) a Fulltext-Index (OK SOLUTION)) your last-resort query would look like:

$sql = "select * from `GDI_teacher` WHERE 
            `employment` LIKE 'Instructor'
            OR `employment` LIKE 'Instructor,%'
            OR `employment` LIKE '%,Instructor'
            OR `employment` LIKE '%,Instructor,%'"

If you want to give the relational table a go, you'll need to set up a second MySQL table, so your structure would look like:

Table 1

`UserID   |   UserName   |    UserEmail    |   etc`
..........................................................
1         |   John Doe   |   [email protected]  | [More]
2         |   Jane Doe   |   [email protected]  | [More]
3         |   Jake Doe   |   [email protected]  | [More]

Table 2

UserID   |   Employment 
.........................
1         |   Instructor
1         |   Translator  
2         |   Instructor  
2         |   Translator  
2         |   Interpreter
3         |   Instructor

Thus each user can will have multiple possible employment listings, identified by the user's ID. Now, if you want to see who is an instructor you can join the tables or run a sub-query.

Just make sure that UserID is an auto-incremented primary key on Table 1, and that it is indexed (but not unique or primary) on Table 2. Depending on how large table 2 becomes, you may want to index both UserID and Employment

明媚如初 2025-01-09 21:57:04

来自 PHP 手册

<?php
// Example 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *

?>

希望对您有所帮助。

From PHP manual:

<?php
// Example 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *

?>

I hope it helps you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文