PHP Function to validate setting

Yes, I intend to do that eventually…

Do I need to do this… ?

// Function to validate the flag value
function CheckFlagValue($pdo, $FlagName, $FlagValue)
{
    $sql  = "SELECT 1 FROM flags WHERE flagname = ? AND flagvalue = ? LIMIT 1";
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$FlagName, $FlagValue]);
    if ($stmt->rowCount() == 1) {
    	return true;
    } else {
    	return false;
    }
}

That code can be abbreviated to

return $stmt->rowCount() == 1;

There is never any reason for having an if statement to return true or false - simply return the condition.

2 Likes

This actually works too :smile:

I’m not sure I understand why but it works :stuck_out_tongue:

Can you explain the == 1 part

True. Unless you want to display the data.

== 1, > 0, < 1, and many other ones aren’t necessary. Normally you would find those conditions on a tutorial for noobies. With num_rows and rowCount, if the data exists, it’ll return a 1 which means true. If the data doesn’t exist, it’ll return a 0 which means false. In any case it would be like if(true == true) when you already know it’s already true and will always return the first error checking. False would always be returned in the else statement. That’s being really redundant so it’s not really necessary to repeat one’s self by comparing the condition when the data is returned as true already.

So this is fine if you were to use it in an if statement.

if($prepare->rowCount()) {

or

if($prepare->num_rows) {
1 Like

Now I understand, return $stmt->rowCount() on its own will return 0 or 1 :smile:

Yup and the best part is, if num_rows or rowCount() returns a 1. It’ll automatically return what is in the if statement no matter what. And like I said above, if num_rows or rowCount() returns a 0, it’ll return what is in the else statement.

So it’s not really needed to do something like

if($stmt->rowCount() == 0) when it’s exactly the same as } else {. And same with
if($stmt->rowCount() == 1) when it’s exactly the same thing as if($stmt->rowCount())

^ in a way.

1 Like
  1. There is such a function.
  2. You don’t need it anyway.

Look, you’ve gone astray from the plain and simple working code I gave you already. You don’t need a count actually. To count something is to make yourself aware of how many something you have.

In your case you don’t need it. You need only know whether you have somethng or not. It’s different question. For this you need just select something and see whether anything was returned. As simple as that. Exactly what my code does.

While you should never use rowCount or num_rows to see whether your query returned something or not. Because you already have the selected data iself - no need anything else.

return $stmt->fetchColumn();

See, here you selected one row from the database and fetched it. That’s exactly what you should do when working with database. If it returns a value (1), then it will be returned from function which will be count as TRUE. If no row found, then this code will return FALSE. Exactly what you need. That’s why this function works as intended. No extra code needed, No row count or comparisons required.

While your approach with rowCount is wrong. It makes no sense to select some data but not to fetch it.

you should handle your errors the right and proper way by using num_rows (mysqli_*) and rowCount()

Don’t listen to that guy. He just have no idea what he’s talking about. This function has absolutely nothing to do with whatever error handling at all

The difference being that thsoe return 0 or 1 where comparing it to 1 returns true or false as it would then be the result of the comparison that is returned rather than the value - just as in the original

if(condition) return true else return false
and
return condition

are equivalent in that both return true or false and not the value of part of the condition

How’s this…

<?php

class ScriptControl
{

  private $pdo;
  private $ScriptName;
  
  public function __construct($pdo, $ScriptName)
  {
    $this->pdo = $pdo;
    $this->ScriptName = $ScriptName;
  }

  public function IsEnabled();
  {
    $sql = "SELECT 1 FROM config WHERE config_name = ? AND config_value = 1 LIMIT 1";
		$stmt = $this->pdo->prepare($sql);
		$stmt->execute([$this->ScriptName]);
		return $stmt->fetchColumn();
  }

  public function IsRunnable();
  {
    date_default_timezone_set("Europe/London");
    $time = date("Hi");
    $sql = "SELECT 1 FROM config WHERE config_name = ? AND config_value = ? LIMIT 1";
		$stmt = $this->pdo->prepare($sql);
		$stmt->execute([$this->ScriptName, $this->$time]);
		return $stmt->fetchColumn();
  }
  
}

?>

and…

<?php

include_once('PDO.class.php');
include_once('ScriptControl.class.php');

$ThisScript = new ScriptControl('SendMessage');

if ( (!$ThisScript->isEnabled()) && (!isset($_POST['bypass'])) )
{
    echo "<p>This script is not enabled</p>\n";
    die();
}

if ( (!$ThisScript->isRunable()) && (!isset($_POST['bypass'])) )
{
  echo "<p>This script is not scheduled to run</p>\n";
  die();
}

?>

The IsEnabled will be used by about 10 different scripts, whereas the IsRunnable will only be used by 1 so is it right to put them together in the same class?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.