0

I have an old code which uses mysql as its extension to communicate with MySQL. The problem is, I couldn't find the equivalent of the mysql_result to convert this code to mysqli.

As you've seen, the SQL code will execute and check the result whether it is 0 or 1 and above. (if 0, then false. if more than 1, then true)

if (empty($_POST)===false) {
$username = $_POST['username'];
$password = $_POST['password'];

$me = new me($username, $password);

if (empty($username)===true||empty($password)===true) {
    die("error1");
} else if ($me->checkexist===false) {
    die("error2");
} else {echo "true";}
}

in class file:

class me {
public $uname;
public $pswd;

public function __construct($uname, $pswd) {
    $this->username = $uname;
    $this->password = $pswd;
}

public function checkexist() {
    $sql = "SELECT COUNT(`uid`) FROM `users` WHERE `username` = '$this->username'";
    $result = $conn->mysqli_query($sql);

    return ($result->fetch_array()[0] == 1) ? true : false;
}
}
Khairoul Ikhwan
  • 157
  • 1
  • 13
  • im trying to know how to implement those code into this but couldn't understand. – Khairoul Ikhwan Dec 14 '15 at 07:06
  • Well, if you have php >= 5.4, then you could do something like `return ($query->fetch_array()[0] == 1) ? true : false;`, assuming you are doing something like `$query = $mysqli->query(...);` – Sean Dec 14 '15 at 07:08
  • mysqli is giving me true value although the user doesn't exist. – Khairoul Ikhwan Dec 14 '15 at 07:17
  • Can you show your `mysqli` code? It is difficult to know what is wrong but just guessing based off your comments. – Sean Dec 14 '15 at 07:26
  • the first one is on my main page. the second one is on my class file.. – Khairoul Ikhwan Dec 14 '15 at 07:29
  • Don't put your code in the comments. Update your question and place your code there. – Sean Dec 14 '15 at 07:30
  • sorry, i'll update it now – Khairoul Ikhwan Dec 14 '15 at 07:32
  • What is `$conn->mysqli_query($sql);`? I don't see a `$conn`, and typically it is either `$conn->query($sql);` or `mysqli_query($conn,$sql);` – Sean Dec 14 '15 at 07:37
  • at the other file, it is connecting to the database – Khairoul Ikhwan Dec 14 '15 at 07:37
  • `$conn = new mysqli('host', 'username', 'pass', 'database'); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ` – Khairoul Ikhwan Dec 14 '15 at 07:38
  • Unless you include `$conn` in the `class me`, then you have a variable scope issue - http://php.net/manual/en/language.variables.scope.php – Sean Dec 14 '15 at 07:39
  • `public function checkExist($user){$stmt = $db->prepare("select `users` from `users` where `username` = ?");$stmt->bind_param('s', $user);$stmt->execute();$result = $stmt->get_result();if($result->num_rows > 0){return true} return false;}` – Magna Dec 14 '15 at 07:49
  • i've fixed my problem by adding `$conn = new mysqli('mysql.hostinger.my', 'u983859021_kroul', 'Khairoul28', 'u983859021_pro2');` to my class.. guess i was having a variable scope issue like @Sean said – Khairoul Ikhwan Dec 14 '15 at 07:59

0 Answers0