Do a query on php but I get no results

0

Well the problem is the following I am doing a simple query with a where within php , I already echo some more with this type of syntax and I have done well, especially in logging and inserting data, the problem is this I work with three iframes of javascript and at the time of login I send a variable of class php to iframe number 0 since that never moves and always stored this variable here the code.

<script>
window.parent.frames[2].location = "../inicio.php";
window.parent.frames[0].location = "../menu2.php?nick=<?php echo $nombre;  ?>";
</script>

There I send my variable php to my iframe number 0, well then now I print it and I know that if it is in that place, then in my profile class, I need that variable since depending on the nickname of the user will show your profile information and this is the code

$nick = "<script> document.write(window.parent.frames[0].prueba) </script>";
echo $nick;

$sql = "SELECT *  FROM usuario WHERE nick = '$nick'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while ($row = mysqli_fetch_assoc($result)) {
        echo "nick: " . $row["nick"] . "<br>";
    }
}

Then as you will see I do an echo within the nick variable in the above code and print the variable depending on the user that I entered, that means that it brings me what I need, but when I put the variable $nick inside of my query of php does not bring me any record, but if I do it by means of the id, if I tare the row that I need or even if I put the nick in a direct way it brings me the row, my problem is that when I put the variable that stores the user that entered through the iframe number zero, it does not bring me an error or anything just stays at zero. some way to solve that slight error, thank you in advance for reading the Post.

    
asked by David 23.07.2016 в 21:33
source

3 answers

0

The nick variable it stores is a javascript code, not a text string that should represent the nickname. Put this after the definition of $ nick and you will notice:

var_dump($nick);

In your variable nick should only be the value of the nick worth the redundancy. If your PHP code of the query is in the file menu2.php the correct definition of nick would be:

$nick = $_GET["nick"];

Instead of:

$nick = "<script> document.write(window.parent.frames[0].prueba);</script>";
    
answered by 28.07.2016 / 15:31
source
0

Store your data in an array and then do var_dump of that to be able to visualize the structure of your data:

$datos = array();
if (mysqli_num_rows($result) > 0) {
 // output data of each row
 while ($row = mysqli_fetch_assoc($result)) {
     $datos[] = $row;
 }
}
//Imprimi los datos de esta manera:

echo "<pre>";
var_dump($datos);
echo "</pre>";

That way you can have an idea of how you are returning the sql query and you can work with ease.

Probably changing:

$sql = "SELECT *  FROM usuario WHERE nick = '$nick'";

for this:

$sql = "SELECT * FROM usuario WHERE nick = '".$nick."'";

also add the mysqli_error () method to Sql to print the error:

$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

One last recommendation, to validate sql queries you do not need to use mysqli_num_rows (), the query answers boolean I'll give you an example:

if($result){
 //la consulta respondio TRUE
} else {
 //error en la consulta
}
    
answered by 23.07.2016 в 22:52
0

David, the step of the variable, does it have to be that way? Since you are in experimental plan, why, based on jQuery, do not apply a 'listener' for communication between the 'frames'. So you can send a value of the child frame to the parent frame, or vice versa.

Once you have the necessary value, I mean the variable, you can invoke, from JQuery itself, your php query code, at the same time you pass that value.

In case you have not worked with this form of communication, and you are interested in learning about it, visit this site to begin with.

Good to start from below; not everyone has such an initiative.

    
answered by 24.07.2016 в 07:01