0

I am new to MySQL and I am facing difficulties to get mysqli query to work. I can connect to the database and query on development machine of which everything seeks to work but the query returns no data on capnel. I have read related post on this issue and none of them seems to solve the issue. I really want to know if this is a hosting flaw, PHP version error or my code. Below is my database connection and the query.

<?php
define("DB_SERVER", "localhost");
define("DB_USER", "myuser");
define("DB_PASSWORD", "mypassword");
define("DB_DATABASE", "mydatabase");

$conn = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
?>


$email = mysqli_real_escape_string($conn, $_POST['email']);           
$password = mysqli_real_escape_string($conn, $_POST['password']);

$sql = "SELECT * FROM 'sbausers' WHERE email = '$email' AND Password = '$password'";
$stmt = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($stmt);
if ($row > 0){
    echo 'it works';
} else {
    echo 'it doesn't work';
}
Nik
  • 2,885
  • 2
  • 25
  • 25
Big Pee
  • 41
  • 7
  • 1
    Your script is wide open to [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's – RiggsFolly Apr 01 '19 at 11:26
  • 2
    You closed your Php tag before execute Query code – Vicky Gill Apr 01 '19 at 11:27
  • 1
    **Error checking** but if you cannot be bothered, Add `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any mysqli_ errors to generate an Exception that you can see on the browser as well as normal PHP errors. – RiggsFolly Apr 01 '19 at 11:28
  • Do you store passwords unencrypted in the database? – Nik Apr 01 '19 at 11:28
  • **PLAIN TEXT PASSWORDS** PHP provides [`password_hash()`](http://php.net/manual/en/function.password-hash.php) and [`password_verify()`](http://php.net/manual/en/function.password-verify.php) please use them. And here are some [good ideas about passwords](https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet) – RiggsFolly Apr 01 '19 at 11:28
  • The connection string is in a different file – Big Pee Apr 01 '19 at 11:29
  • Do you `inlcude` or `require` the other file in this file? – RiggsFolly Apr 01 '19 at 11:31
  • Anyway, as per the first DUP, your query is incorrect – RiggsFolly Apr 01 '19 at 11:31
  • @Riggs Please I have already stated that I have read related post but none of the comments seems to actually solve the issue. The question is not also about single quotes or php errors. It runs on development machine alright with codes error set to on. – Big Pee Apr 01 '19 at 11:36
  • _It runs on development machine alright_ I dont see how that is possibe, so I will just have to leave this question to someone else to answer – RiggsFolly Apr 01 '19 at 11:41
  • If you moved this code from a test to live server. It is most likely that the Userid and Password used on the LIVE database is different to that used on the test machine. ALSO the database server may not be on `localhost` – RiggsFolly Apr 01 '19 at 11:45
  • @RiggsFolly Yeah I have contacted that hosting providers and they are looking into it, I believe that database created is not on localhost – Big Pee Apr 01 '19 at 11:48
  • I find the solutions after digging harder into the issue. I realised that, on linux environment case sensitive really matters to both MySQL database and tables as well as directory structure. I rewrote all the queries making sure all uppercase table names were changed to lowercase. I also found at that, there was a directive in the config file that could be changed to allow uppercase table names to be recognised but that was a little bit cumbersome. Any developer hosting on new linux environment is likely to face this. I wish someone with higher reputation post this as answer. Thanks – Big Pee Apr 10 '19 at 06:26

0 Answers0