mysqli基于行创建链接

时间:2015-06-29 16:34:56

标签: php mysql database mysqli row

我想基于行值创建链接,并且 - 更重要的是,添加变量 - 如果行为空,则此文本...如果行有某个值,则显示此....

//我需要获得这个显示:

//如果行季为空,则生成的链接必须为

// web.com/MOVIES /"。$ row [" title_id"]。"

//如果行季有一些值,那么链接必须是://web.com/SERIES/" .$row [" title_id"]。" / SEASON /".$row["season"]."/EPISODE/".$row["episode"]."

这是基本代码。

        <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "dbname";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "SELECT id, type, label, title_id, season, episode, approved FROM links order by id desc LIMIT 30 OFFSET 50";
    $result = $last_id = $conn->query($sql);


    if ($result->num_rows > 0)
     {
        echo "<table><tr><th>ID</th><th>Label</th><th>URL</th><th>Season</th><th>Episode</th><th>Approved</th></tr>";
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "<tr><td>".$row["id"]."</td><td>".$row["label"]."</td>";
// --------------------------------------------------------------
            echo "<td><a href='http://web.com/(VARIABLES HERE)'>";
            echo " ".$row["title_id"]."</a></td>";
// --------------------------------------------------------------
    //I NEED TO GET THIS DISPLAY:
    // if row season is empty, the resulting link must be
    // web.com/MOVIES/".$row["title_id"]."
    // if row season have some value, then the link must be:
  //web.com/SERIES/".$row["title_id"]."/SEASON/".$row["season"]."/EPISODE/".$row["episode"]."
    // -----------------------------------------
            echo "<td>".$row["season"]."</td><td>".$row["episode"]."</td><td>".$row["approved"]."</td></tr>";
        }
        echo "</table>";
    } else {
        echo "0 results";
    }
    $conn->close();
    ?>

2 个答案:

答案 0 :(得分:2)

正如评论中所述,您使用的是保留的MySQL关键字:type。所以你的查询不能正常工作。您无法告知,因为您尚未设置任何检查以确保其正常运行。你可以像这样在它周围放置反引号:

SELECT id, `type`

更改数据库中密钥的名称(我建议使用此方法,例如titleType)。在你解决这个问题之前,PHP中没有任何工作。

一旦解决了这个问题,就如何生成结果一样,你可以在while循环中做这样的事情(我假设你的意思是剧集价值):

if (empty($row['episode'])) {
     echo 'web.com/MOVIES/'.$row["title_id"];
}
else {
     echo 'web.com/SERIES/'.$row['title_id'].'/SEASON/'.$row['season'].'/EPISODE/'.$row["episode"];
}

答案 1 :(得分:0)

这是正确的完整代码。

<?php
$servername = "localhost";
$username = "WRITE YOUR DB username ";
$password = "WRITE YOUR DB password";
$dbname = "WRITE YOUR db name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, label, title_id, season, episode, approved FROM links order by id desc LIMIT 30 OFFSET 1";
$result = $last_id = $conn->query($sql);


if ($result->num_rows > 0)
 {
    echo "<table><tr><th>ID</th><th>Audio</th><th>URL</th><th>Temporada</th><th>Episodio</th><th>Aprobado</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["id"]."</td><td>".$row["label"]."</td>";
        echo "<td><a href=";
        if (empty($row['episode'])) {
     echo '/peliculas-online/'.$row["title_id"];
    }
    else {
     echo '/series-online/'.$row['title_id'].'/seasons/'.$row['season'].'/episodes/'.$row["episode"];
        }
        echo ">".$row["title_id"]."</a></td>";
        echo "<td>".$row["season"]."</td><td>".$row["episode"]."</td><td>".$row["approved"]."</td></tr>";

    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
?>