How To Connect Login With PHP Script Code Into Mysql Database From A File

Mobile
feeds
Welcome Login | Register

How To Connect Login With PHP Script Code Into Mysql Database From A File
  Forums Index
      » MYSQL Forums
        » » How To Connect Login With PHP Script Code Into Mysql Database From A File



How To Connect Login With PHP Script Code Into Mysql Database From A File
Post Description:
Post Tags: how, to, connect, login, with, php, script, code, into, mysql, database, from, a, file, mysql database, support, help, answers, questions, help
This Post Has Been Viewed 635 Times Since Thu Mar 29, 2007 12:54 pm Posted By hostman with 0 replies
Next Post »» MYSQL Query SELECT Tutorial Guide Steps Database Instructions Examples
How To Connect Login With PHP Script Code Into Mysql Database From A File
when i started to learn about mysql and php, i always had a hard time understainding how to get data from a mysql database using a php file. one fo the first things you must tell mysql is which database to login into. I wrote this sample code for this simple step by step tutorial guide to help you maybe understand more on how you can connect or login to your mysql database using a php file or script.

this is what you will need to know before this works:
1. your database hostname. Most of the time its called localhost but depending on your hosting company, they may have a different name, if you are not sure, contact them

2. your database user name: if you don't know this, contact your hosting company

3. your database user password: if you don't know this, contact your hosting company

4. you database name: this is the database you are going to be using for your script. if you don't know this, contact your hosting company

so lets say i have a database called "user_list", my user name is "dbu2", my user password is "mypassd" and my mysql hostname is "localhost" this is how i would write the connection function of the script:

this is an example you can use to connect to your databases:

     Code:
<?php
###### ENTER YOUR DATABASE INFORMATION HERE #######

$hostname = "localhost";
$username ="dbu2";
$userpassword = "mypassd";
$database_name = "user_list";

###########################################

$db = mysql_connect($hostname, $username , $userpassword);
mysql_select_db($database_name,$db);
?>


now that i have the connection, its time to get and display the database information form mysql database tables. to get the informatio out of the database table i will be using the SELECT function in mysql. so lets say the table in the user_list database is called "my_table" and i want to display the "first_name" field, this is how i would do it"

     Code:

$sql = "SELECT first_name FROM my_table";
$result = mysql_query($sql ,$db);

if ($myrow = mysql_fetch_array($result))
{
do
{
echo"Name: $myrow[first_name ]<br>";
}
while ($myrow = mysql_fetch_array($result));

}


so now to put the whole script together:

     Code:
<?php
###### ENTER YOUR DATABASE INFORMATION HERE #######

$hostname = "localhost";
$username ="dbu2";
$userpassword = "mypassd";
$database_name = "user_list";

###########################################

$db = mysql_connect($hostname, $username , $userpassword);
mysql_select_db($database_name,$db);

$sql = "SELECT first_name FROM my_table";
$result = mysql_query($sql ,$db);

if ($myrow = mysql_fetch_array($result))
{
do
{
echo"Name: $myrow[first_name ]<br>";
}
while ($myrow = mysql_fetch_array($result));

}


continuing with my example, lets say i have 3 visitors in my database and the database information looks like this:
uid first_name last_name
------------------------------------
1 tommy usiras
2 luke skywalker
3 standford moleculas

this is what my script would display:


     Code:
tommy
luke
standford


if you have any question, you can reply to my post here.
Thanks to the folks at www.webune.com web hosting for their support on this tutorial guide.


Leave Your Comments






Share
URL:
You can use this HTML code to put it on your website to show your friends this wallpaper. Use this code on your profile like myspace, friendster, Facebook or others. Just Copy and Paste it in your HTML on your websites



Fourms BBCODE:
BBCODE is use on forums. You can put this code on all your BBCODE enabled forums like PhpBB, vBulletin® and others. Just Copy and Paste this code on your Posts and Replies on your forums





Comments and replies About How To Connect Login With PHP Script Code Into Mysql Database From A File




(0) Comments