简体   繁体   中英

How to write a dynamic SQL query in PHP?

Hello fellow 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. 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-.

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. 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.

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.

More queries need to be written now to balance the game, please help me out, I am stuck.

Thanks in advance, Inder

$type = ($arow['type']==0) ? "Episode" : "Movie"; or $type = $arow['type'] ? "Episode" : "Movie";

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