Hi Guys!

I want to know how to limit the no of records to be shown in dropdown list from mysql.Below is my code. I just want to list 4 records from colum name 'department_name'. The colum has 11 records in total.

<select class="ac" name="area" >
<?php
$qry = mysql_query("SELECT department_id, department_name FROM departments");
while($db = mysql_fetch_array($qry)){
?>
<option value="<?=$db['department_id']?>">
<?=$db['department_name']?>
</option>
<?php
}
?>
</select>

Plus want to know how to pull out the already stored vlaue for yes/no dropdown list and checkbox. I want to know php syntax. I can insert the values into database from my form but for edit page, I want the already stored value to be shown to users who intend to modify a user details.

Thanks

Dani AI

Generated

Quick summary and practical fixes based on the thread: @Member334542 was right that you use LIMIT to restrict rows and @Member120589 correctly reminded that the offset is zero‑based. For predictable dropdown contents always combine an ORDER BY with your LIMIT so you get the same four rows each time. Avoid the old mysql_* functions — use PDO or mysqli and prepared statements for safety and future compatibility (see Choosing a MySQL API).

Example: fetch a small set safely (PDO) and output options, marking the stored department as selected. Note the LIMIT value is bound as an integer.

$limit = 4;
$stmt = $pdo->prepare("SELECT id, name FROM departments ORDER BY name LIMIT :lim");
$stmt->bindValue(':lim', (int)$limit, PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($rows as $r) {
    $sel = ($r['id'] == $user['department_id']) ? ' selected' : '';
    echo '<option value="'.htmlspecialchars($r['id']).'"'.$sel.'>'.htmlspecialchars($r['name']).'</option>';
}

To show an already stored yes/no or checkbox value on an edit page, compare the saved value and add selected or checked. Example patterns:

// yes/no select
$current = $user['active'] ?? 'no';
echo '<option value="yes"'.($current === 'yes' ? ' selected' : '').'>Yes</option>';

// single checkbox
echo '<input type="checkbox" name="flag" value="1"'.(!empty($user['flag']) ? ' checked' : '').'>';

For multiple checkboxes stored as CSV, use explode() and in_array() when rendering. Always escape output with htmlspecialchars() to prevent XSS (see htmlspecialchars). For LIMIT semantics and offsets see MySQL docs: SELECT syntax.

Troubleshooting: if nothing appears on the edit form, verify the user record is loaded before rendering, ensure types match when comparing IDs (cast to int if needed), and confirm the SQL actually returns rows (ORDER BY + LIMIT can hide expected rows if order is different).

Recommended Answers

All 4 Replies

Member Avatar for Member #334542

Query using Limit:

SELECT department_id, department_name FROM departments LIMIT 4

also like this

SELECT department_id, department_name FROM departments LIMIT 4,0
Member Avatar for Member #120589

also like this

SELECT department_id, department_name FROM departments LIMIT 4,0

I'm not sure what this is trying to achieve: return 0 rows, starting at the 4th offset (5th record). Stick with the first example. If you want a simple limit on the number of records returned (first 4 records for example) - just use the one value for the LIMIT keyword. However if you want to return 4 records, but starting at the third record:

SELECT department_id, department_name FROM departments LIMIT 2,4

Note '2' not '3' - this is because records start at Record #0.

Thanks Guys... I've got the basic idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.