ivatanako Posted September 30, 2007 Share Posted September 30, 2007 I am new into PHP. How can I fetch data from mySql and get something like this is in the url.. mysite.com/viewarticle.php?id=xxxx Fetching a table name article, with rows (id and topic) how is it done? I really have no idea where to start.. Quote Link to comment https://forums.phpfreaks.com/topic/71249-solved-fetching-mysql-data/ Share on other sites More sharing options...
LemonInflux Posted September 30, 2007 Share Posted September 30, 2007 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* Quote Link to comment https://forums.phpfreaks.com/topic/71249-solved-fetching-mysql-data/#findComment-358360 Share on other sites More sharing options...
LemonInflux Posted September 30, 2007 Share Posted September 30, 2007 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."); ?> Quote Link to comment https://forums.phpfreaks.com/topic/71249-solved-fetching-mysql-data/#findComment-358361 Share on other sites More sharing options...
ivatanako Posted September 30, 2007 Author Share Posted September 30, 2007 Thank you! ;D Quote Link to comment https://forums.phpfreaks.com/topic/71249-solved-fetching-mysql-data/#findComment-358363 Share on other sites More sharing options...
LemonInflux Posted September 30, 2007 Share Posted September 30, 2007 Click 'Topic Solved', please. And YW Quote Link to comment https://forums.phpfreaks.com/topic/71249-solved-fetching-mysql-data/#findComment-358368 Share on other sites More sharing options...
ivatanako Posted September 30, 2007 Author Share Posted September 30, 2007 Though I have another question, why did you used this variable $id it should be $article, innit? ??? ??? Quote Link to comment https://forums.phpfreaks.com/topic/71249-solved-fetching-mysql-data/#findComment-358373 Share on other sites More sharing options...
Ninjakreborn Posted September 30, 2007 Share Posted September 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71249-solved-fetching-mysql-data/#findComment-358375 Share on other sites More sharing options...
LemonInflux Posted September 30, 2007 Share Posted September 30, 2007 The variable could have been $omg_variable_l0l0l and it would have worked. It's just a name. As long as $_GET['id']; is in tact, it'll work. Quote Link to comment https://forums.phpfreaks.com/topic/71249-solved-fetching-mysql-data/#findComment-358379 Share on other sites More sharing options...
ivatanako Posted September 30, 2007 Author Share Posted September 30, 2007 Thank you again... :D Quote Link to comment https://forums.phpfreaks.com/topic/71249-solved-fetching-mysql-data/#findComment-358383 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.