Note that there are some explanatory texts on larger screens.

plurals
  1. PObindParam is not completing the sql query
    primarykey
    data
    text
    <p>I'm new to PDO statements and so far I've managed to work with it, use prepared statements and many things, until today.</p> <p>I have two querys, the first retrieve some data, store the results and then the second query uses that data to retrieve the final data. I'm working on a bad designed DB, that's why I have to do weird things.</p> <p>The first query gets the year of start and the year of end of a sport league. Then, the year is passed to the second query to get data between those years (WHERE).</p> <p>The problem is that bindParam seems to not work, it doesn't bind the parameter, shows a ?, and then the SQL throws the following exception:</p> <pre><code>Connection failed: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''0701' AND ?'0630' ORDER BY e.FECHA DESC' at line 5 </code></pre> <p>The SQL:</p> <pre><code>$sqlQueryAuxiliar = "SELECT ano_inicio, ano_fin FROM TEMPORADAS ORDER BY ano_inicio DESC LIMIT 1;"; $sqlQuery = "SELECT e.id, e.JORNADA, DATE_FORMAT(e.FECHA, '%Y-%m-%d'), e.HORA, c1.nombre_temporada, c2.nombre_temporada FROM ENCUENTROS AS e JOIN CLUBS AS c1 ON (e.COD_EQUIL = c1.siglas) JOIN CLUBS AS c2 ON (e.COD_EQUIV = c2.siglas) WHERE e.FECHA BETWEEN :anoInicio'0701' AND :anoFinal'0630' ORDER BY e.FECHA DESC;"; </code></pre> <p>And this is the PHP code:</p> <pre><code>$this-&gt;_db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmtAux = $this-&gt;_db-&gt;prepare($sqlQueryAuxiliar); $stmtAux-&gt;execute(); $fetched = $stmtAux-&gt;fetchAll(); $stmtAux = null; $stmt = $this-&gt;_db-&gt;prepare($sqlQuery); $stmt-&gt;bindParam(':anoInicio', $fetched[0][0], PDO::PARAM_STR, 12); $stmt-&gt;bindParam(':anoFinal', $fetched[0][1], PDO::PARAM_STR, 12); $stmt-&gt;execute(); while ($row = $stmt-&gt;fetch()) { $partidos[] = $row; } $stmt = null; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload