简体   繁体   English

PHP / MySQL将查询结果显示为其他内容

[英]PHP/MySQL Show query result as something else

I have a MySQL table which I'm echo'ing out to a php file as a table. 我有一个MySQL表,我将它作为表回显到php文件中。

$query = "SELECT * FROM $tablename";

I want to be able to define alternative words when a particular word appears in one of the cells. 我希望能够在单元格之一中出现特定单词时定义替代单词。 eg the database contains the word "y" and I want to show "Yes!" 例如,数据库包含单词“ y”,我想显示“是!”

I'd like to do it hard coded, so a set of words always replaces another set of words, and also the option for two variables to be entered, one from the database, and the other the word to display. 我想对其进行硬编码,因此一组单词总是替换另一组单词,并且还可以选择输入两个变量,一个来自数据库,另一个用于显示。

I've been researching into CASE Statements, and switch blocks, however i'm not sure if I'm on the right path. 我一直在研究CASE语句和切换块,但是我不确定自己是否走对了。

Any help would be appreciated. 任何帮助,将不胜感激。

I think i need to be able to get my data more separate, to be able to do anything with it, this is what I have: 我认为我需要能够使我的数据更独立,能够对其进行任何处理,这就是我所拥有的:

$query = "SELECT * FROM $tablename order by timestamp desc";
$result = mysqli_query($connect, $query);

<?php while($row1 = mysqli_fetch_array($result)):;?>

            <tr>
                <td><?php echo $row1[5];?></td>
                <td><?php echo $row1[3];?></td>
                <td><?php echo $row1[0];?></td>
                <td><?php echo $row1[7];?></td>
            </tr>
            <?php endwhile;?> 

5,3,0 and 7 being the columns I want to show 5,3,0和7是我要显示的列

Yes case-when needs to be used: 是的,在需要使用时:

select
case when col1 = 'y' then 'Yes'
     when col1 = 'n' then 'NO' end
from tableName1;

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

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