【发布时间】:2017-07-03 10:31:38
【问题描述】:
这是我创建的用于调用我的数据库连接并从数据库中插入和检索数据的函数
有没有办法在不为每个函数创建变量的情况下做到这一点
//require('OrientDB/OrientDB.php');
require 'phpIncludes/PhpOrient/vendor/autoload.php';
use PhpOrient\PhpOrient;
//require 'phpIncludes/sendgrid-php/vendor/autoload.php';
//Used for insert and other non value returning operations just send the query to execute
function runInsertQuery($query) {
$client = new PhpOrient();
$client->hostname = 'localhost';
$client->port = 2424;
$client->username = 'root';
$client->password = 'hello';
$client->connect();
$client->dbOpen('tabe');
$result = $client->command($query);
// var_dump($result);
return $result;
}
function getId($table, $field, $value) {
$client = new PhpOrient();
$client->hostname = 'localhost';
$client->port = 2424;
$client->username = 'root';
$client->password = 'hello';
$client->connect();
$client->dbOpen('tabe');
$query = "select from $table where $field='$value'";
$result = $client->command($query);
$json = json_decode(json_encode($result));
//var_dump($json);
if (sizeof($json) > 0) {
return $json->rid;
} else {
return false;
}
}
function runSelectQuery($query) {
$client = new PhpOrient();
$client->hostname = 'localhost';
$client->port = 2424;
$client->username = 'root';
$client->password = 'hello';
$client->connect();
$client->dbOpen('tabe');
$result = $client->query($query);
$json = json_decode(json_encode($result));
if (sizeof($json) > 0) {
return $json[0]->oData;
} else {
return false;
}
}
function runquery($query)
{
$client = new PhpOrient();
$client->hostname = 'localhost';
$client->port = 2424;
$client->username = 'root';
$client->password = 'hello';
$client->connect();
$client->dbOpen('tabe');
$result = $client->query($query);
$json = json_decode(json_encode($result));
if (sizeof($json) > 0) {
return $json;
} else {
return false;
}
}
如果我这样做
$client = new PhpOrient();
$client->hostname = 'localhost';
$client->port = 2424;
$client->username = 'root';
$client->password = 'hello';
$client->connect();
$client->dbOpen('tabe');
function insertQuery();
{
//body
}
function selectQuery();
{
//body
}
它抛出了变量未定义的错误如何解决这个问题 我是php新手,你能帮我解决这个问题吗
【问题讨论】:
-
您应该将
PhpOrient继承到一个新类,然后使用新的类对象或将这些函数添加到PhpPrient类中。 -
@HamzaAbdaoui Agghhhh 不要使用 GLOBAL 作为传递应该在函数参数中的数据的方式。全球是不懂的人的首选
-
@RiggsFolly Thnx 给小费!