使用php从mysql数据库中检索数据时遇到问题

时间:2015-11-29 22:34:26

标签: php database

我无法获取显示游戏信息的页面,我认为我的选择声明有问题。

游戏桌有以下字段:

game_id, game_name, platform, genre, game_description, game_price,
game_image, game_headlines, and game_added_date.

我只显示要显示的页面:

 game_name, platform, genre, game_description, game_price, game_image, and game_headlines. 

页面运行但即使它在数据库中也不会显示游戏。

网站网址:http://cs.neiu.edu/~fsef15g9/CS319_Project_Group_9/view_games.php

game tabel

<?php # add_games.php 
    include ('includes/session.php');

    $page_title = 'Add Games';
    include ('includes/header.php');

    require_once ('mysqli_connect.php'); // Connect to the db.

    // Check if the form has been submitted:
    if (isset($_POST['submitted'])){

        $errors = array(); // Initialize an error array.

        // Check for a game name:
        if (empty($_POST['game_name'])) {
                $errors[] = 'You forgot to enter game name.';
        } else {
                $search = mysqli_real_escape_string($dbc, trim($_POST['game_name']));
        }

        //$where = '%' + '$search' + '%';
        $q = "SELECT 
              game_name AS game, 
              platform AS pf, 
              genre AS ge, 
              game_description AS gd, 
              game_price AS gp, 
              game_image AS gi, 
              game_headlines AS gh 
              FROM game WHERE game_name LIKE '$search'%";
        $r = @\mysqli_query($dbc, $q); // Run the query.

        // Count the number of returned rows:
        $num = mysqli_num_rows($r);

        if ($num > 0) { // If it ran OK, display the records.

            // Print how many users there are:
            echo "<p>There are currently $num number of games for '$search'.</p>\n";

            // Table header.
            echo '<table align="center" cellspacing="3" cellpadding="3" width="75%">
            <tr><td align="left"><b>Game Name</b></td><td align="left"><b>Platform</b></td><td align="left"><b>Genre</b></td><td align="left"><b>Game Description</b></td><td align="left"><b>Game Price</b></td><td align="left"><b>Game Headlines</b></td></tr>';

            // Fetch and print all the records:
            while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
                echo '<tr><td align="left">' . $row['game'] . '</td><td align="left">' . $row['pf'] . '</td><td align="left">' . $row['ge'] . '</td><td align="left">' . $row['gd'] . '</td><td align="left">' . $row['gp'] . '</td><td align="left">' . $row['gi'] . '</td><td align="left">' . $row['gh'] . '</td></tr>';
            }

            echo '</table>'; // Close the table.

            mysqli_free_result ($r); // Free up the resources.  

        } else { // If no records were returned.

            echo '<p class="error">There are currently no registered users.</p>';

        }

        mysqli_close($dbc); // Close the database connection.
    }
    ?>

    <div class="page-header">

        <h1 style="font-size:40px"><center>Search Games</center></h1>
    </div>
    <form class="form-signin" role="form" action="view_games.php" method="post">

        <center><p><input type="normal" placeholder="Enter Game Name" required autofocus name="game_name" maxlength="60" value="<?php if (isset($_POST['game_name'])) echo $_POST['game_name']; ?>" />
        <button style="display:inline; padding:10px; margin-bottom:5px" type="submit" name="submit" class="btn btn-sm btn-primary" />Add Game</button>
        <input type="hidden" name="submitted" value="TRUE" /></p></center>

    </form>

    <?php
    include ('includes/footer.html');
    ?>

1 个答案:

答案 0 :(得分:3)

您的查询不正确,因为您的通配符%在引号之外

WHERE game_name LIKE '$search'%

需要移动引号内的%

WHERE game_name LIKE '$search%'