Jump to content

[SOLVED] Fetching mySql data


ivatanako

Recommended Posts

do it the other way around. If you want people to see, say, the 1st article you wrote, link them to mysite.com/viewarticle.php?id=1. On viewarticle.php, put at the top:

 

<?php
$article = $_GET['id'];
?>

 

Now, PHP has the article number you want. Then, write:

 

<?php
// Get the article from the table. Change 'tablename' to your table's name.
$sql = 'SELECT * FROM `tablename` WHERE id = "'. $id .'"';
// Query the database
$sqlresult = mysql_query($sql);
while ($row = mysql_fetch_assoc($sqlresult)) {
// Write the code to format your page here.
echo '<b>'. $row['article'] .'</b><br><br>'
}
?>

 

The above code will output in the following:

 

This is article number 1. In here is some article information which I hope you will read, etc. etc.

 

*REST OF PAGE*

of course, you'll need to connect to the database first (if you don't know how to):

 

<?php
$dbHost = "yourhost";          //Your database host
$dbUser = "username";         //Username for DataBase
$dbPass = "password";         //Password
$dbDatabase = "database";   //Database Name in which you keep articles.

$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");
mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");
?>

Actually he also asked how to get them.

In order to generate url's of id's of items from a db it's

something like

<?php
$select * from users;";
$query = mysql_query($select);
while ($row = mysql_fetch_array($query) {
echo $row['firstname'] . '<a href="profileview.php?' . $row['id'] . '" title="user">User</a>';
}
?>

That's just a quick example.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.