duanfen7676
2016-10-15 17:37
浏览 36
已采纳

为什么我的php脚本没有返回任何变量?

I've been trying for a while to add a 'load more' button to a php script.

I've got a rather complicated MYSQL query here that does work but I am now trying to use with prepared statements and just output two variables.

Whilst the script does return the expected number of results it's not actually echoing out any variables. I suspect the problem lies in the foreach loops... In the console response I can see this:

<li>-</li><li>-</li><li>-</li>

The script

//get current starting point of records
$position = (($page_number-1) * $item_per_page);

//fetch records 
$results = $mysqli->prepare("SELECT up.id,up.file,up.title,p.user_name,p.user_id, GROUP_CONCAT(CONCAT(cp.user_id,'~',cp.user_name) SEPARATOR '|') AS tagGroup
FROM tbl_uploads up
LEFT JOIN tbl_users p ON up.user_id = p.user_id
LEFT JOIN tbl_collab c ON up.file = c.file
LEFT JOIN tbl_users cp ON cp.user_id = c.collab_userid
GROUP BY up.file ORDER BY up.id LIMIT ?, ?");

//bind parameters for markers
$results->bind_param("dd", $position, $item_per_page); 
$results->execute(); //Execute prepared Query
$results->bind_result($title, $file); //bind variables to prepared statement

//output results from database

while($row = $results->fetch()){ //fetch values

$titles =  explode (",", $row['title']);
$files = explode (",", $row['file']);

foreach($titles as $title) {
foreach($files as $file) {
    echo '<li>'.$title.'-'.$file.'</li>';   
}
}
}

Here's an example of the out put from phpadmin. enter image description here

图片转代码服务由CSDN问答提供 功能建议

我一直试图在PHP脚本中添加“加载更多”按钮。

我在这里有一个相当复杂的MYSQL查询,但是我现在正在尝试使用预处理语句并输出两个变量。

脚本 返回预期的结果数,它实际上并没有回显任何变量。 我怀疑问题出在foreach循环中...在控制台响应中我可以看到:

 &lt; li&gt;  - &lt; / li&gt;&lt; li&gt;  -  &lt; / li&gt;&lt; li&gt;  - &lt; / li&gt; 
   
 
 

脚本

  // 得到记录的当前起点
 $ position =(($ page_number-1)* $ item_per_page); 
 
 //获取记录
 $ results = $ mysqli-&gt; prepare(“SELECT up.id,up  .file,up.title,p.user_name,p.user_id,GROUP_CONCAT(CONCAT(cp.user_id,'〜',cp.user_name)SEPARATOR'|')AS tagGroup 
FROM tbl_uploads up 
LEFT JOIN tbl_users p ON up。  user_id = p.user_id 
LEFT JOIN tbl_collab c ON up.file = c.file 
LEFT JOIN tbl_users cp ON cp.user_id = c.collab_userid 
GROUP BY up.file ORDER BY up.id LIMIT?,?“); \  n 
 //绑定标记的参数
 $ results-&gt; bind_param(“dd”,$ position,$ item_per_page);  
 $的结果 - &GT;执行();  //执行准备好的Query 
 $ results-&gt; bind_result($ title,$ file);  //将变量绑定到预准备语句
 
 //从数据库输出结果
 
而($ row = $ results-&gt; fetch()){//获取值
 
 $ titles = explode(“,  “,$ row ['title']); 
 $ files = explode(”,“,$ row ['file']); 
 
foreach($ titles as $ title){
foreach($ files as $  file){
 echo'&lt; li&gt;'。$ title .'-'。$ file。'&lt; / li&gt;';  
} 
} 
} 
   
 
 

以下是来自phpadmin的输出示例。 < / p>

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douren1891 2016-10-15 18:56
    最佳回答

    I think you are misunderstanding how bind_result() works.

    When mysqli_stmt_fetch() is called to fetch data, the MySQL client/server protocol places the data for the bound columns into the specified variables var1, ....

    This means that the columns listed in your select statement get mapped to the variables listed in your call to bind_result()

    For example, if you had SELECT first_name, last_name FROM names and you called:

    $results->bind_result($first, $last);

    When you loop over the results, for each row, the value from first_name will be available via the variable $first and the value from last_name will be available via $last

    That means you could do:

    while($results->fetch()){ //fetch values
        echo 'Hi '.$first.' '.$last;
    }
    

    Try your code like this:

            //fetch records
            // change your select to only select the columns you need and the order needs to be the same as bind_result below
            $results = $mysqli->prepare("SELECT up.file,up.title
                                         FROM tbl_uploads up
                                         LEFT JOIN tbl_users p ON up.user_id = p.user_id
                                         LEFT JOIN tbl_collab c ON up.file = c.file
                                         LEFT JOIN tbl_users cp ON cp.user_id = c.collab_userid
                                         GROUP BY up.file ORDER BY up.id LIMIT ?, ?");
    
    
    
    
            //bind parameters for markers
            $results->bind_param("dd", $position, $item_per_page);
            $results->execute(); //Execute prepared Query
            $results->bind_result($title, $file); //bind variables to prepared statement
            // this binds the selected columns to the variable names given for use below
            // $title below is equivalent to $result['title] inside a for loop 
    
    
    
            while($results->fetch()){ //fetch values 
                echo '<li>'.$title.'-'.$file.'</li>';
            }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题