Php可变数据分配

时间:2015-03-10 12:13:35

标签: php sql variables boolean

我想知道为什么第二个输出$ devNO给了我bool(false)这是空的。我根本不知道。我在mysql_query()部分错了吗?

$developer = $_POST['dev'];
$platform = $_POST['plat'];
$genre = $_POST['gen'];

var_dump($developer);
echo "<br>";

$devNO = mysql_query("SELECT No FROM developer WHERE Developer = $developer");
$platNO = mysql_query("SELECT No FROM platform WHERE Platform = $platform");
$genNO = mysql_query("SELECT No FROM genre WHERE Genre = $genre");

var_dump($devNO);

这是我的输出:

enter image description here

在这里,我将展示我的完整代码&#34; games.php&#34;:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">

<html>
<head lang="en">
    <meta charset="utf-8" />
    <title>Game List</title>
    <link rel="stylesheet" href="css.css">
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">

    <style>
        legend {font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif}
    </style>
</head>
<body>
<div id="big_wrapper">
    <header id="top_header">
        <h1><a href="index.html">Dandy's Game Library</a></h1>
    </header>
    <nav id="top_menu">
        <ul>
            <li><strong></stron><a href="index.html">Home</a></strong></li>
            <li><strong><a href="games.php">Game List</a></strong></li>
        </ul>
    </nav>


    <div id="game_wrapper">
        <section id="filter">
            <form action="games.php" method="post" name="search_form">
                <fieldset>
                    <legend><h3><strong>Search</strong></h3></legend>
                    <strong>Developer</strong><br>
                    <select name="dev">
                        <option value="">--Select--</option>
                        <?php

                        include("dbConnection.php");

                        mysql_connect("localhost","root","");
                        mysql_select_db("games");

                        $sql = mysql_query("SELECT Publisher FROM publisher");


                        while($row = mysql_fetch_array($sql)){
                            echo "<option value='".$row['Publisher']."'>" .$row[Publisher]. "</option>";
                        }

                        ?>
                    </select>

                    <br/><br/><strong>Game Platform</strong><br>
                    <select name="plat">
                        <option value="">--Select--</option>
                        <?php

                        $sql = mysql_query("SELECT Platform FROM platform");


                        while($row = mysql_fetch_array($sql)){
                            echo "<option value='".$row['Platform']."'>" .$row[Platform]. "</option>";
                        }
                        ?>
                    </select>

                    <br/><br/><strong>Genre</strong><br>
                    <select name="gen">
                        <option value="">--Select--</option>
                        <?php

                        $sql = mysql_query("SELECT Genre FROM genre");


                        while($row = mysql_fetch_array($sql)){
                            echo "<option value='".$row['Genre']."'>" .$row[Genre]. "</option>";
                        }

                        $developer = $_POST['dev'];
                        $platform = $_POST['plat'];
                        $genre = $_POST['gen'];

                        ?>
                    </select>

                    <br><br><input type="submit" name="search" value="Search"></input>

                </fieldset>
            </form>
        </section>
        <aside id="items">
            <fieldset>
                <legend><h3><strong>Game List</strong></h3></legend>

                <?php

                    var_dump($developer);

                    $devNO = mysql_query("SELECT No FROM developer WHERE Developer = $developer");
                    $platNO = mysql_query("SELECT No FROM platform WHERE Platform = $platform");
                    $genNO = mysql_query("SELECT No FROM genre WHERE Genre = $genre");

                    var_dump($devNO);

                    $sql = sprintf("SELECT Title, Release_Year, Language, Price FROM games WHERE Developer_NO = $devNO");
                    $result = mysql_query($sql);

                    $game_title = 'Title';
                    $game_year = 'Release_Year';
                    $game_lan = 'Language';
                    $game_price = 'Price';
                ?>

                <div id="gamelist">

                    <?php

                        if(!$result) {
                            die(mysql_error());
                        }
                        while($row = mysql_fetch_array($result)) {
                    ?>
                        <div class="row">
                            <div class="cell"><?php echo $row[$game_title]?></div>
                            <div class="cell"><?php echo "Year : ".$row[$game_year]?></div>
                            <div class="cell"><?php echo "Language : ".$row[$game_lan]?></div>
                            <div class="cell"><?php echo "Price : RM".$row[$game_price]?></div>
                        </div>
                    <?php
                        }
                    ?>
                </div>





            </fieldset>
        </aside>
    </div>
</div>
<div id="btm_wrapper">
    <footer id="foot">
        <strong></strong>
    </footer>
</div>
</body>
</html>

从下拉列表中搜索后,完整输出将如下所示: enter image description here

2 个答案:

答案 0 :(得分:0)

如果$ developer是一个字符串使用引号。您需要首先从结果集中获取结果,然后在代码中使用它:

<?php

    $devNOResult = mysql_query("SELECT No FROM developer WHERE Developer = '$developer'");
    $platNOResult = mysql_query("SELECT No FROM platform WHERE Platform = '$platform'");
    $genNOResult = mysql_query("SELECT No FROM genre WHERE Genre = '$genre'");

    $devNO = mysql_fetch_row($devNOResult);

    $sql = sprintf("SELECT Title, Release_Year, Language, Price FROM games WHERE Developer_NO = $devNO");
    $result = mysql_query($sql);

$game_title = 'Title';
$game_year = 'Release_Year';
$game_lan = 'Language';
$game_price = 'Price';
                ?>

                <div id="gamelist">

                    <?php

                        if(!$result) {
                            die(mysql_error());
                        }
                        while($row = mysql_fetch_array($result)) {
                    ?>

答案 1 :(得分:0)

方法mysql_query(),如果成功,则返回一个结果集,即使从查询中只返回一个值,也必须将其作为数组。

理想情况下,此阵列上应该有for-each迭代以获得结果,但是如果您确定您将获得至少一个值,则第一行具有所需的索引(&#39; No& #39;在你的情况下)。

$dataset= mysql_query("SELECT No FROM developer WHERE Developer = '$developer'");
$row = mysql_fetch_assoc($dataset);
$devNO = $row['No'];

另请注意我用于varchar的引用。在另一个答案中正确指出了这一点。立即尝试。