- Forums
- MySQL
- How To Connect Login With PHP Script Code Into Mysql Database From A File
This Page Contains information about How To Connect Login With PHP Script Code Into Mysql Database From A File By wallpaperama in category MySQL with 3 Replies. [872], Last Updated: Sat May 18, 2024
wallpaperama
Thu Mar 29, 2007
3 Comments
2145 Visits
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.
https://www.wallpaperama.com/forums/how-to-connect-login-with-php-script-code-into-mysql-database-from-a-file-t1000.html