dongqiang1226 2013-08-12 12:11
浏览 48
已采纳

使用查询字符串中的变量获取数据?

I hope the title I used here was understandable...

I have a database with two columns: ward_id and ward_name.

I wish to create dynamic pages for each ward and have the ward_name show in the page title. I have created a header.php file which I am including.

I am passing the id through the URL using ....?wid={$row['ward_id']} which is working fine when I create other queries that use that id to get data from the database.

However the problem I am having is that the page refuses to display the ward_name as the page title. I expected something like this to work:

    $wardid = $_GET['wid'];

    $query = "SELECT ward_name, ward_id FROM wards WHERE ward_id=$wardid";
    $result = mysql_query($query);

    while ($row=mysql_fetch_array($result))
    {
$pagetitle = "$row['ward_name']";
    }

But it doesn't, I have tried so many variations on the above I can't possibly remember them all now so I really hope someone can help me... Here is the code as it currently stands:

Header Page:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <meta charset="utf-8">
    <title><?php echo $pagetitle; ?></title>
    <link rel="stylesheet" href="style.css">
    </head>

    <body>

        <div class="wholepage">

            <div class="headlinewrapper">
                <div class="headline">
                    <h1></h1>
                    <h2></h2>
                </div>
            </div>

            <div class="headlinesidewrapper">
                <div class="headlineside">
                    <p>shv jsfj sjnsf jnsf nsnf nj njsfn
njfjn sfns njf njnsf njs dgbjn dn jnd njjn dd d d nj njd njnd njd nn djndj njd</p>
                </div>
            </div>

            <div class="topnavigation">
                <ul>
                <li><a href="index.php">Home</a></li>
                <li><a href="boroughs.php">Boroughs</a></li>
                <li><a href="wards.php">Wards</a></li>
                </ul>
            </div>

            <div class="sidebar">

            </div>

            <div class="mainpagewrapper">

Dynamic page:

<?php

    $pagetitle = "Hello";
    include ('header.php');

    ?>

                <div class="mainpage">
                    <div class="infobox">

                    </div>
    <?php

    require('mysqli_connect.php');

    mysql_select_db('onetwom2_london');


    $wardid = $_GET['wid'];

    $query = "SELECT ward_name, ward_id FROM wards WHERE ward_id=$wardid";
    $result = mysql_query($query);

    while ($row=mysql_fetch_array($result))
    {
    echo "<div class=\"boroughlist\"><p>{$row['ward_name']}</p></div>" ;
    }

    $pagetitle = $result;

    ?>

    <div class="clear">
    </div>      
                </div>
            </div>
        </div>


    </body>

    </html>

So I just want to know how/if it is possible to match the id passed through the URL to the ward_id stored on the database and then have the page title display the ward_name linked to that id. I apologise if this is a really easy question, I have spent hours trying to work this out and I am completely stumped! (the code I posted above is just the end result of 5 hours of frustration so please appreciate I have tried hard before asking you for help :) )

展开全部

  • 写回答

3条回答 默认 最新

  • donglinli2027 2013-08-12 12:37
    关注

    You should step through the problem to see where it goes awry, var-dump $pagetitle in the while loop. See what is being stored if it comes out as NULL you are not retrieving anything from the DB and there is an issue with either Query. if it has the correct variable the problem is with your PHP. Var_dump $pagetitle in your header.php file to be sure it is getting the correct variable.

    Let me know the outcome and I can help you from there

    <?php
        $wardid = $_GET['wid'];
        $query = "SELECT ward_name, ward_id FROM wards WHERE ward_id=$wardid";
        $result = mysql_query($query);
        while ($row=mysql_fetch_array($result))
        {
            $pagetitle = "$row['ward_name']";
            //Step Through The Problem
            var_dump($pagetitle);
        }
        include ('header.php');
    ?>
    <div class="mainpage">
      <div class="infobox">
       </div>
      <?php
        require('mysqli_connect.php');
        mysql_select_db('onetwom2_london');
        $wardid = $_GET['wid'];
        $query = "SELECT ward_name, ward_id FROM wards WHERE ward_id=$wardid";
        $result = mysql_query($query);
        while ($row=mysql_fetch_array($result))
        {
           echo "<div class=\"boroughlist\"><p>{$row['ward_name']}</p></div>" ;
        }
        $pagetitle = $result;
      ?>
        <div class="clear">
        </div>      
    </div>
    

    UPDATED - Try This

    <?php
        require('mysqli_connect.php');
        mysql_select_db('onetwom2_london');
        $wardid = $_GET['wid'];
        $query = "SELECT ward_name, ward_id FROM wards WHERE ward_id=$wardid";
        $result = mysql_query($query);
        while ($row=mysql_fetch_array($result))
        {
            $pagetitle = $row['ward_name'];
            //Step Through The Problem
            var_dump($pagetitle);
        }
        include ('header.php');
    ?>
    <div class="mainpage">
      <div class="infobox">
       </div>
      <?php
        $result2 = mysql_query($query);
        while ($row2=mysql_fetch_array($result2))
        {
           echo "<div class=\"boroughlist\"><p>{$row2['ward_name']}</p></div>" ;
        }
      ?>
        <div class="clear">
        </div>      
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?