PHP/Javascript + MySQL data is not being retrieved when query has variables -
so trying make simple login php. can retrieve rows fine in specific case there seems problem in getting data needed:
when execute query condition in clause via variable, never data column. don't empty set either.
i tested connection fine because when override variable inside method result returns fine.
for instance, when this:
$sql = "select name customers email='" . $email . "'"; $result = $mysql->executesql($conn, $sql); if(!empty($result)){ while($row = mysqli_fetch_array($result)){ echo $row['name']; } }else{ echo "empty set"; }
it doesn't return @ all.
but if this:
$email = 'richardgwapo@gmail.com'; $sql = "select name customers email='" . $email . "'"; $result = $mysql->executesql($conn, $sql); if(!empty($result)){ while($row = mysqli_fetch_array($result)){ echo $row['name']; } }else{ echo "empty set"; }
it fetches data fine. wonder cause of this. tried checking $email data type via gettype() method , says it's string. tried trimming before passing on query no avail well.
what possibly cause of this? here entire sample code test login:
<html> <head> <?php function verifylogin($email, $passwd){ require('mysql.php'); $mysql = new mysql; $conn = $mysql->connecttomysql("127.0.0.1", "root", "", "fivestarhotel"); $sql = "select name customers email='" . $email . "'"; $result = $mysql->executesql($conn, $sql); if(!empty($result)){ while($row = mysqli_fetch_array($result)){ echo $row['name']; } }else{ echo "empty set"; } } ?> <script> var email = ""; var passwd = ""; function buttonpressed(){ email = document.getelementbyid("temail").value; passwd = document.getelementbyid("tpasswd").value; document.write("<?php verifylogin('" + email + "','" + passwd + "');?>"); } </script> </head> <body> <input type="text" id="temail" /> <br/> <input type="password" id="tpasswd" /> <br/> <input type="button" value="go" onclick="buttonpressed()"/> </body> </html>
and here mysql.php abstraction of connection , query execution:
<?php class mysql{ var $conn; function connecttomysql($servername, $username, $password, $dbname){ // create connection $conn = new mysqli($servername, $username, $password, $dbname); // check connection if ($conn->connect_error) { die("connection failed: " . $conn->connect_error); } return $conn; } function executesql($conn, $sql){ $result = mysqli_query($conn,$sql); return $result; } } ?>
sample database (please don't mind empty password yet):
mysql> select * customers; +----+------------------------+-------------+---------------------------+---------+ | id | name | contact | email | password | +----+------------------------+-------------+---------------------------+----------+ | 2 | percival m. micael jr. | 09000000000 | percival.micael@yahoo.com | | | 3 | richard traballo | 09000000000 | richardgwapo@yahoo.com | | | 4 | richard gwapo | 09000000000 | richardgwapo@gmail.com | | | 5 | darrel ayien | 09000000000 | darrelayien@yahoo.com | | | 6 | dummy | 09000000000 | dummy@dummy.com | | | 7 | dummy2 | 09000000000 | dummy2@dummy2.com | | | 8 | dummy3 | 09000000000 | dummy3@dummy3.com | | | 9 | dummy4 | 09000000000 | dummy4@dummy4.com | | +----+------------------------+-------------+---------------------------+----------+ 8 rows in set (0.00 sec)
you can't this.
<script> var email = ""; var passwd = ""; function buttonpressed(){ email = document.getelementbyid("temail").value; passwd = document.getelementbyid("tpasswd").value; document.write("<?php verifylogin('" + email + "','" + passwd + "');?>"); } </script>
javascript client side , php server side. javascript code write line <?php verifylogin('" + email + "','" + passwd + "');?>
in html page.
you need use @ least ajax. check this:
Comments
Post a Comment