简体   繁体   English

如何在PHP中编写动态SQL查询?

[英]How to write a dynamic SQL query in PHP?

Hello fellow StackOverflowers, 你好,StackOverflowers,

As modifications on my website were needed in result of massive growth and suggestions from the public, I needed to modify my database, which I have already done to adjust it to the visitor suggestions. 由于大量增长和公众的建议需要对我的网站进行修改,因此我需要修改数据库,我已经做了相应的调整以适应访问者的建议。 Right, very confusing so I'll just get to the point: 是的,非常令人困惑,所以我要讲到重点:

Current query: 当前查询:

$arow=mysql_fetch_assoc(mysql_query("SELECT * FROM animelist WHERE id = '".$_REQUEST['id']."'"));
$placeholders = array(' ', ',', ':');
$replacements = array('-', '', '');
$title=str_replace($placeholders, $replacements,$arow['name']);
$title=preg_replace("/[^a-zA-Z0-9\s-]/", "", $title);
$link=$title."-".$arow['id'];

$link="Stream-".$title."-Episode-".$row1['episodes_id']."-".$row1['language']."-".$row1['id'];

Initially the part -Episode- was only needed to be named/written as '-Episode-', however to user suggestions and I completely agree, it needs to be dynamic aswell. 最初,仅需要将部分-Episode-命名/编写为“ -Episode-”,但是根据用户的建议,我完全同意,它也必须是动态的。 Lets say (using the terms for reference only) at first the website only had Episodes and not Movies, but now also has Movies. 可以说(首先使用这些术语仅供参考),该网站仅包含剧集而不是电影,但现在也具有电影。 So we want this part to be dynamic aswell. 因此,我们希望这部分也具有动态性。 For this we use database information, I have made a column 'type' INT(1) in the table 'items' I suggest 0 to be -Episode- and if value under type is 1 then I suggest it to be -Movie-. 为此,我们使用数据库信息,我在表“ items”中创建了列“ type” INT(1),我建议将0设为-Episode-,如果type下的value为1,则建议其为-Movie-。

Now the question is how do I correctly implement it in the query? 现在的问题是如何在查询中正确实现它? I understand more queries need to be made similar to the one from $title or $row1. 我知道需要进行更多查询,类似于$ title或$ row1的查询。 this is what I have so far, but it is not complete yet, because I don't know how to: 这是我到目前为止的内容,但尚未完成,因为我不知道如何:

$link="Stream-".$title."-.$type.-".$row1['episodes_id']."-".$row1['language']."-".$row1['id'];

$type=$arow['type']

Now there should be a code, which I am not sure of how to write correctly, which makes the condition that if type = 0 then echo Episode, elseif type = 1 then echo Movie. 现在应该有一个代码,我不确定如何正确编写,这使条件为:如果type = 0,则回显Episode,否则,如果type = 1,则回显Movie。

I greatly appreciate the time you took to read this through and hope you can help me out. 我非常感谢您花时间通读本文章,并希望您能为我提供帮助。

Edit: Assume $row1, is fetched from table named 'videos' and not from 'animelist', however the table 'videos' each video has an 'id' but also has an column named 'anime_id', this anime_id is equal to the 'id' in 'animelist', in short videos id is the post, and anime_id is the category. 编辑:假定$ row1,是从名为“ videos”的表而不是从“ animelist”获取的,但是表“ videos”的每个视频都有一个“ id”,但也有一个名为“ anime_id”的列,此anime_id等于“ animelist”中的“ id”,简而言之,视频id是帖子,anime_id是类别。

More queries need to be written now to balance the game, please help me out, I am stuck. 现在需要编写更多查询来平衡游戏,请帮助我,我被困住了。

Thanks in advance, Inder 在此先感谢Inder

$type = ($arow['type']==0) ? $ type =($ arow ['type'] == 0)吗? "Episode" : "Movie"; “ Episode”:“电影”; or $type = $arow['type'] ? 或$ type = $ arow ['type']? "Episode" : "Movie"; “ Episode”:“电影”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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