php - 根据 URL 示例 (www.example.co.uk/essex.php) 从 MySQL 数据库中选择数据

标签 php mysql database dynamic

我是 php 的新手,我想创建动态页面。

基本上,如果有人登陆 www.example.co.uk/essex.php,我希望能够添加类似 <h1>Company In <?php echo $row['County']; ?> 的内容。这样它就会显示 Company In Essex 如果我需要为另一个县或镇复制页面,可以节省时间。

到目前为止,我已经在phpmyadmin 中设置了一个数据库

Picture of Table

到目前为止,我获取表格的代码是;

$dbconnect = mysqli_connect("HOST", "USER", "PASSWORD", "DB");
if(mysqli_connect_errno()) {
    echo "Connection Failed:".mysqli_connect_error();
    exit;
}

$count_sql="SELECT * FROM areas";
$count_query=mysqli_query($dbconnect, $count_sql);
$count_rs=mysqli_fetch_assoc($count_query);

然后在 www.example.co.uk/essex.php 上

<?php
do {
    echo $count_rs['County'];
} while ($count_rs=mysqli_fetch_assoc($count_query))

?>

但我出错的地方是它从表中提取了 County 的所有数据,而我只希望它提取 Essex 县。

请原谅我解释得不好。

更新 -

    $row = url2content();
extract($row);


function url2content($url = NULL){
if(is_null($url)){
if(isset($_SERVER["REQUEST_URI"]) && $_SERVER["REQUEST_URI"]){
$url = $_SERVER["REQUEST_URI"];
}elseif(isset($_SERVER["PATH_INFO"]) && $_SERVER["PATH_INFO"]){
$url = $_SERVER["PATH_INFO"];
}
if (substr($url,0,1) == '/'){
$url = substr($url,1);
}
}
if (DEBUG_MODE){
echo ('URL requested: "'.$url.'"<br>');
}



function get_towns($county){
    $query = sprintf("select townName from " . AREAS_TABLE . " where countyName = '%s'  order by rand() LIMIT 0,24" ,mysql_real_escape_string($county));
    $res = mysql_query($query);
    if(!$res){
        printf("Unable to query ".AREAS_TABLE." table: %s \n", mysql_error());
    }
    $html="";
    while($row = mysql_fetch_assoc($res)){
        $html.="<li>".stripslashes($row["townName"])."</li>\n";
    }
    return $html;
}}

最佳答案

您必须在查询中使用 WHERE 子句才能从数据库中选择特定国家/地区
SELECT * from table WHERE felid_country=$count_rs['County'];

关于php - 根据 URL 示例 (www.example.co.uk/essex.php) 从 MySQL 数据库中选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45031234/

相关文章:

php - 如何查看 rgb 颜色是否太亮

php - 交响乐 2 : How to avoid the sessions table being dropped by doctrine migrations?

MySQL 错误 1054 : Unknown column in 'field list' on INSERT

mysql - 查找售价低于我的产品

C# - 无法使用 new SqlConnection() 连接到本地 ACCDB 文件

php - 更新数据库记录不起作用

PHP 过滤器使用 preg_replace 只允许字母数字和一些标点符号

php - 在命名空间中使用 SPLFixedArray

php - 如何在PHP函数中使用Mysql数组的行?

c# - 动态更改 SQL Server 数据库连接