doushai4890 2018-10-16 17:59
浏览 64
已采纳

如何使用mysql中的行创建变量?

I would like to create a file like this:

<?php
$cssMainVersion = "1.0.3";
$cssBootStrapVersion = "1.0.1";
$cssElseVersion = "1.0.4";
$jpgVersion = "1.0.1";
$jsMainVersion = "1.0.1";
$jsElseVersion = "1.0.1";
?>

I have created a mySQL table that holds the variable name, and the variable data. For example, the first row contains "cssMainVersion" and the second row contains "1.0.3".

How would I loop thorugh each row and print that data in a new file according to the format above?

  • 写回答

1条回答 默认 最新

  • drdu53813 2018-10-16 18:03
    关注

    You can create variables dynamically with php

    $query = mysqli_query($conn, 'SELECT id, cachename, cacheversion FROM cache');
    while($row = mysqli_fetch_assoc($query)) {
        $$row['cachename'] = $row['cacheversion'];
    }
    

    the $$ will be evaluted from right to left, so, first $row['field'] will be replaced with this value, let say cssMainVersion, then we have $cssMainVersion

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?