将关联数组传递给php函数

时间:2015-03-30 02:50:48

标签: php arrays oop

如何将关联数组传递给php函数?我尝试了下面的代码,但它无法正常工作。请帮助我或至少给出建议。 非常感谢你。

addrecord.php
require 'class.php';
$fields = array(
'strname' =>$name,
'strdescript' =>$strdescript,
'straction' =>$straction
);
$addRecord = new Record();
$addRecord->addRecord($fields);


class.php
class Record{
public $db, $sql,$stmt;
public function __construct(){
$this->db = new connection();
$this->db = $this->db->dbConnect();
}
public function addRecord($fields){
$this->sql =
"INSERT INTO user
(name,description,action)
VALUES
(?,?,?)"
$this->stmt = $this->db->prepare($this->sql);
$this->stmt->bindParam(1,{$fields['strname']},PDO::PARAM_STR);
$this->stmt->bindParam(2,{$fields['strdescript']},PDO::PARAM_STR);
$this->stmt->bindParam(3,{$fields['straction']},PDO::PARAM_STR);
$this->stmt->execute();
if($this->stmt->rowCount==1){
echo "<script>alert('blah.. blah.. blah.')</script>";
}else{ 
echo "<script>alert('blah.. blah.. blah.')</script>";
}//EndIf
}//EndFunction

2 个答案:

答案 0 :(得分:1)

您的问题的代码行有正确的用法。

语法

$var = "{array['key']}";

注意:

  • &#34;更大&#34; string使用&#34;&#34;,(因为&#34;&#34;字符串允许使用{}表示变量,并使用&#39;&#39;在内部。

  • {}告诉php替换变量的值

  • 您的数组成员是要使用的变量

你的错误是1)使用多行,你可以做多行(在许多其他语法中,研究它们):

$var = "this " .
           "is " .
             " a string in multiple lines";

2)你的sql字符串语法错误,描述很可能是字符串类型,字符串必须介于&#39;&#39;

$sql = "INSERT INTO (...) VALUES ( 'my description' )"; //you lack these 's, put them

答案 1 :(得分:0)

要传递空关联数组,请执行以下操作:

$result['data'] = array('content_id'=>'','user_id'=>'','title' =>'','content' =>'');

相关问题