{SOLVED} MySQL query and post

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rhodesia1
    New Member
    • Nov 2006
    • 5

    #1

    {SOLVED} MySQL query and post

    Hello guys
    I currently have established a connection to a MySQL database and succesfully display the text from the table into a field on a .php page using this
    [PHP]<?=$strContent? >[/PHP]
    and this as the db query;
    Code:
    $myConn = connect_db();
    	$strSQL = "SELECT * FROM tpages WHERE fPageName='[B]home[/B]'";
    	$result = mysql_query($strSQL);
    	$row = mysql_fetch_array($result);
    	$strContent = $row['fContent'];
    Currently I have these in the FPageName table in the MySQL database ;
    home
    about
    referral
    events
    faq

    The text in the field fContent for home displays nicely however I also would like to display a second pages fContent value which is in the same table (tPageName) but its in the field called referral

    I have tried various connection strings but cant figure it out. I get one or the other to display but not both. Can anyone help me. I am not that good obviously with MySQL database queries and displaying data.

    Thanks in advance!
  • rhodesia1
    New Member
    • Nov 2006
    • 5

    #2
    is this the right forum?

    Comment

    • Nert
      New Member
      • Nov 2006
      • 64

      #3
      hello,

      i would like to help you with your problem, but i'm a bit confused with what you want achieve in your project if you don't mind can you explain further about want you aim for? i understand a little bit about mysql query maybe i can help you

      ..nert

      Comment

      • rhodesia1
        New Member
        • Nov 2006
        • 5

        #4
        Originally posted by Nert
        hello,
        but i'm a bit confused with what you want achieve in your project if you don't mind can you explain further about want you aim for?
        ..nert
        Hello nert, thanks for the reply. Yes, i can try to explain a little more.
        I have a MySQL database and can display the data in one of the tables fields (the fields in this table are id, fpageName and fContent)
        The fpageName stores the page name (example home page) and the corresponding field in that table (fpageContent) stores the text that was entered into the database field using an admin area for that page.

        I have stored data for several pages in the database table,
        So FpageName has these fields currently
        Code:
        -----[U]ID[/U]---------[U]fpageName[/U]------------[U]fpageContent[/U]------
             1       home             this is the home stored text
             2       about             this is the about stored text
             3       referral          this is the referral stored text
        etc.....

        Now, on the home page for example: I wrote this to connect and retrieve the data from the fpageContent field in the home row.
        [PHP]$myConn = connect_db();
        $strSQL = "SELECT * FROM tpages WHERE fPageName='home '";
        $result = mysql_query($st rSQL);
        $row = mysql_fetch_arr ay($result);
        $strContent = $row['fContent'];[/PHP]

        In the area on the home page I want to display the content stored in the database row ( fpageContent) I am using [php]<? $strContent ?>[/php] and this outputs the data stored in that rows field into the webpage.

        Using this I can connect to the data table's row for the home page content, but I would also like to display the content of another row in a different area on the home page (the referral row's fpageContent stored data). I don't know how to modify the connection string above so that it will allow me to connect and retrieve data from two database rows in the same table.

        I hope that is easier to understand than my original post, in essence I want to be able to output the data in the rows home and referral on the same page and don't know how.

        thanks

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          Possibly I totally misunderstand what you mean, but in the following example I assumed that you want to read 2 rows from the same table, with different content, and display the data of these 2 rows separately at your screen.

          [php]
          // init in case you miss a filed from the db
          $home = "No data available";
          $refer = "No data available";

          // select both rows and display msg when error occurs
          $strSQL = "SELECT * FROM tpages WHERE fPageName='home '
          OR fPageName='refe rral'";
          $result = mysql_query($st rSQL)
          or die("SELECT error: " . mysql_error());

          // read both rows
          while ($row = mysql_fetch_arr ay($result)) {
          if ($row['fContent'] == 'home')
          $home = $row['fContent']; // assign the 'home' string
          else
          $refer = $row['fContent']; // assign the 'referral' string
          }
          // display the value of home here
          echo $home;
          // echo the referral at another place
          echo $refer;
          [/php]
          Ronald :cool:

          Comment

          • rhodesia1
            New Member
            • Nov 2006
            • 5

            #6
            Hello Ronald, thanks for your reply. You have hit the nail on the head, thats exactly what I want to do.

            I tried your post and it either displays the error of "no data available" or if I comment out that error line it displays nothing at all with the echo in the areas on the page I want that particular text to show.

            Here is the complete connection I used;
            [php]session_start() ; //
            include_once("i nclude/mysql.inc.php") ; //database connection string
            include_once("i nclude/functions.inc.p hp"); //
            $myConn = connect_db();
            // init in case you miss a filed from the db

            //$home = "No data available";

            //$refer = "No data available";

            // select both rows and display msg when error occurs

            $strSQL = "SELECT * FROM tpages WHERE fPageName='home ' OR fPageName='refe rral'";

            $result = mysql_query($st rSQL)

            or die("SELECT error: " . mysql_error());

            // read both rows

            while ($row = mysql_fetch_arr ay($result)) {

            if ($row['fContent'] == 'home')

            $home = $row['fContent']; // assign the 'home' string

            else

            $refer = $row['fContent']; // assign the 'referral' string

            }[/php]


            If i just do a basic page with what you put on it, then the home content will display but no referral content. ( if I comment out the home and just use referral then the home content still displays.)

            If I try to echo later in the page, nothing shows at all. ie outside the code you have created on the page. If I uncomment the errors message,then they show in both areas on the page.

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              Stupid of me, I made a typo. The compare statement should be [php]if ($row['fPageName'] == 'home') {[/php]
              Ronald :cool:

              Comment

              • rhodesia1
                New Member
                • Nov 2006
                • 5

                #8
                Originally posted by ronverdonk
                Stupid of me, I made a typo. The compare statement should be [php]if ($row['fPageName'] == 'home') {[/php]
                Ronald :cool:
                Not stupid at all. That change made it work perfectly. Thanks a million, you have made me very happy and now I can post what I wanted on the webpages.

                Cheers!

                Comment

                Working...