As we begin the month of May and is approaching the end of semester in my school, and thus the delivery of projects in each subject this post I'll do to help my fellow engineers for some strange reason does not like to program and therefore will suffer for a couple of months.
So I'll show you how we can connect to a database
- coded PHP , well the way I used to think is good: p
- Database. This file will be called conexion.php (you can call whatever you want) and type in the following:
function () {$ db_host = "localhost" / / Host to connect to, default is localhost $ db_nombre = "DB" / / Name of the database we use
$ db_user = "user "/ / Username to access the database $ db_pass = "password" / / User password
/ / Now we are making a connection and call '$ link' $ link = mysql_connect ($ db_host, $ db_user $ db_pass) or die ("Error connecting to database."); / / Select the database that we want mysql_select_db ($ db_nombre, $ link) or die ("Error selecting the database. "); / / return $ link because we will need it later, when we want to query. return $ link;} ?>
2 .- We must include the above file in each script who needs access to the database with the following code: include ('conexion.php') / / do we call the file with connection data 3 .- Now teach them to take file easily conexion.php on a query to a table called friends, would be as follows:
\u0026lt;?
include ('conexion.php') / / That is the file with connection data
/ / Just call the function that had the data to connect $ link = Log ();
/ / In a PHP write the query variable.
$ sql = "SELECT name, name, phone FROM friends ";
/ / With the function 'mysql_query ()' perform the query you want in the database
$ result = mysql_query ($ sql, $ link) or die (" The following query contains a error:
nSQL:
$ sql);
/ / Now we '$ result' all records in the query, we must now show them one to one
while ($ row = mysql_fetch_array ($ result))
{/ / The while we should already know he does, 'mysql_fetch_array ()' moves one by one the records / / As we have gotten a record $ row, we must show what interests us on the screen. echo "Name is". $ Row ['name']."; / / To display the' name '
echo "Name is". $ Row [' name']."; / / To display the 'name'
echo "The Phone is:". $ row ['phone']."; / / To show the' phone '} ?> The result of the implementation the script would be something like:
Name is: Angel The Last Name is: González
El Tel is: 1425367896
The name is: Carlos
Last Name is: Ruíz
El Tel is: 7485963210
The name is: Karen
The Last Name is: Carrasco
El Tel is: 4578123696
Assuming that these are the data contained in the database. I hope this will help in some small contribution that I leave if you have any questions please leave it in the comments of this post we read in the next.
0 comments:
Post a Comment