PDO prepare语句触发错误

时间:2016-04-14 10:14:05

标签: php mysql pdo

我正在从mysql迁移到PDO。我正在创建一个函数,但是当调用get error:

Catchable fatal error: Object of class stdClass could not be converted to string in file * on line **

当我有“e.match_id ='$ row1-> match_id'”内容时,不确定如何处理此类迁移。代码与旧的mysql:

$query11 = "SELECT COUNT(e.event_id) as numgoals
FROM event e 
WHERE (e.match_id = '$row1->match_id' AND e.team_id = '$row1->team_a_id' AND e.eventtype IN ('soc_G','soc_PG') ) OR
      (e.match_id = '$row1->match_id' AND e.team_id = '$row1->team_b_id' AND e.eventtype = 'soc_OG' )";
$result11 = mysql_query($querye11);
$row11 = mysql_fetch_object($result11);

PDO的功能:

public function getResult11($row)
    {
        $query = "SELECT COUNT(e.event_id) as numgoals
                    FROM event e 
                    WHERE (e.match_id = '?->match_id' AND e.team_id = '?->team_a_id' AND e.eventtype IN ('soc_G','soc_PG') ) OR
                          (e.match_id = '?->match_id' AND e.team_id = '?->team_b_id' AND e.eventtype = 'soc_OG' )";
        $statement = $this->db->prepare($query);
        $statement->execute(array($row));
        return $statement->fetchObject();
    }

如何转换“e.match_id ='$ row1-> match_id'”以使用函数参数更改“$ row1”?我使用了这个“e.match_id ='? - > match_id'”,但不确定它是否正确。

1 个答案:

答案 0 :(得分:-1)

你在字符串错误中使用变量:

$math只会转换->match_id并保持{}不变。

在变量周围使用SELECT {$match->match_id}

$query = "[...] WHERE (e.match_id = :mId AND e.team_id = :t1Id AND e.eventtype IN ('soc_G','soc_PG')) OR
                      (e.match_id = :mId AND e.team_id = :t2Id AND e.eventtype = 'soc_OG')";
$statement = $this->db->prepare($query);
$statement->execute([
    ':mId' => $row1->team_a_id,
    ':t1Id' => $row1->team_a_id,
    ':t2Id' => $row1->team_b_id,
]);

准备好的陈述的正确用户

$statement = $this->db->prepare("SELECT * FROM table WHERE id = :id");

foreach ($ids as $id) {
    $statement->execute([
        ':id' => $id,
    ]);
}

如果在循环中使用

echo"<form method='POST' action='mng_menu1.php'>
<label><input type='text' id='monday' name='monday' ></label>
<label><input type='text'  name='tuesday' ></label>
<label> <input type='text'  name='wednesday' ></label>
<label> <input type='text'  name='thursday' ></label>
<label> <input type='text'  name='friday' </label>
<label> <input type='text'  name='saturday' </label>
<label> <input type='text'  name='sunday' </label>";
;
     if(isset($_POST['saveRota'])){

$con = mysqli_connect("localhost","unn_w12014288","byron122"); // connect to the database

mysqli_select_db($con,"unn_w12014288");

$insertReq =  " UPDATE ip_rota SET mon_morn='$_POST[status]' WHERE employeeName='$_POST[employeeName]'";

mysqli_query($con, $insertReq);


}


$test= $_POST['monday'];

        if(isset($_POST['start'])) {
            $con = mysqli_connect("localhost", "unn_w12014288", "byron122"); // connect to the database
            mysqli_select_db($con, "unn_w12014288");

            $sql = "SELECT e.employeeName, a.mon_morn
FROM ip_employee_availability a
INNER JOIN ip_employee e
ON a.employeeID = e.employeeID
WHERE a.mon_morn = 'yes'
AND NOT EXISTS (SELECT 1
            FROM ip_employee_request r
            WHERE a.employeeID = r.employeeID
            AND r.requestDate = '04/18/2016'
            AND r.requestStatus = 'approved')";

            $myData = mysqli_query($con, $sql);

            while ($data = mysqli_fetch_array($myData)) {

                echo "<label>" . "<input type=text id='idtxt'  required name='employeeName' value=" . $data['employeeName'] . " </label>";
                echo "<select name='status'>
                        <option name='status' value='Rest'>Rest</option>
                        <option name='status' value='Assign'>Assign</option>
                      </select>";


            }echo"<input type='submit' name='saveRota' value='Save Rota'>";
        }


echo"<input type='submit' name='start' value='Start week'>";


 echo "</form>";
 ?>