从 URL GET 参数创建 MySQL 查询 [英] Create MySQL query from URL GET parameters

查看:48
本文介绍了从 URL GET 参数创建 MySQL 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我是 PHP 和 MYSQL 的初学者,我遇到了一个基本问题.如何通过 URL 参数进行查询

hey i am a beginner in PHP and MYSQL, i am stuck with a basic problem. How can i make my query from URL parameters

我的网址示例:

www.abs.com/index.php?cat=shoes

www.abs.com/index.php?cat=shoes&subcat=sports

www.abs.com/index.php?cat=shoes&subcat=sports&color=blue

我如何在我的查询中处理这种 URL,这只是我的示例查询,我的两个表都有大量字段.我需要关于查询的 WHERE 部分的帮助,主要问题是我在我的 URL 中传递 (cat),它在我的数据库中 (b.category).

How can i handle this kind of URL's in my query, that is just my example query, i have a large set of fields for both of my tables. I need help with the WHERE part of query, main problem is i am passing (cat) in my URL where it is (b.category) in my database.

我的查询:

select a.p_id, a.p_name, a.p_prize, b.p_id b.color, b.category, b.subcategory
FROM products a INNER JOIN details b ON a.p_id=b.p_id

推荐答案

首先,您需要通过将 URL 参数分配给 PHP 变量来使用 PHP 处理 URL:

First you need to process the URL using PHP by assigning the URL parameters to PHP variables:

$cat = $_GET['cat'];
$subcat = $_GET['subcat'];
$color= $_GET['color'];

然后你可以使用这些变量来创建一个 MySQL 查询字符串:

Then you can use these variables to create a MySQL query string:

$queryString = "SELECT a.p_id, a.p_name, a.p_prize, b.p_id b.color, b.category, b.subcategory
FROM products a INNER JOIN details b ON a.p_id=b.p_id WHERE b.category = '" . mysql_real_escape_string( $cat ) . "' AND b.subcategory = '" . mysql_real_escape_string( $subcat ) . "' AND b.color = '" . mysql_real_escape_string( $color ) . "' ";

然后您可以使用此查询字符串来查询数据库.

You can then use this query sting to query the database.

这篇关于从 URL GET 参数创建 MySQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