简体   繁体   English

如何使用mysql行结果作为get_file_content变量包含文件名?

[英]How to use mysql row result as a get_file_content variable incclude filename?

Anyone to help me find what's wrong with this script? 有人可以帮助我找到此脚本的问题吗? When this php file is ajax called via a defined ID, it change content of a whole with the result from the DB, but my problem is to get the result from $row['urlOfInclude'] and use it as an include filename in this context : 当通过定义的ID调用此php文件时,它会更改数据库内容与整体内容,但是我的问题是从$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>";

Here is the full code of the php file (rajo.php) which echo result from mysql (while I'm using another ajax code in the html page to show the echo result): 这是php文件(rajo.php)的完整代码,它从mysql回显结果(而我在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);
    ?>

Or this rajo.php: 或这个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;
    }

    ?>

NB: The last code was provided to me by Aretecode Here is the error I got when I run the the last code: Warning: file_get_contents() expects parameter 1 to be string, resource given in /home/user/public_html/folder/filename.php on line 63. Here is the php code on that line "$contents = file_get_contents($handler);" 注意:最后一个代码是由Aretecode提供给我的。这是我在运行最后一个代码时遇到的错误:警告:file_get_contents()期望参数1为字符串,资源位于/ home / user / public_html / folder / filename中第63行上的.php。这是该行上的php代码“ $ contents = file_get_contents($ handler);”。 Here is the DB table: 这是数据库表:


-- -

-- Schema of the table rajo -表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 ;

-- -- Content of the tbale rajo - -在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 */;

Here is the HTML page and the ajax code: 这是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>

Any help would be greatly appreciated... 任何帮助将不胜感激...

You have this in your code: 您的代码中包含以下内容:

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

Shouldn't it be like this: 不应该是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM