-2

好的,所以我有一个包含一些查询的 php 包含,并且希望能够在需要时通过运行一个函数来调用它们。运行该函数时,我没有得到返回的查询。不确定我是否需要使用 return(); 在包含与否。

<?php
// include file with queries
function displayQueryOne(){
$q = "SELECT * FROM culturegrams.new_to WHERE edition = '$url' ORDER BY placement ASC";     
$results= mysql_query($q) or die('could not run Query '.mysql_error());
return($results);

}
?>


<?php
//PAGE # 2 page to display query results
require('queryfile.php');

//running function from the include file
$results = displayQueryOne()

//running indpendent php while loop that will generate hmtl



    while ($r = mysql_fetch_assoc($results)){

?>

    <div class="top_menu"><p class="menu_text"><?php echo $r['title'];?></p></div>
    <div class="mid_menu">      
        <p class="explore_text">
            <?php echo $r['text'];?>
        </p>
    </div>
4

2 回答 2

1

是的,你需要让它像这样返回

function displayQueryOne(){
  $q = "SELECT * FROM culturegrams.new_to WHERE edition = '$url' ORDER BY placement ASC";     
  $results= mysql_query($q) or die('could not run Query '.mysql_error());
  return $results;
}

然后你需要这样称呼它

$results = displayQueryOne();
于 2012-07-25T22:28:16.603 回答
1

如果你的函数返回一个数组会更好,因为那是你真正要做的。

function displayQueryOne(){

$q = "SELECT * FROM culturegrams.new_to WHERE edition = '$url' ORDER BY placement ASC";     
$query = mysql_query($q) or die(mysql_error());
     if($query)
     {
        while($row = mysql_fetch_array($query))
        {
             $array[] = $row;
        }
        return $array;
     }else{ return 'Nothing';}
}
于 2012-07-25T22:43:34.610 回答