2016-04-14 5 views
0

Ich bin von MySQL auf PDO migrieren. Ich schaffe eine Funktion, aber wenn get Fehler Aufruf:PDO Prepare-Anweisung löst einen Fehler

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

nicht sicher, wie diese Art der Migration zu behandeln, wenn ich „e.match_id =‚$ row1-> match_id‘“ Sachen haben. Code mit alten 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); 

Funktion mit 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(); 
    } 

Wie "e.match_id = '$ row1-> match_id'" konvertieren "$ row1" mit Funktionsparameter zu ändern? Ich habe dieses "e.match_id = '? -> match_id'" verwendet, bin mir aber nicht sicher, ob es richtig ist.

+0

uns Zeigen Sie, was in '$ row ist ' – RiggsFolly

+0

Das '? -> match_id 'ist NICHT die richtige Syntax. Wünschen Sie einen benannten Parameter, d. H. ': Match_id' oder Positionsparameter, d. H.' ' – RiggsFolly

+0

' $ row' ist eine andere Abfrage. Nicht sicher, ich werde das verwenden, das passt am besten zu meinem Fall. – Sagi

Antwort

-1

Sie verwenden Variablen in String falsch:

SELECT $match->match_id nur $math und Blätter ->match_id unberührt wandeln.

Verwenden {} um Variable:

SELECT {$match->match_id} 

Proper Benutzer vorbereitete Anweisung

$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, 
]); 

Wenn in Schleife verwendet

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

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

Ich glaube, du verpasst das '?' Ich denke, er ist nur ein wenig verwirrt – RiggsFolly

+0

Wenn ich '$ row1-> match_id',' $ row1-> team_a_id', '$ row1-> team_b_id' habe ich will sie alle zu bekomme die selbe row1. Sollte ich 3 Parameter verwenden? – Sagi

+0

@Sagi In 'execute' übergeben Sie einfach das zugeordnete Array mit der Struktur' ': param' => "Aktueller Wert" ' – Justinas