php if sql query returns valid result - php

I'm sure this is a simple fix, I want to run a code block if an sql query comes back with a positive result. something like:
if($query = mysqli_query($cxn,"[query]"))
{
Code to be executed if the query returns a positive result
}
I have tried this format but doesn't work. I'm sure I have done this before but I'm running in to a wall here.
Hope you can help.

As stated in php.net/manual/mysqli.query.php, mysqli_query will:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
You should for you next question specify what you mean by positive result. But this code will see if there was an error, if the query returned an empty set, and so on... (I have not tried to run the code)
$result = mysqli_query($cxn,"[query]");
if ($result === FALSE) {
echo "There was an error";
}
else if ($result === TRUE) {
echo "The query was successful (a query that didn't return anything)";
}
else if (mysqli_num_rows($result) == 0) {
echo "The result is empty"
}
else {
echo '$result contains data';
}

So you mean if the query doesn't fail? then:
$query = mysqli_query($cxn, "[query]");
if($query !== false) {
// Code to be executed if the query returns a positive result
}

Related

MS Access & php

What is the correct condition for the if statement in the code below
$sql = "INSERT INTO table ($columns) VALUES ($values)";
echo $sql;
$results = odbc_exec($conn, $sql);
if ($results){
echo "Query Executed";
}else {
echo "Query failed " .odbc_error();
}
or should it be
if ($results > 0){
Please advise.
From the manual: http://php.net/manual/en/function.odbc-exec.php
Returns an ODBC result identifier if the SQL command was executed
successfully, or FALSE on error.
So checking for a $result is OK:
<?php
...
$result = odbc_exec($conn, $sql);
if ($result) {
// OK
} else {
// Failure
}
The correct way to test odbc_exec() for success or failure is:
if ($result !== false) {
// success
} else {
// failure
}
The documentation says:
Returns an ODBC result identifier if the SQL command was executed successfully, or FALSE on error.
If a function returns a boolean (FALSE) or non-boolean values, be sure to use the identity comparison operator.
While if ($result) may work in this case (it depends on possible ODBC result identifiers), it will not work in other cases.
For example, strpos() returns FALSE if the substring is not found and the position of the substring otherwise - that means 0 is a positive result.
if ($result) would be wrong in this case while if ($result !== FALSE) will work as expected.

What does the parameter do for the if statment?

What does this loop do? I don't know what it means. I've already tries using the internet to find out what the parameter does but I couldn't find anything.
if (mysqli_query($conn, $sql)) {
echo "Table Game Table created successfully";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
Functions like mysqli_query return a value, in this case a boolean which is either 0 or false (0) if it fails to execute properly (Its like your fridge has a function too, you put in milk and cold milk is returned but fails to do so if the outlet is not plugged in) and returns an object if it executed properly.
The thing with querys is that we always want to verify if a query is successful or not.
if(true){
# execute this code
} else {
# otherwise execute this block of code
}
if(($result = mysqli_query($conn, $sql)) != false){ #Translates to: If $result is not equal to false execute the following code.
# use $result here to print out data.
} else {
# failed the query cause $result equals to false.
}
Its programming logic 1:1, instead of trying to figure out what that function does try to do some basic stuff in the language first.

mysqli_query doesn't return false when no record was found [duplicate]

This question already has answers here:
mysqli_query() always return true [duplicate]
(4 answers)
Closed 1 year ago.
If I run this code, the if-clause won't return false even though the data record doesn't exist in the database myDB in table myTable. I don't know what's wrong..
// MySQLi-Connection...
$result = $mysqli->query("SELECT * FROM `myDB`.`myTable` WHERE `itemtype` = 'comment' AND `itemID` = 3");
if ($result) {
echo "record found!";
} else {
echo "record not found!";
}
The record with itemID = 3 doesn't exist, but my if-clause says that the $result returns true..
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries
mysqli_query() will return a mysqli_result object. For other successful queries
mysqli_query() will return TRUE.
#Reference PHP Manual
You can use
$result = $mysqli->query("YOUR QUERY"));
if($result->num_rows){
// Records Found
}else{
// Empty Result
}
Its much logical to use mysqli_num_rows here:
$result = $mysqli->query("SELECT * FROM `myDB`.`myTable` WHERE `itemtype` = 'comment' AND `itemID` = 3");
if($result->num_rows > 0) {
echo 'record found';
} else {
echo 'query is ok but no results found';
}
You can't put a mysqli_result object in your if since it will still evaluate as TRUE even if it yields 0 rows.
Your PHP if statement will evaluate to TRUE, as an empty mysql results set does not mean it is a failed query. The query ran ok, and mysql retrieved 0 rows, hence $result evaluates to true.
To check if a valid row has been retrieved, try:
if ( ! empty($result) ) {
echo "record found!";
} else {
echo "record not found!";
}

MySQL query acting weird

I've got:
mysql_connect($host,$username,$password);
#mysql_select_db("db") or die("Error: Cannot select database");
$query = "select password from users where name = '".$_POST['login-userid']."'";
$result = mysql_query($query);
if ($result == false) {
echo "Invalid username or password";
} else {
if (mysql_result($result,0) == hash('sha256', $_POST['login-password'])) {
echo "Logging in...";
}
}
For some reason I keep getting an error for the mysql_result line, even when it shouldn't be executed (when the username doesn't exist, ie $result evaluates to false).
mysql_query will only return false if there is an error. In this case there is no error, there are just 0 rows.
You need to use mysql_num_rows to get the number of rows returned.
You can var_dump($result) and see the value, if it's a resource (according to php), then your query was successful, if not false is returned. It's a boolean false but your == should still be respected if indeed false was returned.
mysql_query($query);
returns the result set of your query. It does not return true or false.
You may use mysql_num_rows() to check if the result of the query exist such as:
if(mysql_num_rows($query)) {
// exist
} else {
// does not exist
}

