I have the following problem when doing a mysqli_query. When I do it with a variable in the SQL statement, it returns 0 records, but if I change the variable for a record in the table, it works for me. I attach the code. (It does not give any type of error)
var variableJS = params ['code'];
$codigo ="<script> document.write(variableJS) </script>";
$sql = "SELECT * from material where codigo = '" . $codigo ."'";
$result = mysqli_query($link, $sql) or die(mysqli_error());
while ($row = mysqli_fetch_array($result))
{
echo $row[0].$row[1].$row[2].$row[3].$row[4];
$option = $row[0];
$codigo = $option;
$option = substr($option,0,1);
$nombre = $row[1];
$pcte = $row[2];
$pvp = $row[3];
$stock = $row[4];
if ($option == "P")
{
$option = 1;
}else
{
$option = 2;
}
}
?>
Well, I am already clear that the problem is the value of $ code, which I do not understand because when doing the var_dump it tells me string (45) "P-001". The code is correct what is not correct is string (45) when it should be string (5). Someone knows to that can it be? The $ code is received from another page.php with window.location.search.substr (1);
At last I managed to find the key. The end was picking wrong the variable of the url. This is the correct form: $ code = $ _GET ['code']; I was doing it with a function that I had copied from the internet that did not give me the desired result. Thank you very much everyone.