PHP Coding issue to display whole table from database

Hi Mentor,

I am a “newbee” :slight_smile:
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

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)

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

Why are you trying to echo an array, for starters, and second why are you doing the htmlspecialchars twice?

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.

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?

Hello Mentor,

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

$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?

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

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)

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

#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.

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