I need to convert this code PDO
to Mysqli
but without an array because I send the data to it as follows
$_SESSION['CodUsua']
function usuario_por_codigo($CodUsua)
{
$con = conexion("root", "");
$consulta = $con->prepare("select * from usuarios where CodUsua = :CodUsua");
$consulta->execute(array(':CodUsua' => $CodUsua));
$resultado = $consulta->fetchAll();
return $resultado;
}
How does PDO work?
They are a way of querying the database before preparing the SQL sentence, so that it is saved and executed before in the SQL engine and then we assign the parameters or data that we want to work with the query.
Once the code is executed, it will join the parameters to the SQL code and execute the query.
Preventing them from manipulating your SQL string and data.
Step zero: The operation.
Prepared PDO queries work like this:
1) I create my SQL statement.
2) I prepare it.
3) I assign the parameters that it will have (If not, we continue directly to the execution).
4) I execute the sentence.
5) I return them to use them.
First step: The connection.
This connection code is adapted to the phpdelusion.net guide. Which ensures prepared statements with anti SQL injection protection.
Second step : The simple queries:
These are when we want to get simple data (without WHERE conditions) from the database, for that we do NOT need to prepare anything.
Therefore we will not use prepare, but the command
query()
They would be as follows:
Third step: Using WHERE conditions or better said, using parameters.
Now let's see what happens if we need to put parameters to the SQL statements, to obtain something like this:
For that we must assign parameters, using
execute()
This also requires that the sentence be prepared beforehand.
Example:
Where:
We assign $calories to the calories parameter, which corresponds to the calories field of the fruits table.
Fourth Step: Marking positions.
When we are going to assign parameters to the statements, these will occupy a place called Placeholder , there are two ways to use them.
1) Using ":FieldName"
We have the following SQL statement:
Its parameterized version would be:
Where
This form allows us to assign the parameters in any possible order, because in the same way as they are ordered they already have their corresponding place.
Example 2:
If in this statement we do this:
There will be no problems, this way also allows to maintain a better maintainability of the system, by being able to have names for each parameter.
2) Using ?
Another way is to use the closed question mark (?), the advantage is that you don't write as much hehehe, the disadvantage is that the code must assign the parameters in the same order as they are placed in the SQL statement.
Example:
We have the following SQL statement:
Its parameterized version would be:
Where ? will receive the assignment to the parameter.
Example 2:
If in this statement we do this:
It causes an error, because we are assigning the price to the ID and the ID to the price.
therefore, for this form, the order must be followed.
Being correct:
We use 1,2,3,4,5 for the placeholder number, because the function
bindValue
expects two values.Step Five: Additional Notes
As you can see in the SQL statement, when we match idProducto = :idProducto , a colon is displayed, this means that :idProducto is a little box where the value of the assigned variable will be inserted later.
This is because prepared statements first send MySQL the statement:
So that when you give it execute, both are combined something like this:
But everything happens within MySQL itself, not in PHP.
NOTE: Placing the colon in the assignment is optional, it also works like this:
Sixth step: Ways to assign parameters
To assign parameters to PDO, we have different equally valid ways, I will show you two for now:
1) Using array:
2) Using bindValue:
Eighth step: Is there more to go?, yes. Get the results.
Once we have prepared, assigned and executed, we need to recover the data, for that we use
fetch
We have different possibilities to recover the data, but I will leave you the one that I use:
FOR MORE KNOWLEDGE GO TO:
http://php.net/manual/en/pdostatement.fetch.php
Simple queries :
Prepared queries: fetchAll: Return the next row as an array indexed by column name