如何解决我可以在 bind_result 中使用类实体吗
正如我在这里读到的许多帖子一样,我应该尽可能使用准备好的语句,所以这个想法在我脑海中浮现。
在类的构造函数中,我使用准备好的语句来获取类实体的数据。 我尝试并收到此错误消息: 致命错误:无法通过引用传递参数 1 ...
这是我使用的代码:
<?PHP
final class Getraenkeabo{
private $id = 0;
private $mitgliedsnr = 0;
private $preis = 0.0;
private $abbuchungstag = null;
private $abbuchung_ab = null;
private $abbuchung_bis = null;
public function __get($key){
if(!property_exists($this,$key)){
throw new Exception('Zugriff auf nicht existente Eigenschaft '.$key);
}
else{
return $this->$key;
}
}
public function __set($key,$value){
if(!property_exists($this,$key)){
throw new Exception('Zugriff auf nicht existente Eigenschaft '.$key);
}
elseif($key == 'preis'){
$this->preis == floatval($value);
}
elseif($key == 'abbuchungstag'){
if(!is_null($value)){
if($value == 1 || $value == 15){
$this->abbuchungstag = intval($value);
}
else{
throw new Exception('Als Abbuchungstag sind nur 1 oder 15 erlaubt. '.$value.' gegeben');
}
}
else{
$this->abbuchungstag = null;
}
}
elseif($key == 'abbuchung_ab' || $key == 'abbuchung_bis'){
if(!is_null($value)){
if(is_null($this->abbuchungstag)){
throw new Exception('Um einen Abbuchungszeitraum zu bestimmen,muss zuerst der Abbuchungstag gesetzt werden.');
}
else{
if($key == 'abbuchung_bis' && strtotime($value) < strtotime($this->abbuchung_ab)){
throw new Exception('Das Abbuchungsende '.$value.' liegt nicht nach dem Abbuchungsanfang.');
}
else{
if(strtotime($value) !== false){
$this->key = $value;
}
else{
throw new Exception('Ungültiges Datum "'.$value.'" für '.__CLASS.__.'->'.$key);
}
}
}
}
else{
$this->$key = null;
}
}
else{
$this->$key = intval($value);
}
}
/**
* Konstruktor
* @param int $mitgliedsnr Mitgliedsnummer
*/
function __construct($mitgliedsnr = 0){
if($mitgliedsnr > 0){
$this->mitgliedsnr = $mitgliedsnr;
$stmt = $GLOBALS['db']->prepare('SELECT `id`,`mitgliedsnr`,`preis`,`abbuchungstag`,`abbuchung_ab`,`abbuchung_bis` FROM `getraenkeabo` WHERE `mitgliedsnr`=?');
$stmt->bind_param('i',$mitgliedsnr);
$stmt->execute();
$stmt->bind_result('iifiss',$this->id,$this->mitgliedsnr,$this->preis,$this->abbuchungstag,$this->abbuchung_ab,$this->abbuchung_bis);
$stmt->fetch();
$stmt->close();
}
}
function save(){
$sql = sprintf(
'REPLACE INTO `getraenkeabo` (`id`,`abbuchung_bis`) VALUES (%d,%d,%s,%s)',(is_null($this->abbuchungstag))?'NULL':'"'.$this->abbuchungstag.'"',(is_null($this->abbuchung_ab))?'NULL':'"'.$this->abbuchung_ab.'"',(is_null($this->abbuchung_bis))?'NULL':'"'.$this->abbuchung_bis.'"'
);
$GLOBALS['db']->query($sql) OR die(writeErrorLogfile($GLOBALS['db']->error.$sql));
if($this->id == 0){
$this->id = $GLOBALS['db']->insert_id;
}
return true;
}
function delete(){
require_once(__DIR__.'/class.Mitgliedschaft.PHP');
$ms = new Mitgliedschaft($this->mitgliedsnr);
if($ms->art instanceof Mitgliedschaftsart){
$ms->art = new Mitgliedschaftsart($ms->art->vip_von);
$ms->save();
}
$sql = sprintf('DELETE FROM `getraenkeabo` WHERE `id` = %d',$this->id);
$GLOBALS['db']->query($sql) OR die(writeErrorLogfile($GLOBALS['db']->error.$sql));
return true;
}
}
?>
我怀疑问题是因为 bind_param 使用引用而不是变量,并且可能无法处理取消引用运算符 ->。 是这样吗?
解决方法
我头上的灰烬。结果发现问题与此post中的相同。
在 bind_result 中,您只需指定应存储数据集列的变量,而不是它们的数据类型。这仅对 bind_param 是必需的。似乎是典型的初学者错误。因此,因为我是准备语句的新手,所以不要生我的气。 感谢大家的快速帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。