I need help understanding how to pull data from a database

(The database i have) & insert data into fields on my webpages. So I can edit the site from a mysql database.
As far as I think understand, I have commented the code below...
Q-0= Please help explain what I have left un-defined with comment of "???". (also posted in its own code section at the bottom)
Please break down those 3 lines for me...
Please also correct where I am wrong... I know I have errors in my code, I always do :(
Q-1= Why the echo's? What do they do for me here?
Q-2= How do I seperate the database connection in a different file?

Main Code:
// this begins the php section
<?

//this connects to the database or dies with error
mysql_connect('localhost', 'user', 'pass',  'database') or die(mysql_error());

//this defines the database query to selecting everything from table blah, prior to filtering.
$query = mysql_query('SELECT * FROM blah');

//this opens a css div-id & div-class @ same time
echo '<div id="column_2" class="columnB-area">';

//this defines, stuff, as the variable to be used below, to define what to filter from query above.
while($stuff = mysql_fetch_array($query)) {

//this ???
      echo '<h2>' . $stuff['heading] . '</h2>';

//this ???
      echo '<img src="' . $stuff['image'] . '" alt="' . $stuff['title'] . '" />';

//this ???
      echo '<p>' . $stuff['info'] . '</p>';

//this ends the php section
}

//this closes the open css div-id & div-class @ same time
echo '</div>';

The following code... I need broken down and explained, as to what each part is on each line. Im sure their pretty much the same, and there is a syntax being followed. For me, it kinda looks like Chinese right now though.

Snippet of Code:
//this defines, stuff, as the variable to be used below, to define what to filter from query above.
while($stuff = mysql_fetch_array($query)) {

//this ???
      echo '<h2>' . $stuff['heading] . '</h2>';

//this ???
      echo '<img src="' . $stuff['image'] . '" alt="' . $stuff['title'] . '" />';

//this ???
      echo '<p>' . $stuff['info'] . '</p>';  

Thanks in Advance ;)

Recommended Answers

All 7 Replies

It's a loop through the array. You're defining the array name as $stuff and then you're echoing values from the array with $stuff['array_key']. So $stuff['title'] is pulling the value from the array with the key "title".

Also, you have a syntax error here

echo '<h2>' . $stuff['heading] . '</h2>';

You're missing a single quote

echo '<h2>' . $stuff['heading'] . '</h2>';

** Please correct me cause i am sure there is errors in this... **
PHP Code:

//This is what I understand, probably incorrectly...

//this is defining the array name as $stuff
while($stuff = mysql_fetch_array($query)) 

//this is the beggining of the array
{

//this tells the system to display on screen
      echo 

//this opens the header-2 css tag
'<h2>'

//this combines more than 1 thing's, as 1 group-thing
 . 

//this is the array name
$stuff

//this USES the data from the database already defined as heading (wherever that needs to be & whatever that is and can change via phpmyadin... 
//this is a question on hold for later)
['heading']

//this combines more than 1 things, as 1 group-thing
 . 

//this closes the open header-2 css tag
'</h2>'

//this is the end of the array
;

**Thanks in Advance  ;)**
echo '<h2>' . $stuff['heading'] . '</h2>';

Can someone PLEASE help me by explaining what each of the components in this line of code are?

I have posted the comments on this question, that I believe to be accurate.
I posted these comments in between each component...
But i am trying to fully understand this...

Well echo is the syntax to display a text in PHP.

Everything between '' or "" is a string.
You are concatenating two or more strings with (dot) .

$stuf is the array of the resultset from the Database.
$stuff['heading'] is the value from the dabase of the heading column, based on you selection.
and <h2></h2> it's a HTML tag for Headers.

    //This is what I understand, probably incorrectly...
    //this is defining the array name as $stuff

    while($stuff = mysql_fetch_array($query))

    //this is the beggining of the array
    {

    //this tells the system to display on screen
    echo

    //this opens the header-2 html/css tag
    '<h2>'

    //this is concatenating two or more strings
    .

    //this is the array of the result set from the Database
    $stuff

    //this is the value from the database of the heading column, based on you selection.
    ['heading']

    //this is concatenating two or more strings
    .

    //this closes the open header-2 /htmlcss tag
    '</h2>'

    //this is the end of the array
    ;

    **Thanks in Advance ;)**

assuming the above comments are now accurate.
Below is my current focus of study...

        //this is the value from the database of the heading column, based on you selection.
        ['heading']

can someone please explain a little more...
heading is the value i am selecting from my database? And the result from the database, will be displayed as my heading column on the web page?
So all i have to do is make sure that this 'heading' is corresponding to the correct location in the database & the correct data is stored in the expected location.

You've got most of it right, but one comment isn't complete yet.

 //this is defining the array name as $stuff
while($stuff = mysql_fetch_array($query))

Mysql_fetch_array is a function that returns 1 entry from the database (that was found with the query you just executed).
Every time you call that function, it returns a later entry. so thanks to this while, $stuff first holds the first resulting row, after that the second, then the third, etc.

The heading from the database is the name of a colom in the database. In this case, your database consists out of atleast 4 coloms: heading, title, image and info. so the value of $stuff['heading'] is actually the value of the colom 'heading' in your database.

If you ever get a PHP error 'undefined index' on an array like this, it most likely means you are trying to use a colom that doesnt exist. (this could be due to a type, try replacing ['heading'] with ['haeding'] for example).
If you ever get this error, try doing var_dump($stuff);

that is a lot of helpful information... Thank you for a good explanation!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.