PHP MySQL query not working

I'm having trouble with this long sql query. If I change $result= mysql_query( to an echo statement and copy the resulting string into MySQL, it adds the data into the db just fine. It's only when I'm using PHP to do it that it fails.
Code:
$con = mysql_connect("-","-","-");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else {
// connected to database successfully
}
mysql_select_db("casemanagers", $con);
$result= mysql_query("INSERT INTO `criminal` (`JudgeID`, `Month`, `Year`, `PendingCapDefs`, `PendingCapCases`, `PendingNonCapDefs`, `PendingNonCapCases`, `AsgNewCapDefs`, `AsgNewCapCases`, `AsgNewNonCapDefs`, `AsgNewNonCapCases`, `AsgTRCapDefs`, `AsgTRCapCases`, `AsgTRNonCapDefs`, `AsgTRNonCapCases`, `AsgRCCapDefs`, `AsgRCCapCases`, `AsgRCNonCapDefs`, `AsgRCNonCapCases`,
`DispGPCapDefs`, `DispGPCapCases`, `DispGPNonCapDefs`, `DispGPNonCapCases`, `DispDDCapDefs`, `DispDDCapCases`, `DispDDNonCapDefs`, `DispDDNonCapCases`, `DispNPCapDefs`, `DispNPCapCases`, `DispNPNonCapDefs`, `DispNPNonCapCases`, `DispODCapDefs`, `DispODCapCases`, `DispODNonCapDefs`, `DispODNonCapCases`, `DispBTACapDefs`, `DispBTACapCases`, `DispBTANonCapDefs`, `DispBTANonCapCases`, `DispBTCCapDefs`, `DispBTCCapCases`, `DispBTCNonCapDefs`, `DispBTCNonCapCases`, `DispJTACapDefs`, `DispJTACapCases`, `DispJTANonCapDefs`, `DispJTANonCapCases`, `DispJTCCapDefs`, `DispJTCCapCases`, `DispJTCNonCapDefs`, `DispJTCNonCapCases`, `DispADDCapDefs`, `DispADDCapCases`, `DispADDNonCapDefs`, `DispADDNonCapCases`, `DispSCDCapDefs`, `DispSCDCapCases`, `DispSCDNonCapDefs`, `DispSCDNonCapCases`, `DispCTOCapDefs`, `DispCTOCapCases`, `DispCTONonCapDefs`, `DispCTONonCapCases`, `OldCapDefs`, `OldCapCases`, `OldNonCapDefs`, `OldNonCapCases`) VALUES ('$judgeID',' $month',' $year',' $PendingCapDefs','$PendingCapCases','$PendingNonCapDefs','$PendingNonCapCases','$AsgNewCapDefs','$AsgNewCapCases','$AsgNewNonCapDefs','$AsgNewNonCapCases','$AsgTRCapDefs','$AsgTRCapCases','$AsgTRNonCapDefs','$AsgTRNonCapCases',' $AsgRCCapDefs','$AsgRCCapCases','$AsgRCNonCapDefs',' $AsgRCNonCapCases','$DispGPCapDefs','$DispGPCapCases','$DispGPNonCapDefs','$DispGPNonCapCases','$DispDDCapDefs','$DispDDCapCases','$DispDDNonCapDefs','$DispDDNonCapCases',' $DispNPCapDefs',' $DispNPCapCases',' $DispNPNonCapDefs','$DispNPNonCapCases','$DispODCapDefs',' $DispODCapCases','$DispODNonCapDefs','$DispODNonCapCases','$DispBTACapDefs','$DispBTACapCases','$DispBTANonCapDefs','$DispBTANonCapCases','$DispBTCCapDefs','$DispBTCCapCases','$DispBTCNonCapDefs','$DispBTCNonCapCases','$DispJTACapDefs','$DispJTACapCases','$DispJTANonCapDefs','$DispJTANonCapCases','$DispJTCCapDefs','$DispJTCCapCases','$DispJTCNonCapDefs','$DispJTCNonCapCases','$DispADDCapDefs','$DispADDCapCases','$DispADDNonCapDefs','$DispADDNonCapCases','$DispSCDCapDefs','$DispSCDCapCases','$DispSCDNonCapDefs','$DispSCDNonCapCases','$DispCTOCapDefs','$DispCTOCapCases','$DispCTONonCapDefs','$DispCTONonCapCases','$OldCapDefs','$OldCapCases','$OldNonCapDefs','$OldNonCapCases');");
if ($result==1){
$statusCaption = 'New Civil Report';
echo 'Report Successfully Saved!<br/><br/><-- Back to User Menu';
}
else {
$statusCaption = 'Error';
echo 'There was a problem with one or more of your entries. Please try again.<br/><br/><--Back to Civil Report';
}
mysql_query() returns a statement handle on success, or boolean false on failure/errors. It'll never return an integer '1'.
if ($result !== false) {
... success ...
} else {
... failure ...
}
Note that 'failure' is only due to a syntax error in the query or a violation of a constraint in the db or a failure in the client-server communications link. A select query that returns no rows is NOT a failure. It's just a result set that happens to contain no rows.
$result= mysql_query("INSERT INTO `criminal` VALUES ('$judgeID',' $month',' $year',' $PendingCapDefs','$PendingCapCases','$PendingNonCapDefs','$PendingNonCapCases','$AsgNewCapDefs','$AsgNewCapCases','$AsgNewNonCapDefs','$AsgNewNonCapCases','$AsgTRCapDefs','$AsgTRCapCases','$AsgTRNonCapDefs','$AsgTRNonCapCases',' $AsgRCCapDefs','$AsgRCCapCases','$AsgRCNonCapDefs',' $AsgRCNonCapCases','$DispGPCapDefs','$DispGPCapCases','$DispGPNonCapDefs','$DispGPNonCapCases','$DispDDCapDefs','$DispDDCapCases','$DispDDNonCapDefs','$DispDDNonCapCases',' $DispNPCapDefs',' $DispNPCapCases',' $DispNPNonCapDefs','$DispNPNonCapCases','$DispODCapDefs',' $DispODCapCases','$DispODNonCapDefs','$DispODNonCapCases','$DispBTACapDefs','$DispBTACapCases','$DispBTANonCapDefs','$DispBTANonCapCases','$DispBTCCapDefs','$DispBTCCapCases','$DispBTCNonCapDefs','$DispBTCNonCapCases','$DispJTACapDefs','$DispJTACapCases','$DispJTANonCapDefs','$DispJTANonCapCases','$DispJTCCapDefs','$DispJTCCapCases','$DispJTCNonCapDefs','$DispJTCNonCapCases','$DispADDCapDefs','$DispADDCapCases','$DispADDNonCapDefs','$DispADDNonCapCases','$DispSCDCapDefs','$DispSCDCapCases','$DispSCDNonCapDefs','$DispSCDNonCapCases','$DispCTOCapDefs','$DispCTOCapCases','$DispCTONonCapDefs','$DispCTONonCapCases','$OldCapDefs','$OldCapCases','$OldNonCapDefs','$OldNonCapCases');") or die(mysql_error());
try it
BTW, you're probably missing a lot of variables
try to add
or die(mysql_error());
just after query
it will give you answer/error/tell you what's wrong.
Please read the PHP.net page. The function mysql_query() does not return true/false. It only returns false when there was an error. Your code should look like this:
if ($result === false) {
//error
}
else {
//success
}
That way you only see the error occurs when it REALLY REALLY returns false.
EDIT: Also, I never include ; in the actual query in your PHP code when you have one query. That is a query delimiter and is only needed in command prompt or when you are executing two queries in the same mysql_query() instance.

Categories