将查询放入php包括

时间:2012-07-25 22:26:29

标签: php mysql

好的,所以我有一个php include包含一些查询,希望能够在需要时通过运行一个函数来调用它们。运行该函数时,我没有收到返回的查询。不确定我是否需要使用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>

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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';}
}