简体   繁体   English

准备好的陈述……我在做什么错?

[英]Prepared Statements… What am I Doing Wrong?

I'm trying to update some code and update my coding skills at the same time. 我正在尝试同时更新一些代码并更新我的编码技能。 After looking at prepared statements on a few sites I tried turing this into a prepared statement: 在几个站点上查看准备好的语句后,我尝试将其整理为准备好的语句:

$db=new Database();
$query='SELECT * FROM `student` WHERE `student_id`="'.$student_id.'" LIMIT 1';
$result=$db->query($query)or die('...');
$row=$result->fetch_assoc();

As an "easy" first step, I tried using a prepared statement without any variables because I seem to be hopelessly stuck on a elementary level: 作为“轻松”的第一步,我尝试使用不带任何变量的预处理语句,因为我似乎无可救药地停留在基本水平上:

    $db = new mysqli("localhost", "uname", "pword", "astro8_gakkou");
    if(!($stmt=$db->prepare("SELECT * FROM `student` WHERE `student_id`=5 LIMIT 1"))){
        echo "Prepare failed: (".$db->errno.") ".$db->error;
    }
    if(!$stmt->execute()){
        echo "Execute failed: (".$db->errno.") ".$db->error;
    }
    $result=$stmt->get_result();
    $row=$result->fetch_assoc();

This kills php in it's tracks. 这会杀死php。 It's the last line that's the culprit, but I can't understand why. 这是罪魁祸首的最后一行,但我不明白为什么。 I know I'm just being dumb, but can someone point to what fundamental concept I'm missing? 我知道我只是傻瓜,但是有人可以指出我所缺少的基本概念吗? I've read through php.net along with a host of other sites and I just can't seem to see what step I'm missing. 我已经阅读了php.net和许多其他网站,但似乎看不到我缺少了什么步骤。

UPDATE: still not working, but I've updated the code. 更新:仍然无法正常工作,但是我已经更新了代码。 The log shows this error now: 日志现在显示此错误:

[14-Mar-2014 22:34:10 America/New_York] PHP Fatal error: Call to undefined method mysqli_stmt::get_result() in /webdocs/zinc/class.Student.inc on line 38 [2014年3月14日22:34:10 America / New_York] PHP致命错误:在第38行的/webdocs/zinc/class.Student.inc中调用未定义的方法mysqli_stmt :: get_result()

It looks like you're missing a $result = $stmt->get_result(); 好像您缺少$result = $stmt->get_result();

Then pull the results $row = $result->fetch_assoc(); 然后拉结果$row = $result->fetch_assoc();

Checkout the first user-contributed-note here: http://www.php.net/manual/en/mysqli.prepare.php 在此处签出第一个用户提供的注释: http : //www.php.net/manual/en/mysqli.prepare.php

The answer, despite my proclamations otherwise, was to use PDO. 尽管有我的其他声明,但答案是使用PDO。 In it's simplest form this: 用最简单的形式是:

$db=new Database();
$query='SELECT * FROM `student` WHERE `student_id`="'.$student_id.'" LIMIT 1';
$result=$db->query($query)or die('...');
$row=$result->fetch_assoc();

Became this (using PDO and prepared statements): 变成了这个(使用PDO和准备好的语句):

$db=new PDO("mysql:dbname=school;host=localhost","root","root");
$stmt=$db->prepare("SELECT * FROM `student` WHERE `student_id`= ? LIMIT 1");
$stmt->bindParam(1,$student_id);
$stmt->execute();
$row=$stmt->fetch();

Huge thanks to the folks that pushed me (yelling and screaming) to use PDO. 非常感谢促使我(大喊大叫)使用PDO的人们。 This works wonderfully and doesn't throw a single error. 这样效果很好,不会抛出任何错误。 It'll mean a bit more work updating the code, but I'm thinking it'll be worth it in the long run. 这将意味着需要更多的工作来更新代码,但是从长远来看,我认为这是值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM