i'm new learn php and mysql and i got stuck can't get better logic for my brain i want to get message from page if i type url like index.php?user=user1 and page will show with data table and if username doesn't exist and page show no username found so this example when i type index.php?user=22222 and page show message no username found here my query

<?php
.....
if(isset($_GET['user'])) {
        $result = mysql_query("select * from content where username = '" . $_GET['user'] . "' limit 3;");

        $get_jml = mysql_query("select count(*) as jml from content");
        $jml = 0;
        while($h_jml = mysql_fetch_array($get_jml)){
        $jml_artcles = $h_jml['total'];         
            }   
        }else {}
        $jml_page = ceil($jml_articles); 
?>

        <?php while($data = mysql_fetch_array($result)){ ?>
/*some code here */

        <?php } ?>  

what can i add or change my query above sorry my english bad
thanks

Dani AI

Generated

Good that added a check for “no rows” — that fixes the immediate symptom. For a safe, maintainable solution follow ’s advice: stop using the old mysql_* API and use PDO or mysqli with prepared statements. The basic flow should be: prepare → bind/execute → check whether any rows were returned → display results or a friendly “not found” message (optionally send a 404 status).

// PDO outline (replace credentials)
$pdo = new PDO('mysql:host=localhost;dbname=your_db;charset=utf8mb4','dbuser','dbpass',[
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$user = isset($_GET['user']) ? $_GET['user'] : '';

$stmt = $pdo->prepare('SELECT * FROM content WHERE username = :user LIMIT 3');
$stmt->execute([':user' => $user]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

if (count($rows) === 0) {
    header('HTTP/1.1 404 Not Found');
    echo '<h2 style="color:#EA6213;text-align:center;">Username not found or no posts</h2>';
} else {
    foreach ($rows as $row) {
        echo '<h3>' . htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8') . '</h3>';
        echo '<div>' . htmlspecialchars($row['body'], ENT_QUOTES, 'UTF-8') . '</div>';
    }
}

Troubleshooting notes and cautions: prepared statements prevent SQL injection; always escape output with htmlspecialchars to avoid XSS. If expecting a single user record use LIMIT 1 and fetch() instead of fetchAll(). Avoid redundant COUNT queries unless needed for pagination — when counting, ensure the column alias matches the code (that kind of mismatch and variable-typo will silently break the logic). Also note PDOStatement::rowCount() is not reliable for SELECT on all drivers; prefer fetchAll() + count() or check fetch() result.

Posts can’t be removed, but can be marked solved as suggested. This gives a cleaner record for future readers and follows the safer pattern shown above.

Recommended Answers

All 6 Replies

Member Avatar for Member #120589

Don't use mysql_* functions - they are deprecated - use mysqli or PDO instead,

Don't place input vaiables directly into SQL as you may be open to SQL injection - use prepared statements and bind parameters.

What is this trying to do? The two queries don't seem to make much sense.

i was wrong for this .. I've resolve this problem
how can i delete this post ?

@diafol I have not learned about PDO or MSYQLI ..if you can help .. can you type example from my query above from mysql to PDO ?
Thanks

Posts are never deleted. You can always click solved.

Can you at least tell us what was wrong and what you did to fix it?

Member Avatar for Member #120589

@diafol I have not learned about PDO or MSYQLI ..if you can help .. can you type example from my query above from mysql to PDO ?
Thanks

No sorry. You can learn PDO or mysqli from the php.net manual or from hundreds of online tutorials - or check out pritaeas' php snippets on Daniweb - you just need to search. You can then test your own examples. :)

my problem is if url type index.php?user=user1 is on table so the data will show else if index.php?user=user2 doesn't exist on table the page will show message about what i want example "the url doesn't exist" i fix it base on query above with add this

<?php
.....
if(isset($_GET['user'])) {
$result = mysql_query("select * from content where username = '" . $_GET['user'] . "' limit 3;");
if(mysql_num_rows($result) != 1){
        echo"<h2 style='color:#EA6213;text-align:center;'>Username is invalid or you don't have any post here..</h2>";
        echo "<br/>";
        }

@diafol : Ok thanks for your suggest

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.