/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] DB Connection

Hello guys,

I am new to DBs and PHP, I’m working on a web application that will be storing the data on a database and I have all the HTML form created, I am using WAMP on my computer and my problem right now is that I don’t know where to put the “mysql_connect()” code which (if I’m not wrong) looks like this:

<?php
$con = mysql_connect(“localhost”,”username”,”pass”);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}

?>

Is this supposed to be on an external php file or where should I put it? Thanks in advance everybody.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@eastsideskiJun 15.2010 — This code can be put anywhere in your page. Many developers put it in a separate file and use an include ( include("db_connect.php"); ) to connect. This way, if you need to change a password, you only have to change one file

I would also select the database you want in the same file
[code=php]mysql_select_db("my_db", $con);[/code]
Copy linkTweet thisAlerts:
@Sub_SevenauthorJun 15.2010 — Thanks for the quick reply eastsideski,

I may have more questions since I keep getting stuck, this is an example of some test code I wrote to see if it works:

<html>

<head>

<?php

mysql_select_db("application", $con);

$con = mysql_connect("localhost","root","3@mig05");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

?>

</head>

<body>

<form action="" method="post">

Name: <input type="text" name="first_name" />

Surname: <input type="text" name="second_surname" />

<input type="submit" />

</form>

</body>

</html>

I think what I am missing is some code in the "form action="" " part of the code, or whatever part of code that actually inserts the data into the database...

Thanks again for the help.
Copy linkTweet thisAlerts:
@fouskalexJun 15.2010 — First you make the connection with the database and then you make the selection for the database you want to use.
[CODE]<?php
$con = mysql_connect("localhost", "root", "3@mig05");
if(!$con) {
die("Database connection failed :" . mysql_error());
}
$db = mysql_select_db("application", $con);
if(!$db) {
die("Database selection failed :" . mysql_error());
}
?>[/CODE]

Put all this above the html tag, at the beggining of your file.The best way is to use another file like eastsideski told you and then include it in any file you want but if it is too much for you leave it for now.

Check the text that you use with die(). One says 'connection' and the other says 'selection'. That way when you get the error you will know which one has failed.

In action="" you put the file that you have written the sql that you want to run. As I can see you haven't done anything yet. I suppose you want to insert the data into the database, so you must write an sql query to do so.

Try to find tutorials in google.You will find a lot of them.

"Sql query examples" or "query insert into" will do.
Copy linkTweet thisAlerts:
@Sub_SevenauthorJun 15.2010 — Thanks so much fouskalex,

In the mean time I was looking for info online, I got it working finally, but I will definitely pay attention to your comments.

Now before we can close this case can you just clarify a few things please

  • 1. If I have php code, the extension of that code HAS to be .php no exceptions correct?


  • 2. If question 1 is right and if I send my php code to an external file then I can go back to my .html extension?


  • Thanks for the help.
    Copy linkTweet thisAlerts:
    @fouskalexJun 15.2010 — Yes you write php only in php files (as I know) and you don't have to go back to your .html files because you don't need to.

    You can write your html as you did till now in your .php files. Nothing changes.

    The only thing that changes is that you will use php as well.

    After all when you include the connection.php file that you will make you will do it with php so the file must be .php.
    Copy linkTweet thisAlerts:
    @Sub_SevenauthorJun 15.2010 — Allright, thanks so much for all your help, now I gotta start setting everything else up, have a nice evening!
    ×

    Success!

    Help @Sub_Seven 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.16,
    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,
    )...