从数据库中提取ID后,包括一个PHP文件名

时间:2019-04-26 16:04:50

标签: php include

因此,当我添加产品时,将分配一个ID,并使用该产品ID创建一个文件,例如1000.php。我不知道如何在要包含的product.php文件中调用它:

我知道我在下面粘贴的代码中所做的事情是错误的,但是我不知道如何通过首先从sql中提取id并将其包含在同一文件中来使其调用1000.php文件。有什么建议吗?


<?php 
function between($start, $end, $string) {
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}
$pid = $_GET['id'];
$pid = trim($pid);
include('php/config.php');
$sql = "SELECT * FROM products WHERE pid='".$pid."'";
    $result = $config->query($sql);
    $num_row = mysqli_num_rows($result);
       if($num_row == 1 ) {
         $row = mysqli_fetch_array($result);
         extract($row);
        $display = 1; } else { $display = 0; }
include('".$pid.".php');

1 个答案:

答案 0 :(得分:-1)

如果我很了解您在寻求帮助,请在此行之前: <?php function between($start, $end, $string) { $string = ' ' . $string; $ini = strpos($string, $start); if ($ini == 0) return ''; $ini += strlen($start); $len = strpos($string, $end, $ini) - $ini; return substr($string, $ini, $len); } $pid = $_GET['id']; $pid = trim($pid); include('php/config.php'); $sql = "SELECT * FROM products WHERE pid='".$pid."'"; $result = $config->query($sql); $num_row = mysqli_num_rows($result); if($num_row == 1 ) { $row = mysqli_fetch_array($result); extract($row); $display = 1; } else { $display = 0; } try { $handle = fopen($dir.$filename, 'x');//look at https://www.php.net/manual/en/function.fopen.php on why to use 'x' fwrite($fp, $allTheDataYouWantToWrite); fclose($handle); }catch(\Exception $e){ //do something if exception happens }

您应该创建文件并在其中写入所需的所有数据;这样的事情可能会起作用:

include

此后,您便在该目录中创建了文件并准备使用它 考虑到Snippet A <?php $database = new mysqli( 'localhost' , 'user' , 'test' , 'database' ); $result = $database->query("CALL any_call();"); // returns true $result->close(); $database->close(); ?> 是用来读取php代码的,也许不是您想要的...