Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 这样做怎么可能?_Php - Fatal编程技术网

Php 这样做怎么可能?

Php 这样做怎么可能?,php,Php,我正试图将这些文件放在一个单独的文件中,该文件将包含在每一页中 $sql = 'select id, name, age, address, pincode from json where name = :name'; $arr = array(":name" => $name); // There are some 30 diff sql's and arrays $name = 'peter'; $conn = connect(); function myType(){ globa

我正试图将这些文件放在一个单独的文件中,该文件将包含在每一页中

$sql = 'select id, name, age, address, pincode from json where name = :name';
$arr = array(":name" => $name);
// There are some 30 diff sql's and arrays
$name = 'peter';
$conn = connect();

function myType(){
global $conn;
global $sql; 
global $arr; 
$stmt = $conn->prepare($sql);
$stmt->execute($arr);

while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
      foreach ($row as $value) {
      echo $value.' <br>';
      }
   }
}

myType();
另一页

$sql = 'select id, name, age, address, pincode from json where name = :name';
$arr = array(":name" => $name);
// There are some 30 diff sql's and arrays
$name = 'peter';
$conn = connect();

function myType(){
global $conn;
global $sql; 
global $arr; 
$stmt = $conn->prepare($sql);
$stmt->execute($arr);

while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
      foreach ($row as $value) {
      echo $value.' <br>';
      }
   }
}

myType();
$name='peter';
$conn=connect();
函数myType(){
全球$conn;
全局$sql;
全球$arr;
$stmt=$conn->prepare($sql);
$stmt->execute($arr);
而($row=$stmt->fetch(PDO::fetch\U ASSOC)){
foreach(行作为$value){
回显$value。“
”; } } } myType();
我试图将SQL和数组保存在一个单独的文件中,并在需要时使用它们。保持物品清洁,易于维护。但是变量在后面声明,这给了我:
注意:未定义的变量:第24行的C:\web\apache\htdocs\dev\json.php中的name


你能找到一种不让事情变得丑陋的方法吗?

你应该使用两个文件

  • sql.php
  • fetch.php
  • 然后在fetch.php中使用
    require\u once'sql.php'

    下面是fetch.php的代码:

    $name = 'peter';
    $conn = connect();
    require_once 'sql.php';
    function myType(){
    global $conn;
    global $sql; 
    global $arr; 
    $stmt = $conn->prepare($sql);
    $stmt->execute($arr);
    
    while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
          foreach ($row as $value) {
          echo $value.' <br>';
          }
       }
    }
    
    myType();
    

    这应该很有帮助,您可以随时使用sql.php。

    将查询和绑定参数存储在单独的include中有点奇怪。在include之后,您将如何更改绑定参数

    我的建议是创建一个模型来处理数据库操作。这样做的好处是,您可以封装数据库工作并将其与应用程序逻辑分开,还可以轻松地在整个过程中重用它

    基本示例:

    class CustomersModel
    {
        protected $db;
    
        public function __construct($db)
        {
            $this->db = $db;
        }
    
        public function getByName($name)
        {
            $result = array();
            $sql = 'select id, name, age, address, pincode from json where name = :name';
    
            if($stmt = $conn->prepare($sql))
            {
                $stmt->execute(array(":name" => $name));
                $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
            }
            return $result;
        }
    }
    
    用法:

    require_once('/path/to/CustomerModel.php');
    $conn = connect();
    
    $model = new CustomerModel($conn);
    $customers = $model->getByName('peter');
    
    foreach($customers as $c)
    {
        echo htmlspecialchars($c['name']) . '<br />;
    }
    
    require_once('/path/to/CustomerModel.php');
    $conn=connect();
    $model=新客户模型($conn);
    $customers=$model->getByName('peter');
    foreach(客户为$c)
    {
    回音htmlspecialchars($c['name'])。
    ; }
    将其声明为一个函数,该函数将返回
    $sql
    $arr
    (作为一个数组);或者作为一个对象,有两个方法。该页中有几个sql语句。为什么需要使用全局变量?尝试将变量传递给函数的其他方法,例如参数。它是稍后声明的变量
    $name
    ,我遇到了问题。