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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:40:31+00:00 2026-06-02T02:40:31+00:00

I am creating a php restful API and currently I have the database connection

  • 0

I am creating a php restful API and currently I have the database connection information in each function.

//Connect To Database
    $hostname=host;
    $username=username;
    $password=password;
    $dbname=dbname;

    mysql_connect($hostname, $username, $password) OR DIE('Unable to connect to database! Please try again later.');
    mysql_select_db($dbname);
mysql_query($sqlApiAccess) or die('Error, insert query failed');

What is the best way of doing this, Can I have one database connection per php file? Or do I need to do it per function that uses the database.

  • 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-02T02:40:32+00:00Added an answer on June 2, 2026 at 2:40 am

    Create a config.php And add the code:

    config.php:

    $hostname = 'host';
    $username = 'username';
    $password = 'password';
    $dbname   = 'dbname';
    
    $conn = mysqli_connect($hostname, $username, $password) OR die('Unable to connect to database! Please try again later.');
    mysqli_select_db($conn, $dbname);
    

    Then in any file you wish to use mysql, add the following:

    script2.php

    <?php
    require_once 'config.php';
    
    mysqli_query($sqlApiAccess) or die('Error, insert query failed');
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating several php scripts. Must I always insert the host, username, password, etc.
I have been creating PHP projects for years, and have previously written Database classes,
I am creating a php backup script that will dump everything from a database
I'm new to PHP and I am currently creating a PHP web application for
I have some PHP creating automatic Twitter updates, which in the small print at
I'm creating a PHP CMS and have some system pages like a 404 page,
I'm creating a PHP script that fetches IDs from a database. The results are
I'm creating a PHP file that does 2 mysql database calls and the rest
I have multiple calls to many RESTful services. I translate to PHP using native
I'm currently creating a PHP website where users need to be able to upload

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.