So I was trying to make a simple login with PHP. I can retrieve rows just fine but in this specific case there seems to be a problem in getting the data I needed:
When I execute a query with the condition in the WHERE clause via a variable, I never get the data from the column. I don't get an empty set either.
I tested the connection was just fine because when I override the variable inside the method the result returns just fine.
For instance, when I do this:
$sql = "SELECT name FROM customers WHERE 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 anything at all.
But if I do this:
$email = '[email protected]';
$sql = "SELECT name FROM customers WHERE 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 the data just fine. I wonder what is the cause of this. I already tried checking the $email data type via the gettype() method and it says it's a string. I also tried trimming it before passing it on to the query but to no avail as well.
What could possibly be the cause of this? Here is my entire sample code for the 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 FROM customers WHERE 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 is the MySQL.php for the abstraction of the connection and 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 the empty password yet):
mysql> select * from customers;
+----+------------------------+-------------+---------------------------+---------+
| id | name | contact | email | password |
+----+------------------------+-------------+---------------------------+----------+
| 2 | Percival M. Micael Jr. | 09000000000 | [email protected] | |
| 3 | Richard Traballo | 09000000000 | [email protected] | |
| 4 | Richard Gwapo | 09000000000 | [email protected] | |
| 5 | Darrel Ayien | 09000000000 | [email protected] | |
| 6 | Dummy | 09000000000 | [email protected] | |
| 7 | Dummy2 | 09000000000 | [email protected] | |
| 8 | Dummy3 | 09000000000 | [email protected] | |
| 9 | Dummy4 | 09000000000 | [email protected] | |
+----+------------------------+-------------+---------------------------+----------+
8 rows in set (0.00 sec)