How do I check if a query has returned a value or not because this isn't working? I swear I have done it before but I can't work out why it isn't working.

echo "<form action = 'NoughtsAndCrosses.php' method = 'POST'>
        <input type = 'text' name = 'game_search'>
        <input type = 'submit' value = 'Search'>
    </form>
    ";

if(isset($_POST["game_search"])) {
    $game_search = $_POST["game_search"];
    if(!empty($game_search)) {
        $query = "SELECT `game_id` FROM `games` WHERE `game_id`='".$game_search."'";
        $query_run = mysql_query($query);
        $query_result = mysql_result($query_run, 0);
        echo $query_result;
        if(mysql_num_rows($query_run) != 0) {
            echo "Games found";
        } else {
            echo "No Games Found.";
        }

    }
}

Dani AI

Generated

Quick diagnosis and context from the thread: the immediate error is calling mysql_result($query_run, 0) (and printing it) before you confirm the query actually returned any rows — that call will fail or return false when there are zero rows. Also check that mysql_query() itself didn't return false because of a SQL error. Finally, note that the old mysql_* extension is deprecated/removed in modern PHP; migrate to mysqli or PDO for new code. As and observed, you’ll usually want to search a name/title column (e.g. game_name) and allow partial matches rather than testing the game_id string-for-string. (php.net)

Use a prepared statement and test the result safely. The example below shows a compact PDO pattern that treats numeric input as an id lookup and non‑numeric input as a name search with wildcards. It avoids SQL injection and only tries to fetch once the query has succeeded.

// (set up $pdo with PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
$term = trim($_POST['game_search'] ?? '');
if ($term === '') { /* nothing to search */ }

if (ctype_digit($term)) {
  $stmt = $pdo->prepare('SELECT game_id FROM games WHERE game_id = ? LIMIT 1');
  $stmt->execute([(int)$term]);
} else {
  $stmt = $pdo->prepare('SELECT game_id FROM games WHERE game_name LIKE ? LIMIT 1');
  $stmt->execute(["%{$term}%"]);
}

$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row) {
  echo htmlspecialchars($row['game_id'], ENT_QUOTES, 'UTF-8');
} else {
  echo 'No games found.';
}

Use LIKE (with %) for partial matches and LIMIT 1 for a quick existence check. For counts, run SELECT COUNT(*) and use fetchColumn(); don’t rely on PDOStatement::rowCount() for portable SELECT counts. Enable exceptions (PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) while debugging so you’ll see SQL errors, and always escape output to avoid XSS. If problems persist, log the final SQL or catch the exception and inspect errorInfo() to see the DB error. (dev.mysql.com)

Recommended Answers

All 3 Replies

I might be wrong, but I think your SQL Where clause should not test for game_id but rather for game_name or something similar no?

I agree with perlexed, it looks as though you're asking for a game_id when... well when you already have one?

SELECT `game_id` FROM `games` WHERE `game_id`=

I personally would use the SQL like function, because if the users search term doesn't match your predefined name EXACTLY, you'll get no results.

The MySQL LIKE operator will return results with a similar value to the submitted search term.

I see what you guys mean, that is probably it... thanks :)

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.