The first small project I created to start my site was to be successful in connecting to the database and then displaying the information in php
Step 1- Create a variable the will hold the $_GET variable that will come in from the url.
$pdisplayed = $_GET[‘pid’];
Step 2 - Connect to your database:
$db_connection = mysql_connect('localhost', 'root', '');
if (!$db_connection) {
die('Opps Problem ' . mysql_error());
}
Step 3 – Create the query – you can create the query and store them in variables.
$sqlquery = ‘SELECT id, name, description FROM mysite.product where id = $pdisplayed';
Step 4 - Run the query through the mysql_query() function.
$result = mysql_query($sqlquery);
Step 5 – Now choose with fetch function you will use.
while ($row = mysql_fetch_assoc($result)) {
printf("ID: %s Name: %s", $row[id], $row[name]);
}
or
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}
or
$row = mysql_fetch_row($result);
The best one to use is the mysql_fetch_assoc
And that should now show you data from the database from the url of the page.
EXAMPLE: www.mysite.com/index.php?pid=2
That 2 would be element two in the databalse.
No comments:
Post a Comment