Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6002013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:55:01+00:00 2026-05-23T00:55:01+00:00

I’m using the following PHP MySQL database class. I’m curious as to what I

  • 0

I’m using the following PHP MySQL database class. I’m curious as to what I could do to make it more secure. I’m happy with it so far, but without suggesting to “use PDO” what can I do to improve this currently?

    <?

    class DbConnector {

    public static function getInstance(){
   static $instance = null;
   if($instance === null){
      $instance = new DbConnector();
   }

   return $instance;
    }

    var $theQuery;
    var $link;

    function DbConnector() {

        $host = 'localhost';
        $db     = '';
        $user = '';
        $pass = '';

        // connect to the db
        $this->link = mysql_connect($host, $user, $pass);
        mysql_select_db($db);
        register_shutdown_function(array(&$this, 'close'));
    }

    function find($query) {
        $ret = mysql_query($query, $this->link);
        if (mysql_num_rows($ret) == 0)
            return array();
        $retArray = array();

        while ($row = mysql_fetch_array($ret))
            $retArray[] = $row;

        return $retArray;
    }

    function insert($query) {
        $ret = mysql_query($query, $this->link);

        if (mysql_affected_rows() < 1)
            return false;
        return true;
    }

    function query($query) {
        $this->theQuery = $query;
        return mysql_query($query, $this->link);
    }

    function fetchArray($result) {

        return mysql_fetch_array($result);
    }

    function close() {
        mysql_close($this->link);
    }

    function exists($query) {
        $ret = mysql_query($query, $this->link);
        if (mysql_num_rows($ret) == 0)
            return false;
    }

    function last_id($query) {
        return mysql_insert_id($query);
    }


}

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:55:02+00:00Added an answer on May 23, 2026 at 12:55 am

    If you don’t want to use something that would automatically escape strings for you, you should at least provide an escapeString($string) method (which would call mysql_real_escape_string()) that you can use to escape strings when composing the query.

    If you want to start using PDO (highly recommended) then you will probably have different methods and won’t need to include an escaping method.

    As for general considerations:

    • don’t use var, but rather use public, protected or private (var has been deprecated as of PHP 5.0, and is currently an alias for public)
    • the __construct() method is preferred instead of the method with the name of the class
    • it would be better not to connect in the constructor, but rather only connect when you really need to connect in order to perform a query; a constructor should do as little as possible, and generally only initialize properties
    • mysql_connect() and mysql_select_db() will return false if they cannot do what they’re supposed to; you should throw an exception if that happens and catch it where you use the class
    • the mysql_query() call in the find() and exists() methods might return something other than a resource (false for example) so you should check for that before calling mysql_num_rows()
    • if you’re not using $theQuery at all, you should remove it
    • if you want to have access to $theQuery, you should make it private and provide a getQuery() accssor method
    • if you’re setting the $link property to the resource returned by mysql_connect(), you should use that property in all your mysql_* calls, such as the one at last_id()
    • you should make $link private (as a rule of thumb, all properties should be private or protected, and accessor methods should be provided when needed – read about the concept of encapsulation)
    • even though all methods are by default public, having an explicit public keyword before each of them will make things clearer
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.