Displaying false result, while the query returned true
up vote
1
down vote
favorite
I am trying to execute the below code.
Execution gets success, but it is displaying sorry, Query could not execute...
My Codes:
class.php
public function admin_update($id,$update,$value)
{
try{
$stmt = $this->conn->prepare("UPDATE admin SET ".$update."=:_value WHERE Id=:_id");
$stmt->bindparam(":_value",$value);
$stmt->bindparam(":_id",$id);
$stmt->execute();
}
catch(PDOException $ex)
{
echo $ex->getMessage();
}
}
update.php
$id = "1";
if(admin_update($id,"Access","Y"))
{
echo "Success";
}
else
{
echo "sorry, Query could not execute...";
}
MySQL table is updated, but it does not display Success, instead, it displays sorry, Query could not execute...
php mysqli pdo sql-update
add a comment |
up vote
1
down vote
favorite
I am trying to execute the below code.
Execution gets success, but it is displaying sorry, Query could not execute...
My Codes:
class.php
public function admin_update($id,$update,$value)
{
try{
$stmt = $this->conn->prepare("UPDATE admin SET ".$update."=:_value WHERE Id=:_id");
$stmt->bindparam(":_value",$value);
$stmt->bindparam(":_id",$id);
$stmt->execute();
}
catch(PDOException $ex)
{
echo $ex->getMessage();
}
}
update.php
$id = "1";
if(admin_update($id,"Access","Y"))
{
echo "Success";
}
else
{
echo "sorry, Query could not execute...";
}
MySQL table is updated, but it does not display Success, instead, it displays sorry, Query could not execute...
php mysqli pdo sql-update
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to execute the below code.
Execution gets success, but it is displaying sorry, Query could not execute...
My Codes:
class.php
public function admin_update($id,$update,$value)
{
try{
$stmt = $this->conn->prepare("UPDATE admin SET ".$update."=:_value WHERE Id=:_id");
$stmt->bindparam(":_value",$value);
$stmt->bindparam(":_id",$id);
$stmt->execute();
}
catch(PDOException $ex)
{
echo $ex->getMessage();
}
}
update.php
$id = "1";
if(admin_update($id,"Access","Y"))
{
echo "Success";
}
else
{
echo "sorry, Query could not execute...";
}
MySQL table is updated, but it does not display Success, instead, it displays sorry, Query could not execute...
php mysqli pdo sql-update
I am trying to execute the below code.
Execution gets success, but it is displaying sorry, Query could not execute...
My Codes:
class.php
public function admin_update($id,$update,$value)
{
try{
$stmt = $this->conn->prepare("UPDATE admin SET ".$update."=:_value WHERE Id=:_id");
$stmt->bindparam(":_value",$value);
$stmt->bindparam(":_id",$id);
$stmt->execute();
}
catch(PDOException $ex)
{
echo $ex->getMessage();
}
}
update.php
$id = "1";
if(admin_update($id,"Access","Y"))
{
echo "Success";
}
else
{
echo "sorry, Query could not execute...";
}
MySQL table is updated, but it does not display Success, instead, it displays sorry, Query could not execute...
php mysqli pdo sql-update
php mysqli pdo sql-update
asked Nov 11 at 6:29
namo
195
195
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You need to return the result of execute(). Since you are not returning anything, it is by default considered as null, hence your if() condition is always checking it as false, and going to the else() part.
public function admin_update($id,$update,$value)
{
try{
$stmt = $this->conn->prepare("UPDATE admin SET ".$update."=:_value WHERE Id=:_id");
$stmt->bindparam(":_value",$value);
$stmt->bindparam(":_id",$id);
return $stmt->execute(); // return the result of execute
}
catch(PDOException $ex)
{
echo $ex->getMessage();
return 0; // return 0 in case of failure
}
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You need to return the result of execute(). Since you are not returning anything, it is by default considered as null, hence your if() condition is always checking it as false, and going to the else() part.
public function admin_update($id,$update,$value)
{
try{
$stmt = $this->conn->prepare("UPDATE admin SET ".$update."=:_value WHERE Id=:_id");
$stmt->bindparam(":_value",$value);
$stmt->bindparam(":_id",$id);
return $stmt->execute(); // return the result of execute
}
catch(PDOException $ex)
{
echo $ex->getMessage();
return 0; // return 0 in case of failure
}
}
add a comment |
up vote
1
down vote
accepted
You need to return the result of execute(). Since you are not returning anything, it is by default considered as null, hence your if() condition is always checking it as false, and going to the else() part.
public function admin_update($id,$update,$value)
{
try{
$stmt = $this->conn->prepare("UPDATE admin SET ".$update."=:_value WHERE Id=:_id");
$stmt->bindparam(":_value",$value);
$stmt->bindparam(":_id",$id);
return $stmt->execute(); // return the result of execute
}
catch(PDOException $ex)
{
echo $ex->getMessage();
return 0; // return 0 in case of failure
}
}
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You need to return the result of execute(). Since you are not returning anything, it is by default considered as null, hence your if() condition is always checking it as false, and going to the else() part.
public function admin_update($id,$update,$value)
{
try{
$stmt = $this->conn->prepare("UPDATE admin SET ".$update."=:_value WHERE Id=:_id");
$stmt->bindparam(":_value",$value);
$stmt->bindparam(":_id",$id);
return $stmt->execute(); // return the result of execute
}
catch(PDOException $ex)
{
echo $ex->getMessage();
return 0; // return 0 in case of failure
}
}
You need to return the result of execute(). Since you are not returning anything, it is by default considered as null, hence your if() condition is always checking it as false, and going to the else() part.
public function admin_update($id,$update,$value)
{
try{
$stmt = $this->conn->prepare("UPDATE admin SET ".$update."=:_value WHERE Id=:_id");
$stmt->bindparam(":_value",$value);
$stmt->bindparam(":_id",$id);
return $stmt->execute(); // return the result of execute
}
catch(PDOException $ex)
{
echo $ex->getMessage();
return 0; // return 0 in case of failure
}
}
answered Nov 11 at 6:30
Madhur Bhaiya
18.8k62236
18.8k62236
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246391%2fdisplaying-false-result-while-the-query-returned-true%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown