Php 包含动态值的字符串

Php 包含动态值的字符串,php,sql,string-concatenation,concat,Php,Sql,String Concatenation,Concat,我试图将一个字符串与数据库中的值关联起来,并根据“Operating_System”的值更改变量$protocol $result = mysqli_query($con,"SELECT CONCAT('<OS=\"',Operating_System,'\" Protocol=\"RDP\"/>') as nodestring FROM `serverlist`;"); while($row = mysqli_fetch_array($result)) { echo $ro

我试图将一个字符串与数据库中的值关联起来,并根据“Operating_System”的值更改变量$protocol

$result = mysqli_query($con,"SELECT CONCAT('<OS=\"',Operating_System,'\" Protocol=\"RDP\"/>') as nodestring FROM `serverlist`;");
while($row = mysqli_fetch_array($result)) {
    echo $row['nodestring'];
}
$result=mysqli_query($con,“选择CONCAT(“”)作为`serverlist`;中的nodestring”;
while($row=mysqli\u fetch\u数组($result)){
echo$row['nodestring'];
}
例如:如果“操作系统”的值!='然后将$protocol更改为“SSH”。
关于如何实现这一点,有什么帮助吗?

我想你看起来像这样:

$result = mysqli_query($con,
            "SELECT CONCAT(
                 '<OS=\"', Operating_System, '\" Protocol=\"', 
                        IF (Operating_System != 'Windows', 'SSH', 'RDP')
                  , '\"/>') as nodestring FROM `serverlist`;");
$result = mysqli_query($con,
               "SELECT Operating_System as operating_system FROM `serverlist`;");
while($row = mysqli_fetch_array($result)) {
    $protocol =  $row['operating_system'] != 'Windows' ? 'SSH' : 'RDP';
    echo '<OS="' . $row['operating_system'] . '" Protocol="' . $protocol . '"/>';
}
$result=mysqli\u查询($con,
“选择CONCAT(
“”)作为“服务器列表”中的nodestring;”;
但是不建议这样做,您可以在查询后使用PHP简单地实现这一点。在mysql中使用HTML根本不是一个好的做法。这只会使事情变得更加复杂,没有任何特殊的原因。所以我建议这样做:

$result = mysqli_query($con,
            "SELECT CONCAT(
                 '<OS=\"', Operating_System, '\" Protocol=\"', 
                        IF (Operating_System != 'Windows', 'SSH', 'RDP')
                  , '\"/>') as nodestring FROM `serverlist`;");
$result = mysqli_query($con,
               "SELECT Operating_System as operating_system FROM `serverlist`;");
while($row = mysqli_fetch_array($result)) {
    $protocol =  $row['operating_system'] != 'Windows' ? 'SSH' : 'RDP';
    echo '<OS="' . $row['operating_system'] . '" Protocol="' . $protocol . '"/>';
}
$result=mysqli\u查询($con,
“从“服务器列表”中选择操作系统作为操作系统”;
while($row=mysqli\u fetch\u数组($result)){
$protocol=$row['operating_system']!='Windows'?'SSH':'RDP';
回声';
}

第二个解决方案比第一个解决方案更具可维护性,代码更具可读性。

您是否考虑过使用mysql来实现这一点?我在想类似的事情,但我没有得到我的例子的语法正确。。。