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 9169067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:47:04+00:00 2026-06-17T15:47:04+00:00

I have a class written in PHP which I use to communicate with my

  • 0

I have a class written in PHP which I use to communicate with my website databases. I am also writing a Java application which will communicate with a database too. I was wondering if it was possible to use the same structure but convert it to java and are there any issues I would need to consider if I do this. Here is my php class…

<?php

class Database {

    private static $instance = null;
    private $connection;
    private $selected_database;
    private $last_query;

    public static function getInstance() {
        if (self::$instance != null) {
            return self::$instance;
        } else {
            return new self;
        }
    }

    function __construct() {
        $this->open_connection();
    }

    private function open_connection() {
        $this->connection = mysql_connect(DB_HOST, DB_USER, DB_PASS);
        if (!$this->connection) {
            die('Database connection failed: ' . mysql_error());
        } else {
            $this->selected_database = mysql_select_db(DB_NAME, $this->connection);
            if (!$this->selected_database) {
                die('Database selection failed: ' . mysql_error());
            }
        }
    }

    private function close_connection() {
        if (isset($this->connection)) {
            mysql_close($this->connection);
            unset($this->connection);
        }
    }

    public function query($sql) {
        $this->last_query = $sql;
        $result = mysql_query($sql, $this->connection);
        $this->confirm_query($result);
        return $result;
    }

    private function confirm_query($result) {
        if (!$result) {
            die('Database query failed: ' . mysql_error() . '<br>Last SQL query: ' . $this->last_query);
        }
    }

    public function escape_value($string) {
        return mysql_real_escape_string($string);
    }

    public function fetch_array($result) {
        return mysql_fetch_array($result);
    }

    public function fetch_assoc($result) {
        return mysql_fetch_assoc($result);
    }

    public function num_rows($result) {
        return mysql_num_rows($result);
    }

    public function insert_id() {
        return mysql_insert_id($this->connection);
    }

    public function affected_rows() {
        return mysql_affected_rows($this->connection);
    }

    public function get_fields_from($table_name) {
        $result = $this->query("SHOW COLUMNS FROM `{$this->escape_value($table_name)}`");
        $fields = array();
        while ($row = $this->fetch_assoc($result)) {
            $fields[] = $row['Field'];
        }
        return $fields;
    }

}

?>
  • 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-06-17T15:47:05+00:00Added an answer on June 17, 2026 at 3:47 pm

    No you can create a class in Java, which has a structure, that is nearly like the one above.

    or die() is like throwing an Exception. You should be aware, that there are different ways of returning data from a database in Java and PHP. PHP often uses arrays or assoc-arrays. In Java you will get a “pointer” to the result.
    You must decide, if you want to transfer the data to an array, because of the memory consumption.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a class that will handle internal logging in my application. Now
I have a PHP script which calls methods from a class I have written.
I have written a simple OCI wrapper class in PHP which uses persistent connections
I have a useful object written in PHP lets call it my_class.php <?php class
I have a class written in C++ that uses also some definitions from cuda_runtime.h,
I have a class written in c# which is acting as a wrapper around
I have written a class in python that implements __str__(self) but when I use
So I have this database class in PHP and I only have 1 function
Starting out with php, I have written a basic authentication script which prints out
I have a class written in php that is supposed to find the labels

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.