2014-09-22 46 views
0

我直接在我的标记呼应的出下面的标记为每个结果传递PHP参数传递给准备好的语句函数调用不工作

'<div class="checkboxwrapper"><input type="checkbox" name="checkbox[]" value="' . $userid . '">' . $name . '</div>' 

具有所有这些额外的代码在我的标记,似乎有一个SELECT准备好的声明有点笨重。是否有可能以某种方式将这个准备好的语句保存在一个单独的文件中,将其包含在我的标记的顶部,然后根据我想要的结果调用函数传递一个单独的参数?

getresult.php

<?php 
function getResults($output) { 
    global $db; 
    $stmt = $db->prepare("SELECT UserID, Name, Country FROM Dummy"); 
    $stmt->execute(); 
    $stmt->store_result(); 
    $rows = $stmt->num_rows(); 
    $stmt->bind_result($userid, $name, $country); 
    if($rows) { 
     while($stmt->fetch()) { 
     echo $output; 
     } 
    } else { 
     echo 'No Results found'; 
    } 
    $stmt->close(); 
} 
?> 

indexp.php

<?php 
    getResults('<div class="checkboxwrapper"><input type="checkbox" name="checkbox[]" value="' . $userid . '">' . $name . '</div>'); 
?> 

我似乎无法得到上面的代码工作,我怀疑它是做结果绑定? 理想情况下,id喜欢能够从不同的地方调用函数,并能够通过传递参数指定我想要返回的结果。这可能吗??

+0

回声 '没有找到结果'; 此部分是否受到影响? – 2014-09-22 07:53:23

回答

0

$ userid,$ name和$ country没有在函数范围中定义。

你也许可以做这样的事情:

<?php 
function getResults($output) { 
    global $db; 
    $userid = ''; 
    $name = ''; 
    $country = ''; 

    $stmt = $db->prepare("SELECT UserID, Name, Country FROM Dummy"); 
    $stmt->execute(); 
    $stmt->store_result(); 
    $rows = $stmt->num_rows(); 
    $stmt->bind_result($userid, $name, $country); 

    if($rows) { 
     while($stmt->fetch()) { 
     echo sprintf($output, $userid, $name, $country); 
     } 
    } else { 
     echo 'No Results found'; 
    } 
    $stmt->close(); 
} 
?> 

,改变函数调用来:

<?php 
getResults('<div class="checkboxwrapper"><input type="checkbox" name="checkbox[]" value="%s">%s</div>'); 
?> 
0

当您在index.php中调用getResults()时,未定义$ name和$ userid。

您必须使用模板或多个字符串。

将带变量的字符串作为参数传递给函数将无法工作。