0

有人帮我找出这个脚本有什么问题吗?当这个 php 文件是通过定义的 ID 调用 ajax 时,它会使用来自 DB 的结果更改整体的内容,但我的问题是从中获取结果$row['urlOfInclude']并将其用作此上下文中的包含文件名:

function get_file_content($filename) {
        if (is_file($filename)) {
            ob_start();
            include $filename;
            return ob_get_clean();
        } else {
            ob_clean();
            trigger_error("The file {$filename} is not available");
        }
    }

    $rajo get_file_content("$row['url']");

    echo "<tr>";
      echo "<td>" $rajo "</td>";
      echo "</tr>";

这是从 mysql 回显结果的 php 文件 (rajo.php) 的完整代码(而我在 html 页面中使用另一个 ajax 代码来显示回显结果):

 <?php
    $q = intval($_GET['q']);

    $con = mysqli_connect('localhost','dbuser','dbpass','dbname');
    if (!$con)
      {
      die('Could not connect: ' . mysqli_error($con));
      }

    mysqli_select_db($con,"dbname");
    $sql="SELECT * FROM tablename WHERE id = '".$q."'";

    $result = mysqli_query($con,$sql);


    echo "<table border='0'>
    <tr>
    <th class='02'>ID</th>
    <th class='01'> Nom of the Station</th>
    <th class='03'> FM MHz </th>
    <th class='03'> Telephone Number </th>
    <th class='03'>Skype ID </th>
    <th class='03'> WebSite </th>
    </tr>";

    while($row = mysqli_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['id'] . "</td>";
      echo "<td>" . $row['nameOfTheStation'] . "</td>";
      echo "<td>" . $row['fmmhz'] . "</td>";
      echo "<td>" . $row['telephoneNumber'] . "</td>";
      echo "<td>" . $row['skypeID'] . "</td>";
      echo "<td>" . $row['urlOfInclude'] . "</td>";
      echo "</tr>";
      }

    function get_file_content($filename) {
        if (is_file($filename)) {
            ob_start();
            include $filename;
            return ob_get_clean();
        } else {
            ob_clean();
            trigger_error("The file {$filename} is not available");
        }
    }

    $rajo get_file_content("$row['url']");

    echo "<tr>";
      echo "<td>" $rajo "</td>";
      echo "</tr>";


    echo "</table>";


    mysqli_close($con);
    ?>

或者这个 rajo.php:

<?php

    // you didn't publish your database schema so this has not been tested
    // if you are using this on a public application http://blog.ircmaxell.com/2012/07/secure-programmers-pledge.html
    // look into PDO http://php.net/manual/en/book.pdo.php

    // please, sanitize this, or attempt to... look @ https://twitter.com/soaj1664ashar
    $id = intval($_GET['q']);

    $connection = createConnection(); // create
    $results = getDatabaseStuff($id); // get
    displayDatabaseSuff($results);    // display
    mysqli_close($connection);        // close

    function createConnection()
    {
        $connection = mysqli_connect('localhost','dbuser','dbpass','dbname'); // create

        if (!$connection)
        {
            die('Could not connect: ' . mysqli_error($connection));
        }

        return $connection;
    }

    function getDatabaseStuff($id, $connection)
    {
        // if you continue to use mysql, look at http://php.net/manual/en/mysqli.select-db.php
        mysqli_select_db($connection, 'dbname');

        // the $id had single quotes around it and you are getting the inval for the $id above, test your queries by running them in something like PHPMyAdmin 
        $sqlQuery = "SELECT * FROM `tablename` WHERE `id` = ".$id."";

        // result
        return mysqli_query($connection, $sqlQuery);
    }

    function displayDatabaseSuff($result)
    {
        echo 
        "
          <table border='0'>
          <tr>
          <th class='02'>ID</th>
          <th class='01'> om of the Station</th>
          <th class='03'>FM MHz </th>
          <th class='03'>Telephone Number</th>
          <th class='03'>Skype ID </th>
          <th class='03'>WebSite</th>
          </tr>
        ";

        // go through each result, could be added to another array in another function which could then be looped over to close the connection before displaying anything. Or (preferably) this could all be in a class.
        while($row = mysqli_fetch_array($result))
        {
            echo "<tr>";
            echo "<td>" . $row['id']                . "</td>";
            echo "<td>" . $row['nameOfTheStation']  . "</td>";
            echo "<td>" . $row['fmmhz']             . "</td>";
            echo "<td>" . $row['telephoneNumber']   . "</td>";
            echo "<td>" . $row['skypeID']           . "</td>";
            echo "<td>" . $row['urlOfInclude']      . "</td>";
            echo "</tr>";

            // needed an = sign, assigning the result of this function
            // was ['url'], it says in the description you needed results of $row['urlOfInclude']
            // put this inside the while loop if you want it for each one, now it is inside this loop
            // missed a semi colon http://google.com/?q=php+syntax+error
            // or $contents = getIncludeContents($row['urlOfInclude'])

            // use this to debug to test if your query result actually has some data in it
            var_dump($row['urlOfInclude']);

            $contents = getContents($row['urlOfInclude']);
            echo "<tr><td>$contents</td>/tr></table>";
        }

    }

    // This is most likely what you were looking for 
    // http://php.net/manual/en/function.file-get-contents.php
    // http://php.net/manual/en/function.fopen.php
    function getContents($urlOfInclude)
    {
        try 
        {
            $mode = "r+";

            // handle the file
            $handler = fopen($urlOfInclude, $mode);

            // if it did not/! open the file, throw a new exception
            if (!$handler) 
            {
                throw new Exception("Could not open the file!");
            }

            // contents
            // if you need it to be an include, put that here
            // otherwise, if it is not getting the url correctly, try doing an absolute url
            // this is an unessecary variable
            $contents = file_get_contents($handler);
            return $contents;
        }
        catch (Exception $exception) 
        {
            // securely handle this
            var_dump($exception);
        }
    }

    // http://php.net/manual/en/function.include.php
    function getIncludeContents($filename) 
    {
        if (is_file($filename)) 
        {
            ob_start();
            include $filename;
            return ob_get_clean();
        }
        return false;
    }

    ?>

