/    Sign up×
Community /Pin to ProfileBookmark

Error when connecting to DB

Hi guys, i am basically new to php.. I have recently tried creating a web app using mysql and php.. However, i got a error that states:

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in C:wampwwwmain.php on line 8
Call Stack

# Time Memory Function Location

1 0.0000 241960 {main}( ) ..main.php:0
2 0.0010 251336 mysqli_select_db ( ) ..main.php:8
Could not find database (line 8)

whenever i tried to open the page via web browser.. the following are my connection codes:

[code=php]$hostname = “localhost”;
$username = “DBuser1”;
$dbname= “Newdb”;
$password = “newcomer”;
$conn = mysqli_connect($hostname,$username,$password);
mysqli_select_db($conn) or die(“Could not find database”);[/code]

May i know how do i fix this problem? It is really getting on my nerves… Please help!!! thank you!!!

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@iBeZiApr 12.2015 — You need to tell it which database you want to select.

[code=php]
mysqli_select_db($conn,$dbname) or die("Could not find database");

[/code]
Copy linkTweet thisAlerts:
@rootApr 13.2015 — 
  • 1. you should avoid creation of variables for the sake of it. Instead of having $hostname="localhost"; simply use the term "localhost" where it is needed.

  • 2. You can use a 4th parameter to select the database you wish to use in the opening command

  • 3. Keeping your login details separate from your main script is a good way of ensuring some kind of security should your main page become susceptible to a code injection attack, for example.

  • 4. Using an OO approach will reduce the need to keep a separate variable for the connection handle.


  • [code=php]include("database.php"); // database, username and password info :)
    $db = new mysqli("localhost",$username,$password,$databaseName); [/code]


    then any use of the $db object like a query requires only $result = $db->query("SELECT * FROM somedatabase WHERE wotsits='prawncoctail'"); and then $results contains your needed information based on your query.
    ×

    Success!

    Help @New_Web_Geek spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...