0

我有一个名为 $db 的对象,它在函数内部时会输出其他内容:

<?php
$dsn = "pgsql:"
    . "host=ec2-54-217-239-27.eu-west-1.compute.amazonaws.com;"
    . "dbname=dat08vv8oqt1j1;"
    . "user=secret :);"
    . "port=5432;"
    . "sslmode=require;"
    . "password=secret :)";

$db = new PDO($dsn);
$sql = "create table parent_children (
    parent_path varchar,
    child_path varchar,
    primary key(parent_path,child_path),
    foreign key (parent_path) references nodes (path),
    foreign key (child_path) references nodes (path)
)";
/*$sq = $db ->query($sql);*/

print_r($db);
myqueries($sql, $db);

function myqueries($myobject, $sql){
    print_r($myobject);
    die();
};

第一次输出

PDO Object ( )

下一次:

create table parent_children ( parent_path varchar, child_path varchar, primary key(parent_path,child_path), foreign key (parent_path) references nodes (path), foreign key (child_path) references nodes (path) )

问题是这在函数内部不起作用:

$sq = $myobject ->query($sql);

但它在它之外起作用。

4

1 回答 1

0

查看函数 args。尝试:

myqueries($db, $sql);

来自raina77ow 的评论:

function myqueries(PDO $myobject, $sql) {}

如果 $myobject 不是 PDO 类型,将抛出错误。

于 2013-11-07T16:42:06.457 回答