Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End What is wrong with my connection file?

  • This topic is empty.
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #145058
    Robin
    Participant

    Hello,

    I have a file called config.inc.php, in this file I make the connection with my database:

    <?php
    @$db = new mysqli('localhost','username','password','database');
    if(mysqli_connect_errno()) {
        echo 'Error: Could not connect to database. Please try again later.';
        exit;
    }
    ?>  
    

    When I need the database connection I just include it in the page like this:

    <?php
    include 'inc/head.inc.php';
    
    include 'inc/config.inc.php';
    
    $query = "SELECT * FROM <the table>";
    $result = $db->query($query) or die($db->error);
    ?>
    

    But when I look at the page it gives me the error.. Is there something wrong in my code?

    Its probably some kind of stupid rookie mistake lol

    Robin

    #145062
    makeshift67
    Participant

    try:
    if($db->connect_errno)

    instead of :
    if(mysqli_connect_errno())

    #145072
    Robin
    Participant

    @makeshift67 I tried it and it doesn’t work

    #145074
    makeshift67
    Participant

    what does the error say? are the login credentials correct?

    #145077
    Robin
    Participant

    It just says:

    ‘Error: Could not connect to database. Please try again later.’

    For as far as I can tell the login credentials are like they’re suppose to be (even though my host wont let me choose my own database name, which sucks).

    @$db = new mysqli('localhost','username','password','database');
    

    localhost > just stays the same* username > crappy username host gave me password > password database > name of given database

    * Is this suppose to stay the same? Or is it the url of the site where you have to login to access your database?

    #145100
    waydechism5
    Participant

    Try removing the @ in front of the $db variable.

    #145143
    Julia Anderson
    Participant

    very informative discussion. thanks for sharing such information. http://www.mindmappingsite.com/sw-tool-reviews/software-reviews

    #145148
    Ashish Sinha
    Participant

    Hi,

    I want to develop a PHP based shopping cart. Anybody can suggest me how can I start?

    #145254
    Robin
    Participant

    Thanks for the help @traq it was indeed that I put localhost where I should’ve put the url to the database server. So I did that still no luck. Then I mailed my host and they said that I have the wrong url (which is weird because that was the url they put in their FAQ).

    But it finally works now!

    Thanks again

    Robin

    #145255
    Ashish Sinha
    Participant

    Thanks @Traq

Viewing 10 posts - 1 through 10 (of 10 total)
  • The forum ‘Back End’ is closed to new topics and replies.