Hi there,
Do you have any experience with PHP or mySQL? If not I suggest you read some tutorials, there are thousands of them on the web.
To accomplish what you want, we first must set up a database. In the control panel in your website, look for the place where you can create a database. Fill in the required information and write down the database name, username, and password so we can connect to it later. Then open up the database in phpmyadmin. Create a new table and call it "housingrent". In the "fields" category, let's start with 3. We can add more fields later.
For the first field, let's name it "id". For the type set it to "INT" (which is short for integer). Length/values set to 10. Under extra select "autoincrement" and check the "primary" box.
The next field let's name "info". For the type set it to "text". Leave the rest of that field blank (ie no length, extras, etc).
For the last field let's name it "zip". Set the type to "INT" and length to 5 (number of numbers in a zip code).
Then click save.
The database part is done
Now let's create the PHP part.
First things first, we need some basic things. Create a new document in NotePad and name it what ever you want, but make sure it has a ".php" extension. Now open up this file.
All php code has to go between <?php and ?> brackets. For example...
<?php
//your code here
?>
The first thing we want to do is connect to your database.
We can use this code, but make sure to replace the username, password, and hostname variables with the values you used when you created the database. I left the hostname as "localhost", as many web servers use it, but your host may use something else.
$username = "database username";
$password = "database password";
$dbname = "database name";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL 1");
$selected = mysql_select_db($dbname,$dbh) or die("Unable to connect to MySQL");
Next let's get the results from the database. This code will select the results from the housingrent table and the code between the brackets will be executed for each entry in this table.
$result=mysql_query("SELECT * from housingrent");
while ($object = mysql_fetch_array($result)) {
This creates an array and we can get each of the fields from the database. For example, to get the info field we would use $object[info] and to get the zip code field we would use $object[zip].
So for each entry let's print the zip code and info.
echo "$object[zip]<br>$object[info]";
Now let's put all this code together:
<?php
$username = "database username";
$password = "database password";
$dbname = "database name";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL 1");
$selected = mysql_select_db($dbname,$dbh) or die("Unable to connect to MySQL");
$result=mysql_query("SELECT * from housingrent");
while ($object = mysql_fetch_array($result)) {
echo "$object[zip]<br>$object[info]";
}
?>
This simply displays all the entries, once you have gotten this to work we can work on allowing users to add entries (for now you can just add entries in phpmyadmin with the insert button) and search for entries.
Let me know if there is anything I can clarify,
- Matt