Ask Your Question
3

Can you generate a PHP/MySQL table that records and tallies multiple variables?

asked 2023-05-14 23:35:41 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-05-14 23:37:02 +0000

scrum gravatar image

Yes, it is possible to generate a PHP/MySQL table that records and tallies multiple variables.

Here is an example of a PHP/MySQL table that records and tallies the number of votes for multiple candidates:

CREATE TABLE election_results ( id int(11) NOT NULL AUTO_INCREMENT, candidate_name varchar(255) NOT NULL, candidate_party varchar(255) NOT NULL, num_votes int(11) NOT NULL DEFAULT '0', PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

You can then insert data into the table using SQL queries:

INSERT INTO election_results (candidate_name, candidate_party, num_votes) VALUES ('John Smith', 'Democratic Party', 10), ('Jane Doe', 'Republican Party', 8), ('Bob Jones', 'Green Party', 5);

You can then use PHP to retrieve and display the data from the table in a table format, along with any calculations you wish to perform on the data:

$query = "SELECT candidatename, candidateparty, numvotes FROM electionresults ORDER BY numvotes DESC"; $result = mysqliquery($connection, $query);

echo "

"; echo ""; while ($row = mysqlifetchassoc($result)) { echo ""; } echo "
Candidate NamePartyNumber of Votes
" . $row['candidatename'] . "" . $row['candidateparty'] . "" . $row['num_votes'] . "
";

You can also use SQL queries to perform calculations, such as calculating the total number of votes:

SELECT SUM(numvotes) AS totalvotes FROM election_results;

This will return a single row with the total number of votes.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-14 23:35:41 +0000

Seen: 12 times

Last updated: May 14 '23