Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] Mysqli Query About Is Empty Table Or Not?

mysqli oop database bind_param mysql

  • This topic is locked This topic is locked
1 reply to this topic

#1 Stasonix

Stasonix

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 115 posts
  • Programming Language:C++, PHP, JavaScript, Delphi/Object Pascal, Pascal
  • Learning:C++, PHP, JavaScript, Delphi/Object Pascal

Posted 08 June 2012 - 11:30 PM

So as you can read in the title of this topic I'm trying to know is some table is empty or no, now I am realized this issue with this code:


// if table is empty it must return TRUE, if full FALSE

private function isEmpty($tableName)
{

    if ($result = $this->mysqli->prepare("SELECT COUNT(*) FROM ?"))
    {

        $result->bind_param("s",$tableName);
        $result->execute();
        $row_cnt = $result->num_rows;

        if (empty($row_cnt))
        {

            return TRUE;

        }
        else
        {

            return FALSE;

        }

    }

}

this code seems not a really perfect, I would like to know is there a better way to get the same results, but using predicat SQL EXISTS or something else? Thank you.
  • 0

#2 Stasonix

Stasonix

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 115 posts
  • Programming Language:C++, PHP, JavaScript, Delphi/Object Pascal, Pascal
  • Learning:C++, PHP, JavaScript, Delphi/Object Pascal

Posted 09 June 2012 - 01:45 AM

$mysqli = new mysqli("localhost","root","","db");
if ($result = $mysqli->query("SELECT * FROM `table` LIMIT 1"))
{
if ($obj = $result->fetch_object())
{

  echo "NOT EMPTY";

}
else
{

  echo "empty";

}


$result->close();
}
$mysqli->close();

solved.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download