Isset无法使用mysqli_query

时间:2017-09-17 09:10:45

标签: php variables if-statement mysqli isset

因此,当在我的程序中使用if!isset语句时,它不会识别变量已被设置。我已经设置好了,如果变量$ q没有设置,那么从我的mysqli数据库中获取查询。此查询将从我的数据库中获取最新的数据条目。此数据等于名为$ row的变量。稍后调用$ row变量以检索随后显示在网页上的记录。问题是if语句再次运行(刷新/重新加载页面时),即使从上次运行.php文件时设置了$ q。这是代码:

$db_user = "root";
$db_pass = "";
$db_name = "music_reviews_database";
$db_host = "localhost";

$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!isset($q)) {
    $q = mysqli_query($conn, 
            "SELECT album.Album, 
            artist.ArtistsName, 
            datereviewed.DateReviewed, 
            features.Features, 
            genre.Genre, 
            rating.Rating, 
            songname.SongName,
            link.link,
            songname.ID,
            comments.Comments
            FROM songname 
            INNER JOIN album ON album.ID = songname.ID 
            INNER JOIN artist ON artist.ID = songname.ID
            INNER JOIN datereviewed ON datereviewed.ID = songname.ID
            INNER JOIN features ON features.ID = songname.ID
            INNER JOIN genre ON genre.ID = songname.ID
            INNER JOIN rating ON rating.ID = songname.ID
            INNER JOIN link ON link.ID = songname.ID
            INNER JOIN comments ON comments.ID = songname.ID
            ORDER BY DateReviewed DESC LIMIT 1");
    $row = mysqli_fetch_array($q);
}

所以代码应该是这样的吗?

$q = mysqli_query($conn, 
        "SELECT album.Album, 
        artist.ArtistsName, 
        datereviewed.DateReviewed, 
        features.Features, 
        genre.Genre, 
        rating.Rating, 
        songname.SongName,
        link.link,
        songname.ID,
        comments.Comments
        FROM songname 
        INNER JOIN album ON album.ID = songname.ID 
        INNER JOIN artist ON artist.ID = songname.ID
        INNER JOIN datereviewed ON datereviewed.ID = songname.ID
        INNER JOIN features ON features.ID = songname.ID
        INNER JOIN genre ON genre.ID = songname.ID
        INNER JOIN rating ON rating.ID = songname.ID
        INNER JOIN link ON link.ID = songname.ID
        INNER JOIN comments ON comments.ID = songname.ID
        ORDER BY DateReviewed DESC LIMIT 1");
$row = mysqli_fetch_array($q);
file_put_contents(data.php, return var_export($row,true));
if (file_exists(data.php)) {
    $row = include(data.php);
}

2 个答案:

答案 0 :(得分:0)

您可以删除

if (!isset($q)) {

和结束括号。它们是不必要的。

如果您希望每次都避免运行查询以获得结果,请尝试缓存结果。

E.g。

从查询中获得结果后,将内容发送到文件系统中的某个文件($ path)

file_put_contents($path, '<?php return '.var_export($mysql_row,true).';?>');

然后你可以把它读出来:

if (file_exists($path)) $mysql_row = include($path);

类似于:

if (file_exists(data.php)) {
    $row = include(data.php);
}else{
      $q = mysqli_query($conn, 
        "SELECT album.Album, 
        artist.ArtistsName, 
        datereviewed.DateReviewed, 
        features.Features, 
        genre.Genre, 
        rating.Rating, 
        songname.SongName,
        link.link,
        songname.ID,
        comments.Comments
        FROM songname 
        INNER JOIN album ON album.ID = songname.ID 
        INNER JOIN artist ON artist.ID = songname.ID
        INNER JOIN datereviewed ON datereviewed.ID = songname.ID
        INNER JOIN features ON features.ID = songname.ID
        INNER JOIN genre ON genre.ID = songname.ID
        INNER JOIN rating ON rating.ID = songname.ID
        INNER JOIN link ON link.ID = songname.ID
        INNER JOIN comments ON comments.ID = songname.ID
        ORDER BY DateReviewed DESC LIMIT 1");
        $row = mysqli_fetch_array($q);    
        file_put_contents(data.php, '<?php return '.var_export($row,true).';?>');
    }

答案 1 :(得分:0)

  

问题是if语句再次运行(刷新/重新加载页面时),即使从上次运行.php文件时设置了$q

每个PHP脚本处理单个HTTP请求。脚本完成后,其所有上下文(HTTP请求,HTTP响应,变量等)都将被销毁。新请求由脚本的新实例处理,该实例对其先前的调用一无所知。

如果您需要将PHP脚本执行的信息传递给下一个,您可以使用sessions。然而,解释会议如何运作是一个过于广泛的主题,无法回答。