注意:最后一个代码是由Aretecode提供给我的 这是我在运行最后一个代码时遇到的错误:警告:file_get_contents() 期望参数 1 是字符串,/home/user/public_html/folder/filename 中给出的资源.php 在第 63 行。这是该行的 php 代码“$contents = file_get_contents($handler);” 这是数据库表:


--

-- 表的模式rajo

CREATE TABLE IF NOT EXISTS `rajo` (
  `id` int(11) NOT NULL auto_increment,
  `nameOfTheStation` varchar(250) NOT NULL,
  `fmmhz` int(11) NOT NULL,
  `telephoneNumber` varchar(20) NOT NULL,
  `skypeID` varchar(200) NOT NULL,
  `urlOfInclude` varchar(500) NOT NULL,
  `date` varchar(50) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

-- -- tbale的内容rajo

INSERT INTO `rajo` (`id`, `nameOfTheStation`, `fmmhz`, `telephoneNumber`, `skypeID`, `urlOfInclude`, `date`) VALUES
(1, 'Radio 01 FM', 99, '0', '0', 'radio/rajo1.html', '07/10/2013 11:14:33'),
(2, 'Radio 02 FM-03', 102, '0', '0', 'radio/rajo2.html', '07/10/2013 11:14:33'),
(3, 'Radio 03 FM', 102, '\0', '', 'radio/rajo3.html', '07/10/2013 11:31:35'),
(4, 'Radio 04 FM-04', 103, '', '', 'radio/rajo4.html', '07/10/2013 11:33:47'),
(5, 'Radio 05 FM-05', 103, '', '', 'radio/rajo5.html', '07/10/2013 11:35:57');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

这是 HTML 页面和 ajax 代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <title>Rajo</title>
 <link href="css/screen.css" rel="stylesheet" type="text/css" />
 <script type="text/javascript" src="http://www.museter.com/mrp.js"></script>


<script>
function woneRajo(str)
{
if (str=="")
  {
  document.getElementById("rajo").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("rajo").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","rajo.php?q="+str,true);
xmlhttp.send();
}
</script>
 </head>

<body>
<!--start container -->
<div id="container">

<div class="content" align="center">
     <div height="160" width="430"><img class="float-center" src="images/logo.png" align="center"  /></a>
     <br />
</div>    



     <main>    


     <div class="box" id="box">
      <a href="#" class="nav" onclick="woneRajo(this.rel)" title="01" rel="1"><img class="float-left" src="radio/rajo01.jpg" alt="Cliquez ici pour ecouter cette station FM" align="center" height="83" width="160" /></a>
        </div>

      <div class="box" id="box">
      <a  href="#" class="nav" onclick="woneRajo(this.rel)" title="02" rel="2"><img class="float-left" src="radio/radio02.gif" alt="Cliquez ici pour ecouter cette station FM" align="center" height="83" width="160" /></a>   
      </div>

      <div class="box" id="box">
      <a href="#" class="nav" onclick="woneRajo(this.rel)" title="03" rel="3"><img class="float-left" src="radio/rajo03.jpg" alt="Cliquez ici pour ecouter cette station FM" height="83" width="160" /></a>  
      </div>


    </main>


</div>
<!--end container -->

<!--this is the bottom-docked div which contain the player-->
<div class='player'>
<!--this is the default radio to play every time the page loads. The content will be changed by the ajax -->
<div id="rajo" class="info">
<table border="0">
<tbody><tr>
<th class='tuuti'>ID</th>
<th class='mag'>Name of Station</th>
<th class='yam'>FM MHz</th>
<th class='yam'>Telephone</th>
<th class='yam'>Skype ID</th>
<th class='yam'>URL</th>
</tr><tr><td>2</td><td>Radio 01 FM-02</td><td>102</td><td>2147483647</td><td>0</td><td>radio/rajo01.html</td></tr>
<tr>
<td>

<?php
function get_file_content($filename) {
    if (is_file($filename)) {
        ob_start();
        include $filename;
        return ob_get_clean();
    } else {
        ob_clean();
        trigger_error("The file {$filename} was not found");
    }
}

echo get_file_content("radio/rajo01.html");
?>

</td>
</tr>
</tbody></table>
</div>


</div>  



  </body>

</html>

任何帮助将不胜感激...

4

1 回答 1

0

你的代码中有这个:

$rajo get_file_content("$row['url']");

不应该是这样的:

$rajo = get_file_content("$row['url']");
于 2013-10-16T12:43:36.533 回答