Jump to content

Keep MYSQL Calls in Functions?


limitphp

Recommended Posts

I never thought about it before, but would it be faster to keep mysql calls in functions?

by mysql stuff, I mean my mysql_query, and mysql_result stuff.

 

Here's an example I saw on php.net

<?php
function mysql_evaluate($query, $default_value="undefined") {
    $result = mysql_query($query);
    if (mysql_num_rows($result)==0)
        return $default_value;
    else
        return mysql_result($result,0);
}

function mysql_evaluate_array($query) {
    $result = mysql_query($query);
    $values = array();
    for ($i=0; $i<mysql_num_rows($result); ++$i)
        array_push($values, mysql_result($result,$i));
    return $values;
}
?>

 

And then examples using those functions....

$customer_count = mysql_evaluate("SELECT COUNT(*) FROM customers");

$customer_names = mysql_evaluate_array("SELECT name FROM customers");

 

I never thought about this, but it seems like it would simplify my code....

 

Do yall do similar things to this?

 

 

Link to comment
https://forums.phpfreaks.com/topic/144109-keep-mysql-calls-in-functions/
Share on other sites

Yeah I made my own class.  It's got all the basics with some error checking.  You're welcome to use it.

 

        $host="***";
      $user_name="***";
      $password="***";
      $db="***";
      
      define("DB",$db);
      define("HOST",$host);
      define("USERNAME",$user_name);
      define("PASSWORD",$password); 
      
   class db_works
   {
      var $Query_ID=0;
      var $connection=0;

      function connect()
      {
         if($this->connection==0) 
         {
            $this->connection=mysql_connect(HOST,USERNAME,PASSWORD) or die("Database Error
".mysql_error());
            $SelectResult = mysql_select_db(DB, $this->connection) or die("Could not Select Database".mysql_error());
         } 
         else 
         {
            echo "Database Connection Could not be Established";
            die();
         }
      }
   
      function query($sql) 
      {
         $this->Query_ID=mysql_query($sql,$this->connection);
         if(!$this->Query_ID)
         {
            $errorstr = mysql_error();
            if (stripos($errorstr, "Duplicate") === false)
            {
               echo "Query Failed " . $errorstr . "\n";
            }
         } 
         else return $this->Query_ID;
      }

      function connection_close()
      {
         mysql_close($this->connection);
      }
   }
   
?>

Yeah I made my own class.  It's got all the basics with some error checking.  You're welcome to use it.

 

  <?php
      $host="***";
      $user_name="***";
      $password="***";
      $db="***";
      
      define("DB",$db);
      define("HOST",$host);
      define("USERNAME",$user_name);
      define("PASSWORD",$password); 
      
   class db_works
   {
      var $Query_ID=0;
      var $connection=0;

      function connect()
      {
         if($this->connection==0) 
         {
            $this->connection=mysql_connect(HOST,USERNAME,PASSWORD) or die("<b>Database Error</b><br>".mysql_error());
            $SelectResult = mysql_select_db(DB, $this->connection) or die("Could not Select Database".mysql_error());
         } 
         else 
         {
            echo "Database Connection Could not be Established";
            die();
         }
      }
   
      function query($sql) 
      {
         $this->Query_ID=mysql_query($sql,$this->connection);
         if(!$this->Query_ID)
         {
            $errorstr = mysql_error();
            if (stripos($errorstr, "Duplicate") === false)
            {
               echo "Query Failed " . $errorstr . "\n";
            }
         } 
         else return $this->Query_ID;
      }

      function connection_close()
      {
         mysql_close($this->connection);
      }
   }
   
?>

 

thank you.

 

I see you have the connection stuff in your query function.  Right now, I've been putting:

 

$con = mysql_connect('localhost', 'root') or die ('Failed to connect to the database: '.mysql_error());

mysql_select_db ('testdata',$con);

 

at the top of every php page (via a include file) that uses the database.

 

On my live site, same thing, only it has the user and password in it.

 

Is this a bad practice?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.