我可以显示不同的数据取决于什么行数是在返回,MYSQL


Can I Display Different Data Depending On What Row Number Is In Return, MYSQL?

$sqlCommand = "SELECT * FROM videos WHERE id='$pageid' ORDER BY id DESC LIMIT 4"; 
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
while ($row = mysqli_fetch_array($query)) { 
    $id = $row["id"];
    $titleCurVid = $row["title"];
    $keywords = $row["keywords"];
    $returnVariable = 'Hi, I am the first return in the MYSQL select';
    }

我希望$returnVariable显示不同的东西取决于什么#返回。为例。我想要,

$returnVariable = 'Hi, I am the SECOND return in MYSQL';

显示在第二个返回行中。我很抱歉我的解释很糟糕,我只是php的新手,我不太了解术语。

没关系,问题解决了。我只做了4个不同的取回。

  • 第一次返回(具有唯一,特殊格式显示的返回):

    $sql_storyPublisher1 = mysql_query("SELECT * FROM story ORDER BY id DESC LIMIT 1");
    while($row = mysql_fetch_array($sql_storyPublisher1))
    {
        $publisherID1 = $row["id"];
        $publishertitle1 = $row["title"];
        $returnVariable1 = '<li id="selected" class="firstSlide YellowSelected">' . $publisherID1 . '</li>';    
    }
    

感谢大家的帮助,我想最简单的解决方案就是使用OFFSET命令。

  • 第二,第三&第4次返回(显然用# Return替换数字2):

    $sql_storyPublisher2 = mysql_query("SELECT * FROM story ORDER BY id DESC LIMIT 1 OFFSET 1");
    while($row = mysql_fetch_array($sql_storyPublisher2))
    {
        $publisherID2 = $row["id"];
        $publishertitle2 = $row["title"];
        $returnVariable2 = '<li id="" class="secondSlide">' . $publisherID2 . '</li>';  
    }
    

您可以创建一个数组来存储数字(例如$arr = array(1 => "first", 2 => "second", 3 => "third", 4 => "fourth");等)然后在循环中使用计数器变量访问该数组。我是这么想的……

    $sqlCommand = "SELECT * FROM videos WHERE id='$pageid' ORDER BY id DESC LIMIT 4";  
    $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());  
    $arr = array(1 => "first", 2 => "second", 3 => "third", 4 => "fourth", 5 => "fifth"); //and so on
    $counter = 1
    while ($row = mysqli_fetch_array($query)) {  
        $id = $row["id"]; 
        $titleCurVid = $row["title"]; 
        $keywords = $row["keywords"]; 
        $returnVariable = 'Hi, I am the '.$arr[$counter].' return in the MYSQL select'; 
$counter++;
        } 

语法可能不正确,但您可以理解。请注意,上面的示例只在有5条或更少的记录时显示它。

您不需要对数据库进行四个单独的查询…我的意思更像是这样:

///// 1st Return (the one with the unique, special format display) =
$sql_storyPublisher1 = mysql_query("SELECT * FROM story ORDER BY id DESC");
$row = mysql_fetch_array($sql_storyPublisher1);
$publisherID1 = $row["id"];
$publishertitle1 = $row["title"];
$returnVariable1 = '<li id="selected" class="firstSlide YellowSelected">' . $publisherID1 . '</li>';    
///// 2nd, 3rd & 4th Return (obviously replacing the number 2 with whatever # return it is) =
$row = mysql_fetch_array($sql_storyPublisher2);
$publisherID2 = $row["id"];
$publishertitle2 = $row["title"];
$returnVariable2 = '<li id="" class="secondSlide">' . $publisherID2 . '</li>';  

当然可能有更好的方法,但这是简单和可读的。你可能想要确保你实际上有4行。如果你不确定,你可以用if:

if($row = mysql_fetch_array($sql_storyPublisher2)) {
    $publisherID2 = $row["id"];
    $publishertitle2 = $row["title"];
    $returnVariable2 = '<li id="" class="secondSlide">' . $publisherID2 . '</li>';  
}