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

Currently I have Windows App which is directly opening database connection (SQL Server is
I'm a moderate to good PHP programer and have experience with terminal/shell scripts but
I'm not very good at regex but maybe there's a simple way to achieve
I am creating a simple RSS application and I am not that good in
Can anyone please suggest me a good sample code for zooming image in NSImageView.
Does anybody know a good C sample that loads bitmaps and handles all the
Does anyone have any good samples, resources, pointers etc. for C# Silverlight with RIA
I run a site where it is important to have good and simple URLs
What is good design in this simple case: Let's say I have a base
I have a simple winforms application that I am connecting to my database with

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.