Jump to content

MySQL PHP Recordsets


warstormer

Recommended Posts

As this is to do with MySQL and PHP, not sure where to post it... Basically it's about manipulating recordsets, and I'm coming from an ASP background so bear with me.

 

I have this: $result = mysql_query("select * from news where news_alert = 'A' LIMIT 0, 30 ") or die(mysql_error());

 

Now I assume that $result is my recordset. How do I then display individual data? In ASP you would just write '=rs("field_name")' and there the data would appear...

 

I am actually trying to pull out the name of a relevant image and insert it into a line of html like so:

 

<img src="images/<?recordset data ?>" />

 

Any help much appreciated!

Link to comment
Share on other sites

To pull data from a recor set you'd use mysql_fetch_assoc or mysql_fetch_array. I use assoc as I prefer to work with associative arrays ($arr['key']]) rather than indices ($arr[0]).

 

If your query returns multiple rows then you'd use mysql_fetch_assoc within a while loop to loop through the rows that was returned. If your query only returns 1 row then you wont need to use a while loop.

 

Here's an example:

<?php

// connect/select database

$result = mysql_query("select * from news where news_alert = 'A' LIMIT 0, 30 ") or die(mysql_error());

// check that the query returned any results
if(mysql_num_rows($result) > 0)
{
    // loop through the results
    while($row = mysql_fetch_assoc($result))
    {
        echo '<img src="images/' . $row['field_name_here'] . '" />';
    }
}

 

change field_name_here with the actual field (or column) name from your table.

Link to comment
Share on other sites

Cool, thanks very helpful.... if I did just have 1 row in my recordset what's the syntax for displaying the data? Is it just...

 

{

  $row = mysql_fetch_assoc($result)

    {

        echo '<img src="images/' . $row['field_name_here'] . '" />';

    }

}

 

sorry if that's completely wrong!

Link to comment
Share on other sites

I completely disagree. Why have a while loop when you dont need one? When you come back to look at that, it would be very easy to get confused. You'll be expecting that code block to repeat and it doesn't.

 

Cool, thanks very helpful.... if I did just have 1 row in my recordset what's the syntax for displaying the data? Is it just...

 

{

  $row = mysql_fetch_assoc($result)

    {

        echo '<img src="images/' . $row['field_name_here'] . '" />';

    }

}

 

sorry if that's completely wrong!

 

Yeah, thats right. Although the braces are unnecessary.

 

Personally, if i'm retrieving just one field from a single row, i tend to use the mysql_result() function - seems to make your intentions clearer that way. However, thats just a matter of opinion.

Link to comment
Share on other sites

Thanks for that as well... appreciate all the advice....! Guess I'll see which one works best...

 

I completely disagree. Why have a while loop when you dont need one? When you come back to look at that, it would be very easy to get confused. You'll be expecting that code block to repeat and it doesn't.

 

Cool, thanks very helpful.... if I did just have 1 row in my recordset what's the syntax for displaying the data? Is it just...

 

{

  $row = mysql_fetch_assoc($result)

    {

        echo '<img src="images/' . $row['field_name_here'] . '" />';

    }

}

 

sorry if that's completely wrong!

 

Yeah, thats right. Although the braces are unnecessary.

 

Personally, if i'm retrieving just one field from a single row, i tend to use the mysql_result() function - seems to make your intentions clearer that way. However, thats just a matter of opinion.

Link to comment
Share on other sites

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.