
I will try to explain as simple as possible the steps to create a DB, then create a table to hold the data, and finally to connect to the DB to request the data and print that data in a web page. For a better understanding you should have basic knowledge of PHP.
Our DB is going to hold a list of contacts and the fields will be name and email address.
We are going to do the DB creation process through phpMyAdmin which is an open source tool written in PHP intended to handle the administration of MySQL over the World Wide Web. It can perform various tasks such as creating, modifying or deleting databases, tables, fields or rows; executing SQL statements; or managing users and permissions. If you have a hosting plan, you should be able to access phpMyAdmin through the cpanel. In case you don’t, check with your ISP.
Those that would like to have their own server, take a look at How to install AppServ
Next… Assuming you already got into phpMyAdmin panel, what you see is something like this:
In my case I have all set in English. But you can change the language choosing it from the language drop down menu.
To create the DB, we go to the textbox Create new database, we type a name, and then click create.
Tip: Try to choose a name that tells you something about the project so when you have a few more tables there, you can easily distinguish them.
In my example, I just named it test_1. So test_1 is our DB. Now we need to create a table to save the data. To do this, go to Create new table on database test_1 and give it a name.
We’ll call it contact and in “number of fields” we put 3. Then click go. Number of fields is the amount of cells our table will have. In our example, we have 3 cells. One for the ID to be automatically assigned to each new record, a second one to save contact name, and last one to save email account.
Tip: It is strongly recommended that you never use special characters like á-é-í-ó-ú, ( , ), ñ, [], etc, for the table or field names. I found these rules somewhere:
* No identifier can contain ASCII NUL (0×00) or a byte with a value of 255.
* Database, table, and column names should not end with space characters.
* Database and table names cannot contain “/”, “\”, “.”, or characters that are not allowed in file names.
Now that we have the table, we set the Field, Type and Length. I’m not going to explain the Type and Length various options since it’s beyond this tutorial. If you want to dig a little bit about it, please check MySQL datatypes. You can check the image below and do the same. And in the case of the ID, remember to select “auto_increment” in the extra field option, and choose “primary key” from the radio buttons.
Click Save to complete the table creation process. Now we are going to enter the data for name and email. To do that, click insert on top of menu options. When done, click go. We now have our first “record” done. If you would like to enter a new record, repeat the process.
So, we are ready to move on. To sum it up, we created a DB, we created a table with records and assigned data to those records. Now we just need to connect to the DB, do a query to request the data and show everything in a web page.
How we do that? With PHP. But first you need to confirm that you have PHP enabled in your hosting or wherever you are testing this. To do so, open your HTML editor, paste the code below, save it as .php in the root of your server.
<?php phpinfo(); ?>
Then open this same page in a browser. If PHP is enabled you will see all the information related to PHP, MySQL, etc. Once you confirm it, please don’t forget to delete the page. In case the page didn’t return any information (assuming you are accessing the page but nothing showed up) you will have to check with your hosting.
Assuming that PHP is enabled, create a new folder, a new php page and paste the code below. I’m going to explain it a little bit although it’s quite simple.
<?php
$db_host = "localhost";
$db_database = "test_1"; // DB Name
$db_username = ""; // Username to access MySQL server. Username and password should be provided by your hosting or if you have your own server, when you installed MySQL.
$db_password = ""; // Password
// Here we establish the connection with MySQL server
$db_connect = mysql_connect($db_host, $db_username, $db_password) or die (\'Error connecting to mysql\');
// We define which table we want to connect with
$db_select_db = mysql_select_db($db_database);
// Basically it means: Select everything from table contact order by name in descending order
$query="SELECT * FROM contact ORDER BY name DESC";
// Save the query resutls in $result var
$result = mysql_query($query) or die(\'Error : \' . mysql_error());
// Use a while to loop through all of the records we have in the DB.
// With mysql_fetch_assoc we are getting the data in associative array format instead of indexing format.
// This way we can access the result using the name of the field
while ($data=mysql_fetch_assoc($result)) {
// Finally, we create divs to hold and show the result
echo "<div style=\'float:left; width:300px;\'>".$data[\'name\']."</div>";
echo "<div style=\'float:left;\'>".$data[\'email\']."</div>";
echo "<div style=\'clear:both;\'></div>";
};
?>
If you followed the steps correctly, you should be able to see the contacts list. If not, check your DB to see if you entered the data. If you still have problems, send me an email and I’ll see what I can do to help you.
Suggestion: If you want to learn how to master a soft or script language, my suggestion is that you have to read…. then keep reading… and read even more. Don’t get used to copy / paste. It might solve the inmediate problem but you will never know what you are actually doing.
Good Luck!
Federico :-)

como seria el lenguaje para conectarse en un servidor de internet osea de un hosting


Hola, no puedo visulaizar la pagina, me tira el siguiente error
Warning: Unexpected character in input: ‘\’ (ASCII=92) state=1 in C:\xampp\htdocs\PRUEBA.PHP on line 32
Warning: Unexpected character in input: ”’ (ASCII=39) state=1 in C:\xampp\htdocs\PRUEBA.PHP on line 32
Warning: Unexpected character in input: ‘\’ (ASCII=92) state=1 in C:\xampp\htdocs\PRUEBA.PHP on line 32
Warning: Unexpected character in input: ”’ (ASCII=39) state=1 in C:\xampp\htdocs\PRUEBA.PHP on line 32
Warning: Unexpected character in input: ‘\’ (ASCII=92) state=1 in C:\xampp\htdocs\PRUEBA.PHP on line 33
Warning: Unexpected character in input: ”’ (ASCII=39) state=1 in C:\xampp\htdocs\PRUEBA.PHP on line 33
Warning: Unexpected character in input: ‘\’ (ASCII=92) state=1 in C:\xampp\htdocs\PRUEBA.PHP on line 33
Warning: Unexpected character in input: ”’ (ASCII=39) state=1 in C:\xampp\htdocs\PRUEBA.PHP on line 33
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\PRUEBA.PHP on line 30


Fernando: Tendría que ver el código para poder ayudarte mejor. Pero pareciera que estas usando de manera incorrecta caracteres que en php tienen un significado especial. Los caracteres son \ y "".
Por ejemplo:
$output = 'I'm a good boy'; Esto me daría error ya que la comilla simple entre I y m no esta escapada.
La manera correcta sería:
$output = 'I\'m a good boy'; o $output = "I'm a good boy";
Enviame prueba.php por mail, lo miro y te digo que es lo que esta dando error.
Saludos, Federico Naumow Waldoke
