SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    May 2013
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile PHP Coding issue to display whole table from database

    Hi Mentor,

    I am a "newbee" :-)
    I have made a site " bubble under "by reading the book "Built your own Website"
    Now I am updating the site by adding more features and modules by making use of MYSQL and PHP. Like now I have created a database and want to display a table coming from database.
    I am referring the sitepoint Book "php__mysql_novice_to_ninja_5th_edition". Its a great book help me in learing PHP and MYSQL in website development.

    I have a problem in building script
    As I want to display all the columns of the club_event table which I made in Localhost server XAmpp and wrote the code as under to retrieve all columns
    while ($row = $result->fetch())
    {


    $events[] = array('Date' => $row['Date'], 'Description' => $row['Event_Description']);
    }


    include 'events.html.php';
    ?>

    but I am unable to display all the columns in my website.
    by below code:

    <?php echo htmlspecialchars($club_event , ENT_QUOTES, 'UTF-8'); ?>

    If I want to display single column it is working but for all columns to be displayed it is giving an error:
    Warning: htmlspecialchars() expects parameter 1 to be string, array given in C:\xampp\htdocs\bubble\events.html.php on line 12

    Kindly help me out resolving this issue.
    Thanks
    Abha

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    apply the transformation as you're creating your array.
    $events[] = array('Date' => $row['Date'], 'Description' => $row['Event_Description']);

    =>
    $events[] = array('Date' => $row['Date'], 'Description' => htmlspecialchars($row['Event_Description'] , ENT_QUOTES, 'UTF-8'));

    (Date should already be in a sanitized format that doesnt require htmlspecialchars)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Member
    Join Date
    May 2013
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Hello Mentor,

    Thanks for the quick reply :-).
    I tried the code of transformation simultaneously while creating an array as u said as under
    $events[] = array('Date' => $row['Date'], 'Description' => htmlspecialchars($row['Event_Description'] , ENT_QUOTES, 'UTF-8'),
    'Cost' => htmlspecialchars($row['Approximate_Cost'], ENT_QUOTES, 'UTF-8'));

    but it's still giving the error.
    and the error it is showing in the line
    <?php echo htmlspecialchars($club_event, ENT_QUOTES, 'UTF-8'); ?> // This line is on no. 13 and used to display all the elements of club_event table which is stored in another file and included in the main file.

    The error message is as displayed in my website is:
    Warning: htmlspecialchars() expects parameter 1 to be string, array given in C:\xampp\htdocs\bubble\events.html.php on line 13

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Why are you trying to echo an array, for starters, and second why are you doing the htmlspecialchars twice?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Member
    Join Date
    May 2013
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello again Mentor,

    I am displaying table "$club_event" <?php echo htmlspecialchars($club_event, ENT_QUOTES, 'UTF-8'); ?> as it is not an array. Also I displayed htmlspecialchars once only during creating array. But i m not able to built an error free script.

    where I m doing mistake I m not getting that.Kindly help me solving this issue.

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    well $club_event clearly is an array, or you wouldnt have been told you Warning: htmlspecialchars() expects parameter 1 to be string, array given in C:\xampp\htdocs\bubble\events.html.php

    So... where is $club_event being created?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Member
    Join Date
    May 2013
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Mentor,

    club_event is the table created in MYSQL Database.which i m echoing .
    Then y the issue

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    $club_event is a PHP variable. At some point, somewhere in your code, you have defined $club_event = <something here>. What is that piece of code?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  9. #9
    SitePoint Member
    Join Date
    May 2013
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Mentor,

    <?php foreach ($events as $club_event): ?>

    <?php echo htmlspecialchars($club_event, ENT_QUOTES, 'UTF-8'); ?>

    $sql = 'SELECT * FROM club_event';

    These are the above lines where I have used club_event.
    I have not defined anywhere $club_event=<something>

    Thanks

  10. #10
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Sure you have. Your foreach defines $club_event every time it loops.

    $club_event is an array.
    $events[] = array('Date' => $row['Date'], 'Description' => htmlspecialchars($row['Event_Description'] , ENT_QUOTES, 'UTF-8'),
    'Cost' => htmlspecialchars($row['Approximate_Cost'], ENT_QUOTES, 'UTF-8'));
    $club_event is an element of the $events array, which is itself an array containing three key-value pairs: 'Date','Description', and 'Cost'. Use those keys to access the values of $club_event.

    ($club_event['Date'] , for example)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  11. #11
    SitePoint Member
    Join Date
    May 2013
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Mentor :-),
    as I step forward in my coding...... but there is one issue .i.e. <?php echo htmlspecialchars( $club_event['Date'] ENT_QUOTES, 'UTF-8'); ?>. This piece of code is used to display individual column but wat should I do to display whole table .
    If I am using these lines to display all columns as :

    <?php echo htmlspecialchars( $club_event['Date'] ENT_QUOTES, 'UTF-8'); ?>
    <?php echo htmlspecialchars( $club_event['Event_Description'] ENT_QUOTES, 'UTF-8'); ?>
    <?php echo htmlspecialchars( $club_event['Approximate_Cost'] ENT_QUOTES, 'UTF-8'); ?>


    It is giving an error:

    Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\bubble\events.html.php on line 13

    How do I display whole table means all columns

    Thanks

  12. #12
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    #1: You've erased the comma before ENT_QUOTES. Look at your post in #9, and then at it in #11.
    #2: There will not be an entry in the array for 'Approximate_Cost', you renamed that to just 'Cost' in post #3.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  13. #13
    SitePoint Member
    Join Date
    May 2013
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so so much Mentor.....as u kept patience and guided me properly ,u let me know my mistakes and treated so well. Thanks as now my code is working properly.
    Now I have to apply CSS styling and build the database in tabular format.
    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •