我可以显示不同的数据,具体取决于返回的行号,MYSQL?

时间:2011-09-13 21:08:49

标签: php mysql dynamic return rows

$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显示不同的东西,具体取决于#return。 例如。 我想要的,

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

在第二个返回的行中显示。对于我可怕的表达,我很抱歉,我刚刚接触到了php,我不太了解这个术语。

3 个答案:

答案 0 :(得分:1)

没关系,问题解决了。我刚刚做了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命令。

  • 2nd,3rd&amp;第4次返回(显然用#返回数字2替换数字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>';  
    }
    

答案 1 :(得分:0)

您可以创建一个存储数字的数组(类似$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条或更少的记录。

希望它有意义。

答案 2 :(得分:0)

您不需要对数据库进行四次单独的查询......我的意思是这样的:

///// 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>';  
}