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!";
}
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
}
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.