Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.