¿Cómo cambiar esta consulta en una declaración preparada?

Right, so i'm still getting my head around prepared statements and every time i think, yeah i've got it, a new query comes along and i'm thinking Hmmmm i would i set that up?

So here we go, i have a query that pulls records from a database based on a date and orders them by said date. The records it finds are based on a year and month value and the query looks like this:

$getresults = mysql_query(" SELECT * FROM `results` WHERE `date` LIKE '2012-$monthid%' ORDER BY date ");

I already have basic prepared statement for getting a users record from my database:

$query = "SELECT *
            FROM results
            WHERE date = ?
            LIMIT 1";

    if($stmt = $this->conn->prepare($query)) 
    {
        $stmt->bind_param('s', $date);
        $stmt->execute();

        if($stmt->fetch()) 
        {
            $stmt->close();
            return true;
        }
        else
            return false;
    }

How would i change this to make it more like the first query?

Gracias por la ayuda.

preguntado el 02 de febrero de 12 a las 11:02

1 Respuestas

One idea would be to filter by year and month in separate parts of the WHERE cláusula:

$query = "SELECT * FROM results WHERE YEAR(date) = 2012 AND MONTH(date) = ? ORDER BY date";

if ($stmt = $this->conn->prepare($query)) {
    $stmt->bind_param('i', $monthid);
    ...
}

Respondido 02 Feb 12, 15:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.