Jump to content

Very Basic Beginner's Question--PHP variables for all values in MySQL table


Recommended Posts

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!

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.

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.

 

 

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.

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.

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.

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.