I am trying to make a query to fetch a single piece of data, but according to the query it does not give me any results, it tells me that it is null, but I think I have something wrong with get_result, I appreciate any comment, greetings
Notice: Undefined index: Measure in C:\xampp\htdocs\measures\insertar.php on line 36
this is my code:
$idVariedad = "ALIBABA";
$sqlMedida = $conexion->prepare("Select Medida from siat.medida where Variedad = :Variedad;");
$sqlMedida->bindParam(':Variedad',$idVariedad,PDO::PARAM_STR, 15);
$sqlMedida->execute();
$valor = $sqlMedida->fetch(PDO::FETCH_NUM);
echo $valor['Medida'];
The problem I see in your code is that you use a fetch_style here:
But then the way to read the result is not according to that fetch style .
If you check the documentation , the fetch style you use, which is
PDO::FETCH_NUM
:It means that if you do this:
The reading form should be like this:
You were using the proper reading form of
PDO::FETCH_ASSOC
, which, according to the documentation:So this would work fine:
For this case, you can also use
fetchColumn()
which:For example:
The parameter you are putting as an integer should remain as a string
and when obtaining the result you must use the variable with which everything is being done
Friend I would do it like this, and you access your data in the form: $valor['NombreDeColumnaEnDB'], your query has a slightly strange structure, if nothing arrives in the array, check if your query does something from the console MySQL. Additionally, I leave you the documentation of the BindParam function: https://www.php.net/manual/es/pdostatement.bindparam.php