Mysqli geeft geen error bij num_rows > 0
Ik zit met het volgende in mijn register script als er iemand wil registreren maar die naam bestaat al dan geeft ie niet de output weer dat de naam al bestaat. Echter als ik num_rows < 1 dan wel.
scripts
[register.php]
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
if(isset($_POST['register']))
{
if($Func->is_empty($_POST) == FALSE) {
echo $Func->Error('You need to fill in all of the fields!');
} else {
$User->setuserName($_POST['regist_username']);
$User->setuserPassword($_POST['regist_password']);
$User->setuserPassword2($_POST['repeat_regist_password']);
$User->setuserEmail($_POST['email']);
$User->registUser();
echo var_dump($User);
}
}
}
?>
<center><h1>Regristreer NU!</h1></center>
<form action="" method="post">
<table width="100%">
<tr>
<td>Username:</td> <td><div class="input_image"><input type="text" name="regist_username" /></div></td>
</tr>
<tr>
<td>Password:</td><td><div class="input_image"><input type="password" name="regist_password" /></div></td>
</tr>
<tr>
<td>Password *:</td><td><div class="input_image"><input type="password" name="repeat_regist_password" /></div></td>
</tr>
<tr>
<td>Email:</td><td><div class="input_image"><input type="text" name="email" /></div></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="register" value="Register" /></td>
</tr>
</table>
</form>
if($_SERVER['REQUEST_METHOD'] == "POST")
{
if(isset($_POST['register']))
{
if($Func->is_empty($_POST) == FALSE) {
echo $Func->Error('You need to fill in all of the fields!');
} else {
$User->setuserName($_POST['regist_username']);
$User->setuserPassword($_POST['regist_password']);
$User->setuserPassword2($_POST['repeat_regist_password']);
$User->setuserEmail($_POST['email']);
$User->registUser();
echo var_dump($User);
}
}
}
?>
<center><h1>Regristreer NU!</h1></center>
<form action="" method="post">
<table width="100%">
<tr>
<td>Username:</td> <td><div class="input_image"><input type="text" name="regist_username" /></div></td>
</tr>
<tr>
<td>Password:</td><td><div class="input_image"><input type="password" name="regist_password" /></div></td>
</tr>
<tr>
<td>Password *:</td><td><div class="input_image"><input type="password" name="repeat_regist_password" /></div></td>
</tr>
<tr>
<td>Email:</td><td><div class="input_image"><input type="text" name="email" /></div></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="register" value="Register" /></td>
</tr>
</table>
</form>
[User Class]
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
Class User {
protected $_userName;
protected $_userPassword;
protected $_userPassword2;
protected $_userEmail;
private $db; //DatabaseHandler
private $Func; //FunctionHandler
public function __construct($db,$Func) {
$this->db = $db;
$this->Func = $Func;
}
public function setuserName($data) {
$this->_userName = $data;
}
public function getuserName() {
return $this->_userName;
}
public function setuserPassword($data) {
$this->_userPassword = $data;
}
public function getuserPassword() {
return $this->_userPassword;
}
public function setuserPassword2($data) {
$this->_userPassword2 = $data;
}
public function getuserPassword2() {
return $this->_userPassword2;
}
public function setuserEmail($data) {
$this->_userEmail = $data;
}
public function getuserEmail() {
return $this->_userEmail;
}
//Regristration
public function registUser() {
$query = ('SELECT account FROM accounts WHERE account = "'.$this->_userName.'"');
if(!$result = $this->db->query($query))
{
trigger_error('Fout in query: '.$this->db->error);
}
else
{
if($this->db->num_rows < 1) {
//echo 'Gebruikersnaam al bekend';
return $this->Func->Error('Deze gebruikers naam is al in gebruik. Kies een andere<br />');
}
}
}
}
?>
Class User {
protected $_userName;
protected $_userPassword;
protected $_userPassword2;
protected $_userEmail;
private $db; //DatabaseHandler
private $Func; //FunctionHandler
public function __construct($db,$Func) {
$this->db = $db;
$this->Func = $Func;
}
public function setuserName($data) {
$this->_userName = $data;
}
public function getuserName() {
return $this->_userName;
}
public function setuserPassword($data) {
$this->_userPassword = $data;
}
public function getuserPassword() {
return $this->_userPassword;
}
public function setuserPassword2($data) {
$this->_userPassword2 = $data;
}
public function getuserPassword2() {
return $this->_userPassword2;
}
public function setuserEmail($data) {
$this->_userEmail = $data;
}
public function getuserEmail() {
return $this->_userEmail;
}
//Regristration
public function registUser() {
$query = ('SELECT account FROM accounts WHERE account = "'.$this->_userName.'"');
if(!$result = $this->db->query($query))
{
trigger_error('Fout in query: '.$this->db->error);
}
else
{
if($this->db->num_rows < 1) {
//echo 'Gebruikersnaam al bekend';
return $this->Func->Error('Deze gebruikers naam is al in gebruik. Kies een andere<br />');
}
}
}
}
?>
[Class Function]
Code (php)
var_dump($User) geeft bij username die nog niet in gebruik is dit.
Code (php)
1
2
2
Deze gebruikers naam is al in gebruik. Kies een andere
object(User)#3 (6) { ["_userName":protected]=> string(4) "test" ["_userPassword":protected]=> string(4) "test" ["_userPassword2":protected]=> string(4) "test" ["_userEmail":protected]=> string(4) "test" ["db":"User":private]=> object(mysqli)#1 (17) { ["affected_rows"]=> int(0) ["client_info"]=> string(6) "5.1.41" ["client_version"]=> int(50141) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["field_count"]=> int(1) ["host_info"]=> string(20) "localhost via TCP/IP" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(6) "5.1.41" ["server_version"]=> int(50141) ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(86) ["warning_count"]=> int(0) } ["Func":"User":private]=> object(Func)#2 (0) { } }
object(User)#3 (6) { ["_userName":protected]=> string(4) "test" ["_userPassword":protected]=> string(4) "test" ["_userPassword2":protected]=> string(4) "test" ["_userEmail":protected]=> string(4) "test" ["db":"User":private]=> object(mysqli)#1 (17) { ["affected_rows"]=> int(0) ["client_info"]=> string(6) "5.1.41" ["client_version"]=> int(50141) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["field_count"]=> int(1) ["host_info"]=> string(20) "localhost via TCP/IP" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(6) "5.1.41" ["server_version"]=> int(50141) ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(86) ["warning_count"]=> int(0) } ["Func":"User":private]=> object(Func)#2 (0) { } }
Bij username die al en gebruik is verandert alleen de affected_rows=> int(0) naar int(1) maar dan nog geeft die als ik num_rows > 0 doe geen melding van dat de gebruikersnaam al bekend is.
Iemand een idee? Of zie ik het niet(Kan ook nog).
Met vriendelijke groet,
Thomas de Vries.
EDIT: Opgelost ik deed $this->db->num_rows ip $result->num_rows
Gewijzigd op 17/05/2016 14:42:00 door Thomas de vries
Er zijn nog geen reacties op dit bericht.