mysqli_query empty

0

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.

    
asked by Esmi2016 29.08.2018 в 04:58
source

2 answers

0

try to use the

var_dump($codigo);

the var_dump is a function shows structured information on one or more expressions including its type and value, it helps you to see the value of that variable, if $ code you get NULL it is empty so you can not make the query

    
answered by 29.08.2018 в 08:07
0

Make it a trim() to $codigo , since it could possibly have blank spaces, it would be something like this:

$codigo ="<script> document.write(variableJS) </script>";
$codigo = trim($codigo);
$sql = "SELECT * from material where codigo = '$codigo'";
$result = mysqli_query($link, $sql) or die(mysqli_error());

I recommend that you start implementing sentences prepared to avoid problems of injection:

I hope you get solutions, you tell me!

    
answered by 29.08.2018 в 21:39