简体   繁体   English

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

[英]Having trouble retrieving data from a mysql database using php

I can't get the page to display the game information I think something is wrong with my select statement. 我找不到显示游戏信息的页面,我认为我的select语句有问题。

The game table has following fields: 游戏表具有以下字段:

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

I only what the page to display: 我只显示什么页面:

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

The page runs but won't display the game even if it in the database. 该页面运行,但是即使数据库中也没有显示游戏。

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

游戏表

<?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');
    ?>

Your query is incorrect, as you have your wildcard % outside the quotes 您的查询不正确,因为您的通配符%不在引号中

WHERE game_name LIKE '$search'%

Need to move the % inside the quotes 需要在引号内移动%

WHERE game_name LIKE '$search%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM