Forum Moderators: coopster

Message Too Old, No Replies

Serious learning problems

Noob site developer needs help selecting with php

         

bobnew32

12:57 am on May 6, 2003 (gmt 0)

10+ Year Member



Ok, right now I have the knowledge to select everything from a table that has a certain quality, like selective everything where id=1. Thats fine and good, but I'm not sure how to put it in rows.

I know it all involves somthing with mysql_query, and mysql_fetch_array, but I cannot understand the concept at all. I can understand how to select certain pieces of data, but no way in hell to reference it or call it to echo. I hope I'm making sense, but do you know how to reference a row when it could be different every time?

For the use of a scenario, I am calling three rows of data from a database, grouping it by the id number. I want to put the data in different places on an html page, (its for news, the title, author, news_text would all look alike, but the data would come after one another in kind of a template, and would repeat itself, but all on the same page.

Please help me understand this concept and how to group and call different rows of information, i'm at a standstill here and my mind just isn't getting the "logic" of it. Thx for reading this and your help also.

grahamstewart

1:16 am on May 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds like your getting there.

The sequence of events usually goes like this..

  • Connect to database (
    mysql_connect
    and
    mysql_select_db
    )
  • Execute an sql query (
    mysql_query
    )
  • Enter a loop - retrieve the rows of results until there are none left (using
    while
    and
    mysql_fetch_assoc
    or
    mysql_fetch_array
    )

    This excellent webMonkey article on PHP/MySQL [hotwired.lycos.com] should get you started.

  • grahamstewart

    1:26 am on May 6, 2003 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Heres a bit of code that might help explain the structure...

    [pre]
    mysql_connect( 'whatever' );
    or die( "Error connecting: ".mysql_error());

    mysql_select_db('whatever');
    or die( "Error selecting database: ".mysql_error());

    $sql = 'select title, author, news_text from news where id = 1';

    $result = mysql_query($sql)
    or die( "Error selecting news: ".mysql_error());

    while ( $row = mysql_fetch_assoc($result) ) {
    print '<h1>'.$row['title'].'</h1>';
    print '<p>'.$row['news_text'].'</p>';
    print '<span class="byline">Written by '.$row['author'].'</span>';
    }
    [/pre]

    Hope that helps.

    bobnew32

    1:31 am on May 6, 2003 (gmt 0)

    10+ Year Member



    I do understand that structure, its the problem where multiple rows match the id of 1 and I want to echo some part of different rows that match the id of 1(not exactly the id of 1 maybe some other quality).

    As of now I understand how to select data from 3 different rows, but i'm only able to echo the data one after another. ~<Dangit!>~.

    bobnew32

    1:34 am on May 6, 2003 (gmt 0)

    10+ Year Member



    OMG I think I just had an insight. If I have 3 rows selected and want each row to do the same thing, I must somehow create an array that grabs one and where to put the stuff as a template or somthing, and then it'll keep grabbing the rows and placing the information where it needs to be. Do I sound correct? BTW, i'm referring to news stuff.

    gethan

    1:51 am on May 6, 2003 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    $sql = "select * from table where id=1";

    $result = mysql_query($sql);

    $array = mysql_fetch_array($result);

    print "$array[colname]";

    or use print_r($array) to get the full list...

    HTH

    gethan

    1:55 am on May 6, 2003 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Sorry too slow with the last post....

    Try this..

    while ($array = mysql_fetch_array($result)) {
    // do stuff.
    }

    Will iterate through the entire result set.

    grahamstewart

    1:56 am on May 6, 2003 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Not sure I understand your problem. Perhaps you could give me an example?

    That code will display all the results. If 10 rows have the id of 1 then it will display a page of 10 results, one after the other.

    (By the way, 'id' is a name that usually given to a field that uniquely identifies one and only one row in the table, so you might want to rename it).

    bobnew32

    2:28 am on May 6, 2003 (gmt 0)

    10+ Year Member



    (By the way, 'id' is a name that usually given to a field that uniquely identifies one and only one row in the table, so you might want to rename it).

    Yeah I know that, I was trying to do an example, but heres my real example lol.

    I have a database called television_news which has various amounts of data about television news such as news_id (primarykey), title, newstext, and posted user.

    I have a design of how I want the information for that to show up but since this is the front page of my site, I only want to display the newest 3 news articles entered (being the 3 highest news_ids are the 3 most recent).

    If I design how I want one to look, is there a way to make the other three go into that way also? Picture it as *title* at the top, under it is *posted_user*, and under that is *newstext*. It would pull each row of information that matches the criteria (newest 3 ids) and put the information for EACH row into that template displaying each after another (just like a post).

    I hope I said that sufficiently and thx for helping, its so confusing.

    willybfriendly

    2:50 am on May 6, 2003 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    You can append "ORDER BY id DESC" to your sql query. Then do a loop for three iterations.

    $sql = 'select title, author, news_text from news where id = 1 order by id desc';

    for(i = 0; i< 3; i++)
    {
    echo "...."
    }

    Does that do what you want?

    WBF

    jatar_k

    2:53 am on May 6, 2003 (gmt 0)

    WebmasterWorld Administrator 10+ Year Member



    or just do ORDER BY id DESC limit 3

    bobnew32

    3:14 am on May 6, 2003 (gmt 0)

    10+ Year Member



    Willy, I'm not sure if your still thinking of the previous posts, but I dont wanna make the primary way of selecting them by selecting them through one id. But if i'm thinking the way I think you are, I can do

    $sql = 'select * from news ORDER BY id DESC limit 3
    ';
    That solves that which I already knew how to solve, but can you give me a sample of the

    for(i = 0; i< 3; i++)
    {
    echo "...."
    }

    situation, I'm not completely sure what it means.

    willybfriendly

    4:00 am on May 6, 2003 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Not sure how you want to display the data you have just retrieved. That said, and using jatr_k's rather obvious "limit 3", :

    while($row = mysql_fetch_array())
    {
    echo "<P><B>$row[title]</B> - $row[newstext]<BR> - $row[posted_user]</P>";
    }
    This will format it as paragraphs with a bold title. You could as easily use <H> tags or fit it in a table, depending on your needs.

    WBF

    grahamstewart

    4:24 am on May 6, 2003 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    can you give me a sample of the
    for(i = 0; i< 3; i++)

    See message 3 where I told you how to format it as a headline, story and byline. :)