piblondin Posted February 4, 2010 Share Posted February 4, 2010 Hi everyone-- I'm brand new to this stuff and have what I think is a very simple question. I have a MySQL Database with a few different fields related to stories we want to put on our website. Examples are title, story_id, url, image_url, etc.... Story_id is unique for each row in the database. What I want to do is be able to insert specific values from the database into my HTML. For example, I would like to have something like echo $headline[5] display the headline for the row with story_id=5 (or simply row 5)...it doesn't matter. It doesn't get much simpler, so I'm hoping someone can help--thanks much! Quote Link to comment https://forums.phpfreaks.com/topic/190963-very-basic-beginners-question-php-variables-for-all-values-in-mysql-table/ Share on other sites More sharing options...
KevinM1 Posted February 4, 2010 Share Posted February 4, 2010 Hi everyone-- I'm brand new to this stuff and have what I think is a very simple question. I have a MySQL Database with a few different fields related to stories we want to put on our website. Examples are title, story_id, url, image_url, etc.... Story_id is unique for each row in the database. What I want to do is be able to insert specific values from the database into my HTML. For example, I would like to have something like echo $headline[5] display the headline for the row with story_id=5 (or simply row 5)...it doesn't matter. It doesn't get much simpler, so I'm hoping someone can help--thanks much! What you need is a function, something along the lines of: function headline($storyId) { $query = "SELECT headline FROM tablename WHERE story_id = $storyId"; $result = mysql_query($query); if ($result) { $row = mysql_fetch_assoc($result); echo $row['headline']; } else { echo "Could not process request."; } } headline(5); Should give you an idea on what to do. Quote Link to comment https://forums.phpfreaks.com/topic/190963-very-basic-beginners-question-php-variables-for-all-values-in-mysql-table/#findComment-1007022 Share on other sites More sharing options...
Ty44ler Posted February 4, 2010 Share Posted February 4, 2010 You'll need to connect to your database in your php file first so it would be something like this: <?php $con = mysql_connect('localhost','USERNAME','PASSWORD'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db('DATABASENAME'); ?> Then you'll have to select what you want from your database: $result = mysql_query("SELECT * FROM [i]TABLENAME[/i] WHERE story_id = '5'"); while ($row = mysql_fetch_array($result)) { $headline=$row["headline"]; echo $headline; That queries your database for the story_id of 5 and returns the headline for it. Quote Link to comment https://forums.phpfreaks.com/topic/190963-very-basic-beginners-question-php-variables-for-all-values-in-mysql-table/#findComment-1007023 Share on other sites More sharing options...
Ty44ler Posted February 4, 2010 Share Posted February 4, 2010 Nightslyr's function will work well if you want to repeatedly use a different story_id which is what I think you're referring to. I misinterpreted as mine will return iterate through the database to return all field names so you can set them into variables. Quote Link to comment https://forums.phpfreaks.com/topic/190963-very-basic-beginners-question-php-variables-for-all-values-in-mysql-table/#findComment-1007029 Share on other sites More sharing options...
piblondin Posted February 4, 2010 Author Share Posted February 4, 2010 Excellent--thanks guys. Nightslyr's code is close to what I want. Now, do I need to repeat it for each of the different functions (and associated db fields)? Tyler, yes, I'm looking to be able to do something like headline(1), headline(4), link(3), etc. Quote Link to comment https://forums.phpfreaks.com/topic/190963-very-basic-beginners-question-php-variables-for-all-values-in-mysql-table/#findComment-1007040 Share on other sites More sharing options...
Ty44ler Posted February 4, 2010 Share Posted February 4, 2010 Excellent--thanks guys. Nightslyr's code is close to what I want. Now, do I need to repeat it for each of the different functions (and associated db fields)? Tyler, yes, I'm looking to be able to do something like headline(1), headline(4), link(3), etc. yep just put whichever one you want into the function you made and it will echo the result. Quote Link to comment https://forums.phpfreaks.com/topic/190963-very-basic-beginners-question-php-variables-for-all-values-in-mysql-table/#findComment-1007045 Share on other sites More sharing options...
piblondin Posted February 4, 2010 Author Share Posted February 4, 2010 Yup, got that Ty44ler. What I was curious about was when I'm defining the functions, do I need to repeat all of that code below for each one or is there an easier way to run through all the fields of the database and create associated functions? function headline($storyId) { $query = "SELECT headline FROM tablename WHERE story_id = $storyId"; $result = mysql_query($query); if ($result) { $row = mysql_fetch_assoc($result); echo $row['headline']; } else { echo "Could not process request."; } } Thanks again so much for your help. This is just what I was looking for! Quote Link to comment https://forums.phpfreaks.com/topic/190963-very-basic-beginners-question-php-variables-for-all-values-in-mysql-table/#findComment-1007054 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.