Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Code Using Different Database

Options
  • 11-11-2014 10:26am
    #1
    Registered Users Posts: 364 ✭✭


    Hey guys. I'm trying to do a login screen using sessions for my project and haven't really had any practice before. So I looked online for tutorials and sample code and came across this:

    http://www.formget.com/login-form-in-php/

    I copy and pasted the code to see if it would work for me and used the sample database called "company" and the table in it called "login". It worked absolutely fine so I decided to continue.

    The problems came when I decided to change the code to work with my database called "clinic" and its table called "members". For some strange reason, despite changing every word "company" to "clinic" and every word "login" in reference to the table to "members" the code still refers to the old database.

    By this I mean when I try to log in to the site with a username and password from the "members" table, I get an error saying "Username or Password is invalid". However, when I log in with a username and password from the "login" table from the company database, it logs in perfectly. This happens even after I've deleted the company database completely!

    I can't understand this at all. How the php is even connecting to the company database I just don't know. I seriously can't wrack my brain around this one.

    I'm not sure if I should post the code or not because it's five files and I don't want to be taking up loads of space.


Comments

  • Moderators, Computer Games Moderators, Technology & Internet Moderators Posts: 19,240 Mod ✭✭✭✭L.Jenkins


    Is there a string pointing to a DB and not referencing tables?


  • Registered Users Posts: 364 ✭✭Lago


    Something like this?
    $db = mysql_select_db("clinic", $connection);


  • Registered Users Posts: 2,797 ✭✭✭mightyreds


    put the connection code into a seperate file and include it in your php where you need to connect to the database that way you can just put up the connection file should be something like this
    <?php
    
    $con=mysql_connect("database","username","password");
    
    	if(!$con){
    		die("Database could not connect: ".mysql_error());
    	}
    	if(!mysql_select_db("database",$con)){
    	echo "Error in selecting the database";
    	}
    ?>
    


  • Registered Users Posts: 364 ✭✭Lago


    Sorry for the noob question but how would I then put that into the rest of my files to replace lines like:
    $connection = mysql_connect("localhost", "root", "");
    $db = mysql_select_db("clinic", $connection);


  • Registered Users Posts: 2,797 ✭✭✭mightyreds


    I'm a noob myself just I did this exact thing last year if you put at the top of your php
    include "filename" ;
    That will connect to the database without having to rewrite on every page you need it


  • Advertisement
  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    Lago wrote: »
    Sorry for the noob question but how would I then put that into the rest of my files to replace lines like:

    from a quick reading of the PHP manual you need to do both.
    $connection = mysql_connect("localhost", "root", "");

    this connects to the database server.
    $db = mysql_select_db("clinic", $connection);

    This sets the selected db as active.

    Every subsequent call to mysql_query() will be made on the active database.


    Please note that i have never written a line of PHP in my life nor have i used MySQL. this is all just from a quick peruse of the manual at PHP.Net
    It all seems pretty logical though.


  • Registered Users Posts: 797 ✭✭✭FobleAsNuck


    if you're learning it might be a good idea not to do it using tutorials using deprecated code. read up about PDO


Advertisement