Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我可以根据返回的行号显示不同的数据吗,MYSQL?_Php_Mysql_Dynamic_Return_Rows - Fatal编程技术网

Php 我可以根据返回的行号显示不同的数据吗,MYSQL?

Php 我可以根据返回的行号显示不同的数据吗,MYSQL?,php,mysql,dynamic,return,rows,Php,Mysql,Dynamic,Return,Rows,我希望$return变量根据返回的内容显示不同的内容。 例如 我想要一个 $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

我希望$return变量根据返回的内容显示不同的内容。 例如 我想要一个

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

在返回的第二行中显示。很抱歉,我的解释太糟糕了,我刚接触php,对术语不太熟悉。

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

$returnVariable = 'Hi, I am the SECOND return in MYSQL';
语法可能不正确,但你明白了。请注意,上面的示例仅在有5条或更少记录时显示


希望它有意义。

没关系,问题解决了。我只是做了4次不同的抓取

首先返回具有唯一、特殊格式显示的:

    $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++;
        } 
谢谢大家的帮助,我想简单的解决方法就是使用OFFSET命令

第二、第三和第四次退货显然将数字2替换为任何退货:

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

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

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

我实际上是想删除WHERE id='$pageid',并用WHERE featured='y'替换它,我明白你的意思,但我需要能够根据返回的数组和唯一格式的数据创建不同的变量名,使用不同的html标记$returnVariable1='hi i am No.1'first return------$returnVariable2='hi i am No.2'Second return------$returnVariable3='hi i am No.3'3rd return------$returnVariable4='hi i am No.4'4th return------啊,您想创建$returnVariable1,$returnVariable2。。。在飞行中?您可以尝试使用PHP变量,使用2个美元符号来动态创建变量,比如$returnVariable=$$returnVariable.$counter。请在这里@Alex阅读,如果结果限制为4,那么您甚至可以不使用while循环,只需调用mysqli_fetch_数组4次。因此,您是说,执行四个单独的mysqli_fetch_数组,并将每个数组偏移,但比最后一个多偏移一次,这样我就可以得到四个不同的变量,我可以在每个变量中创建格式完全不同的数据,并在我的身体中回荡出来?顺便说一句,我之所以需要四个不同格式的变量,是因为我从我的cms中获取特色故事,并将它们显示在Jquery控制的特色新闻滑块中。第一个包含不同的div id和类,但我仍然希望动态地回显数据。
///// 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>';  
if($row = mysql_fetch_array($sql_storyPublisher2)) {

    $publisherID2 = $row["id"];
    $publishertitle2 = $row["title"];

    $returnVariable2 = '<li id="" class="secondSlide">' . $publisherID2 . '</li>';  
}