I have to retrevie data from the database using

"SELECT * FROM $tbl_name WHERE cat1 = technicalfest ORDER BY id DESC";

can any one tell me how to use multiple

Dani AI

Generated

Quick summary and immediate fixes

The immediate problems in the thread are (a) the string in the WHERE needs quotes (e.g. cat1 = 'technicalfest'), and (b) a loop variable mismatch will break output (the example uses while($rows=...) but then echoes $row[...]). already flagged the quoting issue, and ’s suggestion of mysqli_multi_query is unnecessary for a single SELECT — use UNION or IN to combine results, or run separate prepared statements if they must be different queries.

A safer, modern approach

Avoid the old mysql_* API (removed in recent PHP). Use PDO or mysqli with prepared statements so values are quoted and injection is prevented. Also never interpolate unvalidated table names — whitelist them. Example pattern:

<?php
// whitelist $tbl_name first
$stmt = $pdo->prepare("SELECT * FROM `$tbl_name` WHERE cat1 = :cat ORDER BY id DESC");
$stmt->execute([':cat' => $category]);
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
    echo '<li>' . htmlspecialchars($row['some_field']) . '</li>';
}

Troubleshooting and tips

  • Turn on DB errors while developing (PDO exceptions or mysqli_error) to see the real problem.
  • Use htmlspecialchars() on output to avoid XSS.
  • If $tbl_name is dynamic, validate it against a hardcoded array of allowed names.
  • ORDER BY id DESC is fine for “newest first”; add an index on id for performance if the table is large.
  • Use mysqli_multi_query only when you truly need to execute multiple SQL statements in one call — it complicates error handling and is rarely necessary for reading rows.

Checklist: quote string literals, use prepared statements, fix the $rows/$row typo, whitelist dynamic table names, and enable error reporting to see what’s failing.

Recommended Answers

All 6 Replies

Member Avatar for Member #120589

You can use mysqli to run multiple queries:

http://php.net/manual/en/mysqli.multi-query.php

However, you may find that your queries may be able to be combined into a single query (e.g. using UNION or IN). Supply your queries so that we can determine the best possible solution.

can u make it for me diafor .. i am trying but error occured

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name where cat1=technical ORDER BY id DESC";
// OREDER BY id DESC is order result by descending

$result=mysql_query($sql);
?>

<?php

// Start looping table row
while($rows=mysql_fetch_array($result)){
?>

 data present here with echos


<?php
// Exit looping and close connection 
}
mysql_close();
?>

can u write this code which selects and order the data .. please :)

Member Avatar for Member #120589

OK, since you've got the basics, I'll just add a bit to it. BTW 'multiple' confused me - I thought you wanted to run multiple queries. You just need to output the data.

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name where cat1='technical' ORDER BY id DESC";

$result=mysql_query($sql);
echo "<ul>";
while($rows=mysql_fetch_array($result)){
    echo "<li>{$row['field1']} | {$row['field2']} | {$row['field3']} | {$row['field1']}</li>";
}
echo "</ul>";
mysql_close();

Change field1, field2, field3, field4 to fieldnames that actually exist in your DB table.

Hi sabarinadh,

In your query you have not added single quote to your cat1 value.

Except integer you should use quote.

Eg: cat1 = 'technical'

Hi Nithin i tried the above code but still some errors if you dont mine can u please develope the code which was used to retreive data from users where cat1=technicalfest and order by id desc :)

Member Avatar for Member #120589

Ahem, I corrected the single quote thing in my last post. Anyway, did my code not work?

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.