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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:48:05+00:00 2026-05-15T01:48:05+00:00

my sample is good or not? I have a good connection to the database,

  • 0

my sample is good or not?
I have a good connection to the database, or too should be in the class?

Thanks

<?php
mysql_connect('localhost','root','admin');
mysql_select_db('test');

class UserDisplay
{
    function getDisplayName()
    {
        $sql = 'select first_name, last_name, display_name from users where user_id = "3"';
        $results = mysql_query($sql);
        $row = mysql_fetch_array($results);
        $this->user_id = $user_id;

        return $this->user_id;
    }
}

class UserInsert

    function InsertName($name)
    {
        mysql_query("INSERT INTO Persons (first_name)VALUES ('".$name."')");
    }
}

$userD = new UserDisplay();
echo "User known as: " . $userD->getDisplayName() . "\n";

$userI = new UserInsert();
$userI->InsertName("Peter");
?>
  • 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-15T01:48:05+00:00Added an answer on May 15, 2026 at 1:48 am

    Probably be better off just making them functions, instead of wrapping them up in classes. Extract all of it, except the last into it’s own php file. You forgot to extract display name from $user

    // myFunctions.php
    <?php
    function connectDb() {
        mysql_connect('localhost', 'root', 'admin');
        mysql_select_db('test');
    }
    
    function closeDb() {
        mysql_close();
    }
    
    function displayName() {
        $sql = 'select first_name, last_name, display_name from users where user_id = "3"';
        $results = mysql_query($sql);
        $user = mysql_fetch_array($results);
        return $user['display_name'];
    }
    
    function insertFirstName($name) {
        mysql_query("INSERT INTO Persons (first_name)VALUES ('" . $name . "')");
    }
    ?>
    
    // index.php
    <?php
    require_once 'myFunctions.php';
    
    connectDb();
    echo "User known as: " . displayName() . "\n";
    insertFirstName("Peter");
    closeDb();
    ?>
    

    But if you insist on using classes:

    // User.php
    <?php
    class User {
    
        private function connectDb() {
            mysql_connect('localhost', 'root', 'admin');
            mysql_select_db('test');
        }
    
        private function closeDb() {
            mysql_close();
        }
    
        function displayName() {
            $this->connectDB();
    
            $sql = 'select first_name, last_name, display_name from users where user_id = "3"';
            $results = mysql_query($sql);
            $user = mysql_fetch_array($results);
    
            $this->closeDB();
    
            return $user['display_name'];
        }
    
        function insertFirstName($name) {
            $this->connectDB();
            mysql_query("INSERT INTO Persons (first_name)VALUES ('" . $name . "')");
            $this->closeDB();
        }
    }
    ?>
    
    // index.php
    <?php
    require_once 'User.php';
    
    $user = new User;
    echo "User known as: " . $user->displayName() . "\n";
    $user->insertFirstName("Peter");
    ?>
    

    Actually not bad. abstracted out the db connection and close.

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

Sidebar

Related Questions

Anyone have a good resource or provide a sample of a natural order sort
I'm not very good at C, and I always get stuck on simple string
Does anyone know of a good sample (inline in reply, or web tutorial) for
Where can I find a good sample of Asynchronous IO on files with thread
I am looking for some good links with best practices and sample code on
I'm looking for good/working/simple to use PHP code for parsing raw email into parts.
My question is how does one abstract a database connection from the model layer
Below is my very simple static class. Not sure what is wrong. I am
I am looking for a simple but good enough Named Entity Recognition library (and
I searched all over this site and the web for a good and simple

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.