MYSQLI Object oriented, what's wrong with my script?
up vote
-3
down vote
favorite
I'm trying to do an execution of a query and see if it goes well, but right now it doesn't enter the IF or ELSE.
I had it on mysqli procedural and all worked flawlessy now I'm trying to change it to object oriented and it won't enter inside if/else.
if(isset($_POST['submit']))
{
$email = $_POST["email"];
$password = md5($_POST["password"]);
$query = "SELECT * FROM Users WHERE Email=? AND Password=?";
$stmt = $conn->prepare($query);
$stmt->bind_param('ss', $email,$password);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 1)
{
?>
<script type="text/javascript">
alert("INSIDE");
</script>
<?php
$row = $result->fetch_assoc();
if(isset($_POST['remember']))
{
$_SESSION["remember"] = "1";
}
$_SESSION["username"] = $row['Username'];
$_SESSION['check'] = "1";
$_SESSION['ID'] = $id;
$_SESSION['permission'] = $row['Admin'];
header("Location: dashboard.php");
exit;
}
else
{
?>
<script type="text/javascript">
alert("Credentials Are Wrong!");
</script>
<?php
exit;
}
$stmt->close();
}
Thank you all.
php oop object mysqli
|
show 3 more comments
up vote
-3
down vote
favorite
I'm trying to do an execution of a query and see if it goes well, but right now it doesn't enter the IF or ELSE.
I had it on mysqli procedural and all worked flawlessy now I'm trying to change it to object oriented and it won't enter inside if/else.
if(isset($_POST['submit']))
{
$email = $_POST["email"];
$password = md5($_POST["password"]);
$query = "SELECT * FROM Users WHERE Email=? AND Password=?";
$stmt = $conn->prepare($query);
$stmt->bind_param('ss', $email,$password);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 1)
{
?>
<script type="text/javascript">
alert("INSIDE");
</script>
<?php
$row = $result->fetch_assoc();
if(isset($_POST['remember']))
{
$_SESSION["remember"] = "1";
}
$_SESSION["username"] = $row['Username'];
$_SESSION['check'] = "1";
$_SESSION['ID'] = $id;
$_SESSION['permission'] = $row['Admin'];
header("Location: dashboard.php");
exit;
}
else
{
?>
<script type="text/javascript">
alert("Credentials Are Wrong!");
</script>
<?php
exit;
}
$stmt->close();
}
Thank you all.
php oop object mysqli
It'd be a lot easier to tell you what's wrong with it if you'd tell us what the problem is. Please include the error this returns, and what's supposed to happen instead.
– Davіd
yesterday
It doesn't execute the query, it gives error when executing query
– Luca Verdecchia
yesterday
Have you looked into usingfetch()
instead ofquery()
?
– Nigel Ren
yesterday
1
md5()
is not strong enough encryption.
– mickmackusa
yesterday
1
DO NOT usemd5()
as encryption. Usepassword_hash()
andpassword_verify()
instead.
– rpm192
yesterday
|
show 3 more comments
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I'm trying to do an execution of a query and see if it goes well, but right now it doesn't enter the IF or ELSE.
I had it on mysqli procedural and all worked flawlessy now I'm trying to change it to object oriented and it won't enter inside if/else.
if(isset($_POST['submit']))
{
$email = $_POST["email"];
$password = md5($_POST["password"]);
$query = "SELECT * FROM Users WHERE Email=? AND Password=?";
$stmt = $conn->prepare($query);
$stmt->bind_param('ss', $email,$password);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 1)
{
?>
<script type="text/javascript">
alert("INSIDE");
</script>
<?php
$row = $result->fetch_assoc();
if(isset($_POST['remember']))
{
$_SESSION["remember"] = "1";
}
$_SESSION["username"] = $row['Username'];
$_SESSION['check'] = "1";
$_SESSION['ID'] = $id;
$_SESSION['permission'] = $row['Admin'];
header("Location: dashboard.php");
exit;
}
else
{
?>
<script type="text/javascript">
alert("Credentials Are Wrong!");
</script>
<?php
exit;
}
$stmt->close();
}
Thank you all.
php oop object mysqli
I'm trying to do an execution of a query and see if it goes well, but right now it doesn't enter the IF or ELSE.
I had it on mysqli procedural and all worked flawlessy now I'm trying to change it to object oriented and it won't enter inside if/else.
if(isset($_POST['submit']))
{
$email = $_POST["email"];
$password = md5($_POST["password"]);
$query = "SELECT * FROM Users WHERE Email=? AND Password=?";
$stmt = $conn->prepare($query);
$stmt->bind_param('ss', $email,$password);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 1)
{
?>
<script type="text/javascript">
alert("INSIDE");
</script>
<?php
$row = $result->fetch_assoc();
if(isset($_POST['remember']))
{
$_SESSION["remember"] = "1";
}
$_SESSION["username"] = $row['Username'];
$_SESSION['check'] = "1";
$_SESSION['ID'] = $id;
$_SESSION['permission'] = $row['Admin'];
header("Location: dashboard.php");
exit;
}
else
{
?>
<script type="text/javascript">
alert("Credentials Are Wrong!");
</script>
<?php
exit;
}
$stmt->close();
}
Thank you all.
php oop object mysqli
php oop object mysqli
edited yesterday
asked yesterday
Luca Verdecchia
125
125
It'd be a lot easier to tell you what's wrong with it if you'd tell us what the problem is. Please include the error this returns, and what's supposed to happen instead.
– Davіd
yesterday
It doesn't execute the query, it gives error when executing query
– Luca Verdecchia
yesterday
Have you looked into usingfetch()
instead ofquery()
?
– Nigel Ren
yesterday
1
md5()
is not strong enough encryption.
– mickmackusa
yesterday
1
DO NOT usemd5()
as encryption. Usepassword_hash()
andpassword_verify()
instead.
– rpm192
yesterday
|
show 3 more comments
It'd be a lot easier to tell you what's wrong with it if you'd tell us what the problem is. Please include the error this returns, and what's supposed to happen instead.
– Davіd
yesterday
It doesn't execute the query, it gives error when executing query
– Luca Verdecchia
yesterday
Have you looked into usingfetch()
instead ofquery()
?
– Nigel Ren
yesterday
1
md5()
is not strong enough encryption.
– mickmackusa
yesterday
1
DO NOT usemd5()
as encryption. Usepassword_hash()
andpassword_verify()
instead.
– rpm192
yesterday
It'd be a lot easier to tell you what's wrong with it if you'd tell us what the problem is. Please include the error this returns, and what's supposed to happen instead.
– Davіd
yesterday
It'd be a lot easier to tell you what's wrong with it if you'd tell us what the problem is. Please include the error this returns, and what's supposed to happen instead.
– Davіd
yesterday
It doesn't execute the query, it gives error when executing query
– Luca Verdecchia
yesterday
It doesn't execute the query, it gives error when executing query
– Luca Verdecchia
yesterday
Have you looked into using
fetch()
instead of query()
?– Nigel Ren
yesterday
Have you looked into using
fetch()
instead of query()
?– Nigel Ren
yesterday
1
1
md5()
is not strong enough encryption.– mickmackusa
yesterday
md5()
is not strong enough encryption.– mickmackusa
yesterday
1
1
DO NOT use
md5()
as encryption. Use password_hash()
and password_verify()
instead.– rpm192
yesterday
DO NOT use
md5()
as encryption. Use password_hash()
and password_verify()
instead.– rpm192
yesterday
|
show 3 more comments
2 Answers
2
active
oldest
votes
up vote
0
down vote
You should be using
$stmt->bind_result($col1, $col2 ...);
and
$result = $stmt->fetch();
in order to access the data from the query, rather than
$conn->query($stmt);
(an example is provided at https://secure.php.net/manual/en/mysqli-stmt.fetch.php). Note that for this to work you will need to specify the column names you want to fetch from the database, rather than using * in your SQL query, and for each column data is fetched from in the query, you should have a variable for in the fetch() parameters, so for example, something as follows should work (note these may not match the names of your database columns):
$email = $_POST["email"];
$password = md5($_POST["password"]);
$stmt = $conn->prepare("SELECT ID, Name FROM Users WHERE Email=? AND Password=?");
$stmt->bind_param('ss', $email, $password);
$stmt->execute();
$stmt->bind_result($id, $name);
$stmt->fetch();
$stmt->close();
echo $id . ': ' . $name;
New contributor
add a comment |
up vote
0
down vote
Updated Answer
You are very close. Use $result = $stmt->get_result();
instead of $result = $stmt->query;
to check to see if the query returned a result or not.
$email = $_POST["email"];
$password = md5($_POST["password"]);
$query = "SELECT * FROM Users WHERE Email = ? AND Password = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('ss', $email, $password);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows !== 0){
if(isset($_POST['remember'])){
$_SESSION["remember"] = "1";
}
$_SESSION['check'] = "1";
$_SESSION['ID'] = $row['ID'];
header("Location: dashboard.php");
exit();
}else{
echo
'<script type="text/javascript">
alert("Credentials Are Wrong!");
</script>';
exit();
}
$stmt->close();
As several have already stated in their comments do not use MD5 for password hashes. PHP has it's own built in functions for handling passwords. Please research Password_has() and Password_verify(). Spend the time to research and implement these now instead of later. It will save you time.
Thanks for the responce, and I will surely see php functions for password hash! But I've done as you told and now it seems that it doesn't go inside IF or ELSE. Check my original post for the new version of the code
– Luca Verdecchia
yesterday
Runecho $result->num_rows;
before the if statement and tell me what it says.
– Joseph_J
yesterday
There is some error as browser says 500 error, so there is an error on the code but dreamviewer doesn't tell me anything about errors. If i remove $result = $stmt->get_result(); the browser doesn't display any 500 error
– Luca Verdecchia
yesterday
I don't see anything wrong with that line of code.
– Joseph_J
yesterday
I've updated the answer, If i remove $result = $stmt->get_result(); the browser doesn't display any 500 error
– Luca Verdecchia
yesterday
|
show 4 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You should be using
$stmt->bind_result($col1, $col2 ...);
and
$result = $stmt->fetch();
in order to access the data from the query, rather than
$conn->query($stmt);
(an example is provided at https://secure.php.net/manual/en/mysqli-stmt.fetch.php). Note that for this to work you will need to specify the column names you want to fetch from the database, rather than using * in your SQL query, and for each column data is fetched from in the query, you should have a variable for in the fetch() parameters, so for example, something as follows should work (note these may not match the names of your database columns):
$email = $_POST["email"];
$password = md5($_POST["password"]);
$stmt = $conn->prepare("SELECT ID, Name FROM Users WHERE Email=? AND Password=?");
$stmt->bind_param('ss', $email, $password);
$stmt->execute();
$stmt->bind_result($id, $name);
$stmt->fetch();
$stmt->close();
echo $id . ': ' . $name;
New contributor
add a comment |
up vote
0
down vote
You should be using
$stmt->bind_result($col1, $col2 ...);
and
$result = $stmt->fetch();
in order to access the data from the query, rather than
$conn->query($stmt);
(an example is provided at https://secure.php.net/manual/en/mysqli-stmt.fetch.php). Note that for this to work you will need to specify the column names you want to fetch from the database, rather than using * in your SQL query, and for each column data is fetched from in the query, you should have a variable for in the fetch() parameters, so for example, something as follows should work (note these may not match the names of your database columns):
$email = $_POST["email"];
$password = md5($_POST["password"]);
$stmt = $conn->prepare("SELECT ID, Name FROM Users WHERE Email=? AND Password=?");
$stmt->bind_param('ss', $email, $password);
$stmt->execute();
$stmt->bind_result($id, $name);
$stmt->fetch();
$stmt->close();
echo $id . ': ' . $name;
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
You should be using
$stmt->bind_result($col1, $col2 ...);
and
$result = $stmt->fetch();
in order to access the data from the query, rather than
$conn->query($stmt);
(an example is provided at https://secure.php.net/manual/en/mysqli-stmt.fetch.php). Note that for this to work you will need to specify the column names you want to fetch from the database, rather than using * in your SQL query, and for each column data is fetched from in the query, you should have a variable for in the fetch() parameters, so for example, something as follows should work (note these may not match the names of your database columns):
$email = $_POST["email"];
$password = md5($_POST["password"]);
$stmt = $conn->prepare("SELECT ID, Name FROM Users WHERE Email=? AND Password=?");
$stmt->bind_param('ss', $email, $password);
$stmt->execute();
$stmt->bind_result($id, $name);
$stmt->fetch();
$stmt->close();
echo $id . ': ' . $name;
New contributor
You should be using
$stmt->bind_result($col1, $col2 ...);
and
$result = $stmt->fetch();
in order to access the data from the query, rather than
$conn->query($stmt);
(an example is provided at https://secure.php.net/manual/en/mysqli-stmt.fetch.php). Note that for this to work you will need to specify the column names you want to fetch from the database, rather than using * in your SQL query, and for each column data is fetched from in the query, you should have a variable for in the fetch() parameters, so for example, something as follows should work (note these may not match the names of your database columns):
$email = $_POST["email"];
$password = md5($_POST["password"]);
$stmt = $conn->prepare("SELECT ID, Name FROM Users WHERE Email=? AND Password=?");
$stmt->bind_param('ss', $email, $password);
$stmt->execute();
$stmt->bind_result($id, $name);
$stmt->fetch();
$stmt->close();
echo $id . ': ' . $name;
New contributor
New contributor
answered yesterday
M. Kilpatrick
1611
1611
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
Updated Answer
You are very close. Use $result = $stmt->get_result();
instead of $result = $stmt->query;
to check to see if the query returned a result or not.
$email = $_POST["email"];
$password = md5($_POST["password"]);
$query = "SELECT * FROM Users WHERE Email = ? AND Password = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('ss', $email, $password);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows !== 0){
if(isset($_POST['remember'])){
$_SESSION["remember"] = "1";
}
$_SESSION['check'] = "1";
$_SESSION['ID'] = $row['ID'];
header("Location: dashboard.php");
exit();
}else{
echo
'<script type="text/javascript">
alert("Credentials Are Wrong!");
</script>';
exit();
}
$stmt->close();
As several have already stated in their comments do not use MD5 for password hashes. PHP has it's own built in functions for handling passwords. Please research Password_has() and Password_verify(). Spend the time to research and implement these now instead of later. It will save you time.
Thanks for the responce, and I will surely see php functions for password hash! But I've done as you told and now it seems that it doesn't go inside IF or ELSE. Check my original post for the new version of the code
– Luca Verdecchia
yesterday
Runecho $result->num_rows;
before the if statement and tell me what it says.
– Joseph_J
yesterday
There is some error as browser says 500 error, so there is an error on the code but dreamviewer doesn't tell me anything about errors. If i remove $result = $stmt->get_result(); the browser doesn't display any 500 error
– Luca Verdecchia
yesterday
I don't see anything wrong with that line of code.
– Joseph_J
yesterday
I've updated the answer, If i remove $result = $stmt->get_result(); the browser doesn't display any 500 error
– Luca Verdecchia
yesterday
|
show 4 more comments
up vote
0
down vote
Updated Answer
You are very close. Use $result = $stmt->get_result();
instead of $result = $stmt->query;
to check to see if the query returned a result or not.
$email = $_POST["email"];
$password = md5($_POST["password"]);
$query = "SELECT * FROM Users WHERE Email = ? AND Password = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('ss', $email, $password);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows !== 0){
if(isset($_POST['remember'])){
$_SESSION["remember"] = "1";
}
$_SESSION['check'] = "1";
$_SESSION['ID'] = $row['ID'];
header("Location: dashboard.php");
exit();
}else{
echo
'<script type="text/javascript">
alert("Credentials Are Wrong!");
</script>';
exit();
}
$stmt->close();
As several have already stated in their comments do not use MD5 for password hashes. PHP has it's own built in functions for handling passwords. Please research Password_has() and Password_verify(). Spend the time to research and implement these now instead of later. It will save you time.
Thanks for the responce, and I will surely see php functions for password hash! But I've done as you told and now it seems that it doesn't go inside IF or ELSE. Check my original post for the new version of the code
– Luca Verdecchia
yesterday
Runecho $result->num_rows;
before the if statement and tell me what it says.
– Joseph_J
yesterday
There is some error as browser says 500 error, so there is an error on the code but dreamviewer doesn't tell me anything about errors. If i remove $result = $stmt->get_result(); the browser doesn't display any 500 error
– Luca Verdecchia
yesterday
I don't see anything wrong with that line of code.
– Joseph_J
yesterday
I've updated the answer, If i remove $result = $stmt->get_result(); the browser doesn't display any 500 error
– Luca Verdecchia
yesterday
|
show 4 more comments
up vote
0
down vote
up vote
0
down vote
Updated Answer
You are very close. Use $result = $stmt->get_result();
instead of $result = $stmt->query;
to check to see if the query returned a result or not.
$email = $_POST["email"];
$password = md5($_POST["password"]);
$query = "SELECT * FROM Users WHERE Email = ? AND Password = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('ss', $email, $password);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows !== 0){
if(isset($_POST['remember'])){
$_SESSION["remember"] = "1";
}
$_SESSION['check'] = "1";
$_SESSION['ID'] = $row['ID'];
header("Location: dashboard.php");
exit();
}else{
echo
'<script type="text/javascript">
alert("Credentials Are Wrong!");
</script>';
exit();
}
$stmt->close();
As several have already stated in their comments do not use MD5 for password hashes. PHP has it's own built in functions for handling passwords. Please research Password_has() and Password_verify(). Spend the time to research and implement these now instead of later. It will save you time.
Updated Answer
You are very close. Use $result = $stmt->get_result();
instead of $result = $stmt->query;
to check to see if the query returned a result or not.
$email = $_POST["email"];
$password = md5($_POST["password"]);
$query = "SELECT * FROM Users WHERE Email = ? AND Password = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('ss', $email, $password);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows !== 0){
if(isset($_POST['remember'])){
$_SESSION["remember"] = "1";
}
$_SESSION['check'] = "1";
$_SESSION['ID'] = $row['ID'];
header("Location: dashboard.php");
exit();
}else{
echo
'<script type="text/javascript">
alert("Credentials Are Wrong!");
</script>';
exit();
}
$stmt->close();
As several have already stated in their comments do not use MD5 for password hashes. PHP has it's own built in functions for handling passwords. Please research Password_has() and Password_verify(). Spend the time to research and implement these now instead of later. It will save you time.
edited yesterday
answered yesterday
Joseph_J
2,6221617
2,6221617
Thanks for the responce, and I will surely see php functions for password hash! But I've done as you told and now it seems that it doesn't go inside IF or ELSE. Check my original post for the new version of the code
– Luca Verdecchia
yesterday
Runecho $result->num_rows;
before the if statement and tell me what it says.
– Joseph_J
yesterday
There is some error as browser says 500 error, so there is an error on the code but dreamviewer doesn't tell me anything about errors. If i remove $result = $stmt->get_result(); the browser doesn't display any 500 error
– Luca Verdecchia
yesterday
I don't see anything wrong with that line of code.
– Joseph_J
yesterday
I've updated the answer, If i remove $result = $stmt->get_result(); the browser doesn't display any 500 error
– Luca Verdecchia
yesterday
|
show 4 more comments
Thanks for the responce, and I will surely see php functions for password hash! But I've done as you told and now it seems that it doesn't go inside IF or ELSE. Check my original post for the new version of the code
– Luca Verdecchia
yesterday
Runecho $result->num_rows;
before the if statement and tell me what it says.
– Joseph_J
yesterday
There is some error as browser says 500 error, so there is an error on the code but dreamviewer doesn't tell me anything about errors. If i remove $result = $stmt->get_result(); the browser doesn't display any 500 error
– Luca Verdecchia
yesterday
I don't see anything wrong with that line of code.
– Joseph_J
yesterday
I've updated the answer, If i remove $result = $stmt->get_result(); the browser doesn't display any 500 error
– Luca Verdecchia
yesterday
Thanks for the responce, and I will surely see php functions for password hash! But I've done as you told and now it seems that it doesn't go inside IF or ELSE. Check my original post for the new version of the code
– Luca Verdecchia
yesterday
Thanks for the responce, and I will surely see php functions for password hash! But I've done as you told and now it seems that it doesn't go inside IF or ELSE. Check my original post for the new version of the code
– Luca Verdecchia
yesterday
Run
echo $result->num_rows;
before the if statement and tell me what it says.– Joseph_J
yesterday
Run
echo $result->num_rows;
before the if statement and tell me what it says.– Joseph_J
yesterday
There is some error as browser says 500 error, so there is an error on the code but dreamviewer doesn't tell me anything about errors. If i remove $result = $stmt->get_result(); the browser doesn't display any 500 error
– Luca Verdecchia
yesterday
There is some error as browser says 500 error, so there is an error on the code but dreamviewer doesn't tell me anything about errors. If i remove $result = $stmt->get_result(); the browser doesn't display any 500 error
– Luca Verdecchia
yesterday
I don't see anything wrong with that line of code.
– Joseph_J
yesterday
I don't see anything wrong with that line of code.
– Joseph_J
yesterday
I've updated the answer, If i remove $result = $stmt->get_result(); the browser doesn't display any 500 error
– Luca Verdecchia
yesterday
I've updated the answer, If i remove $result = $stmt->get_result(); the browser doesn't display any 500 error
– Luca Verdecchia
yesterday
|
show 4 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237892%2fmysqli-object-oriented-whats-wrong-with-my-script%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
It'd be a lot easier to tell you what's wrong with it if you'd tell us what the problem is. Please include the error this returns, and what's supposed to happen instead.
– Davіd
yesterday
It doesn't execute the query, it gives error when executing query
– Luca Verdecchia
yesterday
Have you looked into using
fetch()
instead ofquery()
?– Nigel Ren
yesterday
1
md5()
is not strong enough encryption.– mickmackusa
yesterday
1
DO NOT use
md5()
as encryption. Usepassword_hash()
andpassword_verify()
instead.– rpm192
yesterday