For example:
$qrInsert = "INSERT INTO DBASE1.DBO.TABLE1 VALUES ('sampVal','sampVal','sampVal')";
odbc_exec($msCon,$qrInsert);
if( 'the query if successfully executed' ){
//then do this
//if not then
}else{
//then do this
}
Is there an easy way to know if it is successfully inserted, or in other cases, updated, and deleted succesfully?
Thanks
Try like
if(odbc_exec($msCon,$qrInsert))
{
echo 'Executed Successfully';
} else {
echo 'Error in execution';
}
odbc_exec only will return true if the query executed successfully,or else return false if it is not
if (odbc_exec($msCon,$qrInsert)){
// do this
}
else{
// do that
}
just replace your code with
$qrInsert = "INSERT INTO DBASE1.DBO.TABLE1 VALUES ('sampVal','sampVal','sampVal')";
if( odbc_exec($msCon,$qrInsert); )
{
//then do this
//if not then
}
else
{
//then do this
}
It Return 0 or 1 depends on failure or success of your query.You Can store result of "odbc_exec" in a variable & compare it in 'If','Else' condition.Benefit of storing in a variable is ,you can use it where ever you want .
i.e.
$query_result = odbc_exec($msCon,$qrInsert);
if($query_result)
echo 'Executed Successfully';
else
echo 'Execuion Error';
Related
The query executes .. but let's say for example the user changed the value of $_GET['sub'] to get an id which is not in the database , lets say for example : 60 .
it should print "NOT FOUND" instead it prints found ! Why is that ?
$main = new MainClass();
$subid = mysqli_real_escape_string($main->MsqlConRes,$_GET['sub']);
if (is_numeric($subid))
{
$main->query = mysqli_query($main->MsqlConRes,"SELECT * FROM subjects WHERE id = ".$subid."") or die(mysqli_error());
if ($main->query)
{
echo'Found';
}
else
echo'Not Found !';
}
else
$main->errors(404);
use mysqli_num_row() in if
if (mysqli_num_row($main->query)>0)
{
echo'Found';
}
else
echo'Not Found !';
Since query is executing properly so if ($main->query) will always be true
I m trying to delete a row in php mysql. The row is deleted quiet ok but mysqli_affected_rows($conn) is not giving a response so I can confirm the deletion for the user.
$sql="DELETE FROM `users` WHERE `memberID`='$id'";
$res=mysqli_query($conn,$sql);
if(mysqli_affected_rows($conn) == 1){
echo 'success'
} else {
echo "The User could not be deleted due to some error";
echo mysqli_affected_rows($conn);
}
You don't need to check by boolean value 0 or 1. But if you want to do it that way, you will need to declare a flag before that and then change the flag value according to condition after that you can do it [as your way]
(mysqli_affected_rows($conn) == 1)
But as default a simple if() conditon will check and return true on pass and false on fail.
So, simply you can do:
if($res)
{
//your codes if query is done
} else
{
//your codes if query fails
}
By default, the mysqli_query() function will return you a true if all went well or a false if there is any error. You can check if your query did not return any error like so:
if($res) {
echo "Delete success";
} else {
echo "Delete error: " . mysqli_error();
}
The basics of what I want to do is check the user values.
If they are there return true and go back to the page otherwise return false and print null values.
$query = sprintf("SELECT * from Users where username = ? and password = ?");
$params1 = array( $username, $password);
$stmt = sqlsrv_query($conn, $query, $params1);
if ($stmt === false)
{
die(print_r(sqlsrv_errors(), true));
}
$Users = array();
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$username1 = $row['username'];
$password1= $row['password'];
}
$Users["username"] = $username1;
$Users["password"] = $password1;
//echo json_encode($Users);
echo "you are here";
if ($username1==null)
{
return false;
echo "null values";
}
else {
return true;
if (!empty($_SERVER['HTTP_REFERER']))
header("Location: ".$_SERVER['HTTP_REFERER']);
else
echo "No referrer.";
}
echo "\n\nyou're at the end though";
sqlsrv_free_stmt($stmt);
sqlsrv_close( $conn );
}
else {
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
As you can see I am trying to debug the program and see where the program is going. It gets to the point
//echo json_encode($Users);
echo "you are here";
After that nothing else seems to work and I don't know why.
Directly after your working echo you have:
if ($username1==null)
{
return false;
...
}
else
{
return true;
...
}
...
So nothing after this will get executed because of the return statements. From the manual:
If called from within a function, the return statement immediately
ends execution of the current function, and returns its argument as
the value of the function call. return also ends the execution of an
eval() statement or script file.
If called from the global scope, then execution of the current script
file is ended.
Also note that you should check for the number of rows returned from your query, now you will get undefined variable warnings if no row was found.
before you enter thw while statment you need to check the row count in if statment if its not 0 return true else return false something like this
if(sqlsrv_num_rows($stmt)
{
// your code be here
return true;
}else{
// your code be here
return false;
}
http://php.net/manual/en/function.sqlsrv-num-rows.php
This is a really simple one, I just can't get my head around it sorry. I have this PHP code which picks up my form value, then compares it with the value stored in the database. That works fine.
However I am not sure how to write this logic in terms of this query:
If posted value = database value {
// do something } else { // do something else }
if (empty($_POST['order_id']) === false) {
// prepare data for inserting
$order_id = htmlentities(trim($_POST['order_id']));
$order_id = preg_replace("/[^0-9]/","", $order_id);
$result = mysqli_query($con,"SELECT * FROM listings WHERE order_id = $order_id");
$row = mysqli_fetch_assoc($result);
echo $row['order_id'];
}
SOLVED:
Solved the question, was a silly one I know! Just needed this at the end of the code:
if($order_id === $row['order_id']) {
echo 'found';
} else {
echo 'not found';
}
Try
If ($result->num_rows === 1) { do something } else { do something else }
Since you did the business logic in your query you can just use
if( ! is_null($row)) {
// do
} else {
// nothing
}
Did I read too much into "If posted value = database value "? Are you just referring to the order_id?
if ($row['listingName'] == $_POST['frm_listingName']) {
// something
}
else {
//something else
}
Check this code:
if (empty($_POST['order_id']) === false) {
// prepare data for inserting
$order_id = htmlentities(trim($_POST['order_id']));
$order_id = preg_replace("/[^0-9]/","", $order_id);
$result = mysqli_query($con,"SELECT * FROM listings WHERE order_id = $order_id");
if(mysqli_num_rows($result)>0)
{
//Match found. do something.
}
else
{
//No match found. do something.
}
}
N.B. In place of mysqli_num_rows($result) you can also use $result->num_rows
I have the following simple php code snippet, which will, when called, delete a relevant article from a database. The result is passed to a javascript function, which will update the page via AJAX. I would like to return the string false if the query fails, as I've below.
if($cmd=="deleterec"){
$deleteQuery = "DELETE FROM AUCTIONS1 WHERE ARTICLE_NO = ?";
if ($delRecord = $con->prepare($deleteQuery)) {
$delRecord->bind_param("s", $pk);
$delRecord->execute();
$delRecord->close();
echo "true";
} else {
echo "false";
}
}
I would like to know what I have missed and the correct way to check if a query was successful or not.
You're currently only checking whether the SQL statement is correctly prepared, you're not checking whether it actually deleted the record.
Try:
...
echo ($delRecord->affected_rows > 0) ? 'true' : 'false';
$delRecord->close();
That doesn't address whether you're correctly checking the result string in your Javascript code - if that's a problem we'll need more information.
You need to use mysqli->affected_rows() for checking if the query was successful (or you could use mysqli_stmt->execute()'s result value).
Taking your example, and modifying nothing but for the above:
if($cmd=="deleterec") {
$deleteQuery = "DELETE FROM AUCTIONS1 WHERE ARTICLE_NO = ?";
if ($delRecord = $con->prepare($deleteQuery)) {
$delRecord->bind_param("s", $pk);
$delRecord->execute();
if ($delRecord->affected_rows > 0) {
echo "true";
} else {
echo "false";
}
$delRecord->close();
}
}
Use the return value of mysqli_stmt->execute() to see if the query was executed successful.
if($cmd=="deleterec"){
$deleteQuery = "DELETE FROM AUCTIONS1 WHERE ARTICLE_NO = ?";
$delRecord = $con->prepare($deleteQuery);
if ( $delRecord === false ) {
echo "false";
}
$delRecord->bind_param("s", $pk);
if ( $delRecord->execute() ) {
echo "true";
} else {
echo "false";
}
$delRecord->close();
}
Checking prepare() could be left out because the query is fixed and should be working (unless there is an error on the server side). execute() returns true, if the query was executed successful. Using affected_rows() can be misleading because perhaps there was no item to delete and therefore affected_rows() whould return 0. Nontheless the query was successfully executed.
function run_query($query,$conn,$dbname){
$conn->select_db($dbname);
if($result = $conn->query($query)){
return $result;
}
else{
echo 'error ';
exit();
}
}
run_query() accepts three parameters, the query, connection, and DB. will through an error if the query was unsuccessful, else result object will be returned.