/    Sign up×
Community /Pin to ProfileBookmark

How to view count of Mysql table rows on HTML page?

Hello! I have an HTML page and MySQL table for my idea. Can you help me to realise it using button

[code=html]<input type=”submit” name=”count” value=”count”></input>[/code]

and dropdown list

[code=html]<select>
<option>a</option>
<option>b</option>
<option>c</option>
<option>d</option>
<option>e</option>
</select>[/code]

?
I want by clicking on button see how many rows I have in MySQL table with option that i choose.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@holyhttpFeb 08.2016 — Give a name to your drop-down list:

<select name="selection">

<option>a</option>

<option>b</option>

<option>c</option>

<option>d</option>

<option>e</option>

</select>



I assume you are using PHP in your back-end.

If not, adapt the following to your server-side language.

$val=addslashes(trim($_POST['selection']; //capture the value of the selection

$sql="SELECT count(id) FROM tablename where id='$val'";


//replace id by the name of your database table primary key name.

//replace tablename by the actual name of the database table where

//your SQL will be performed against
Copy linkTweet thisAlerts:
@ShmeliakaauthorFeb 10.2016 — Thank you. But I don't know where I have to put your code: in html document or in the new php document. Tell me please.
Copy linkTweet thisAlerts:
@jedaisoulFeb 10.2016 — Hi, I have moved this thread to the PHP forum as it relates to the use of PHP code.

Since you want to display the results of the PHP code in the HTML page it, probably, would be most convenient to embed the code in the HTML page. If so, use [b]<?php[/b] (at the start) and [b]?>[/b] (at the end of the code). However, you would also (generally) need to rename the page file as .PHP instead of .HTML (as that is the simplest way of indicating to the server that the file contains PHP code).
Copy linkTweet thisAlerts:
@SelfCoderFeb 11.2016 — Write your and over your example in the php file:

<i>
</i>&lt;input type="submit" name="count" value="&lt;?=$sql?&gt;"&gt;&lt;/input&gt;


But "$sql" is for this not correctly, please rename "$sql" as "$result"!
Copy linkTweet thisAlerts:
@NogDogFeb 11.2016 — At its simplest, with no validation at all, it might go something like...
[code=php]
<html>
<head><title>Example</title></head>
<body>
<form action="" method="get">
<p>
<select name="letter">
<option>a</option>
<option>b</option>
<option>c</option>
<option>d</option>
<option>e</option>
</select>
<input type="submit" name="count" value="count" />
</p>
</form>
<?php
if(!empty($_GET['letter'])) {
// you'll need to define these for the DBMS you are using:
$dbDns = 'something';
$dbUser = 'user_name';
$dbPass = 'the_password';

$pdo = new PDO($dbDns, $dbUser, $dbPass);
$stmt = $pdo->prepare('SELECT COUNT(*) AS num_matches FROM the_table WHERE the_column = :letter');
$stmt->execute(array(':letter' => $_GET['letter']));
echo "<p>There are ".$stmt->fetchColumn()." rows in the DB for '".htmlentities($_GET['letter'])."'.</p>";
}
?>
</body>
</html>
[/code]

(totally untested)
Copy linkTweet thisAlerts:
@rootFeb 12.2016 — What about using an HTML page that uses AJAX (JavaScript) to pass the selection to the server script to run the query and return the result back to the AJAX query and update a <div> element with the result.
Copy linkTweet thisAlerts:
@NogDogFeb 12.2016 — What about using an HTML page that uses AJAX (JavaScript) to pass the selection to the server script to run the query and return the result back to the AJAX query and update a <div> element with the result.[/QUOTE]

While valid and [I]possibly[/I]* better, that might be a layer of complexity beyond what I'd guess the OP is ready for.

___________
  • * There's still a curmudgeonly part of me that does not want to have to depend on the browser to do anything. ?
  • ×

    Success!

    Help @Shmeliaka spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.25,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...