如何构建 Joomla!查询匹配 table 中的两个字段值?
How to construct a Joomla! query for matching two field values in a table?
我有一个 table #__newtoys_variants 有很多字段,其中有 id 和 v_prod_id。
现在虽然 id 是唯一的 - v_prod_id 是产品 id
url 显示产品信息和价格
example.com/index.php?option=com_newtoys&id=2&vid=7
这里的 id 值是从 id 中提取的,vid 是 v_prod_id 从 db table 行中提取的对应
下面是table的简介
id v_prod_id v_price
1 7 200
2 7 220
3 1 250
4 1 270
5 2 300
6 10 350
7 9 220
8 7 195
现在我打算在前端显示 404 错误/500 错误/页面不存在 - 如果 id 和 v_prod_id 在前端不匹配 url
如果用户将 url 更改为
example.com/index.php?option=com_newtoys&id=2&vid=1
然后想要在前端显示404错误/500错误/页面不存在
这是 Table 数据库
任何人都可以帮助它实现同样的目标吗
这是一个简短的函数 - 不确定 sql 查询或函数中究竟应该包含什么,以便 id & v_prod_id 应该像在数组中一样匹配,如果结果为零,则错误消息可以被显示
function loadProduct($id ,$vid){
$mainframe =JFactory::getApplication();
$option = JRequest::getCmd('option');
$db =JFactory::getDBO();
global $Itemid;
$sql = "";
$db->setQuery($sql);
if ($rows = $db->loadObjectlist()) {
return $rows[0];
} else {
if ($db->getErrorNum()) {
JError::raiseError(500, "Something went horribly wrong, the query returned the error ". $db->getErrorMsg());
} else {
JError::raiseError(404, "404, Page does not Exists ". $db->getErrorMsg());
}
}
}
谁能帮忙提点建议。增加赏金
检查 v_prod_id 匹配的示例代码
<?php
//connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
if(isset($_GET['id']) && ($_GET['vid'])){
//sanitize the id and vid i didnt sanitized
$id = $_GET['id'];
$vid = $_GET['vid'];
//Coded by Ajmal PraveeN
$sqlq = $conn->prepare("SELECT id, v_prod_id FROM __newtoys_variants WHERE id= :id");
$sqlq->bindValue(':id', $id);
$sqlq->execute();
$row = $sqlq->fetch(PDO::FETCH_ASSOC);
//if you want to check the id whether it matches? remove the comment line and check the id matches
if(/*($id !== $row['id']) && */($vid !== $row['v_prod_id'])){
echo 'v prod id error';
}
?>
试试这个:
function loadProduct($id, $vid){
$mainframe = JFactory::getApplication();
$option = JRequest::getCmd('option');
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('id, v_prod_id');
$query->from($db->quoteName('#__newtoys_variants'));
$query->where($db->quoteName('id')." = ".$db->quote($id), 'AND');
$query->where($db->quoteName('v_prod_id')." = ".$db->quote($vid));
$db->setQuery($sql);
if ($rows = $db->loadObjectlist()) {
return $rows[0];
} else {
if ($db->getErrorNum()) {
JError::raiseError(500,
"Something went horribly wrong, the query returned the error ". $db->getErrorMsg());
} else {
JError::raiseError(404,
"404, Page does not Exists ". $db->getErrorMsg());
}
}
}
有关详细信息,请参阅此处:
https://docs.joomla.org/Selecting_data_using_JDatabase
更新
您在评论中询问了 Joomla!以下查询的翻译:
Select *, (select prod_name from #__newtoy_products where id=v.id) as prod_name
from #__newtoys_variants AS v Where v.state='1' and v.id = '".$v_prod_id."'
...基本与此相同:
SELECT v.*, p.prod_name
FROM #__newtoys_variants AS v
LEFT JOIN #__newtoy_products AS p
ON p.id = v.id
WHERE v.state='1' and v.id = '".$v_prod_id"'
...在 Joomla 中应该映射到这样的东西!:
$query->select(array('v.*', 'p.prod_name'))
->from($db->quoteName('#__newtoys_variants', 'v'))
->join('LEFT', $db->quoteName('#__newtoy_products', 'p'))
. ' ON (' . $db->quoteName('p.id') . ' = ' . $db->quoteName('v.id') . ')')
->where($db->quoteName('v.state')." = ".$db->quote(1), 'AND')
->where($db->quoteName('v.id')." = ".$db->quote($v_prod_id));
我有一个 table #__newtoys_variants 有很多字段,其中有 id 和 v_prod_id。
现在虽然 id 是唯一的 - v_prod_id 是产品 id url 显示产品信息和价格
example.com/index.php?option=com_newtoys&id=2&vid=7
这里的 id 值是从 id 中提取的,vid 是 v_prod_id 从 db table 行中提取的对应
下面是table的简介
id v_prod_id v_price
1 7 200
2 7 220
3 1 250
4 1 270
5 2 300
6 10 350
7 9 220
8 7 195
现在我打算在前端显示 404 错误/500 错误/页面不存在 - 如果 id 和 v_prod_id 在前端不匹配 url
如果用户将 url 更改为
example.com/index.php?option=com_newtoys&id=2&vid=1
然后想要在前端显示404错误/500错误/页面不存在
这是 Table 数据库
这是一个简短的函数 - 不确定 sql 查询或函数中究竟应该包含什么,以便 id & v_prod_id 应该像在数组中一样匹配,如果结果为零,则错误消息可以被显示
function loadProduct($id ,$vid){
$mainframe =JFactory::getApplication();
$option = JRequest::getCmd('option');
$db =JFactory::getDBO();
global $Itemid;
$sql = "";
$db->setQuery($sql);
if ($rows = $db->loadObjectlist()) {
return $rows[0];
} else {
if ($db->getErrorNum()) {
JError::raiseError(500, "Something went horribly wrong, the query returned the error ". $db->getErrorMsg());
} else {
JError::raiseError(404, "404, Page does not Exists ". $db->getErrorMsg());
}
}
}
谁能帮忙提点建议。增加赏金
检查 v_prod_id 匹配的示例代码
<?php
//connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
if(isset($_GET['id']) && ($_GET['vid'])){
//sanitize the id and vid i didnt sanitized
$id = $_GET['id'];
$vid = $_GET['vid'];
//Coded by Ajmal PraveeN
$sqlq = $conn->prepare("SELECT id, v_prod_id FROM __newtoys_variants WHERE id= :id");
$sqlq->bindValue(':id', $id);
$sqlq->execute();
$row = $sqlq->fetch(PDO::FETCH_ASSOC);
//if you want to check the id whether it matches? remove the comment line and check the id matches
if(/*($id !== $row['id']) && */($vid !== $row['v_prod_id'])){
echo 'v prod id error';
}
?>
试试这个:
function loadProduct($id, $vid){
$mainframe = JFactory::getApplication();
$option = JRequest::getCmd('option');
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('id, v_prod_id');
$query->from($db->quoteName('#__newtoys_variants'));
$query->where($db->quoteName('id')." = ".$db->quote($id), 'AND');
$query->where($db->quoteName('v_prod_id')." = ".$db->quote($vid));
$db->setQuery($sql);
if ($rows = $db->loadObjectlist()) {
return $rows[0];
} else {
if ($db->getErrorNum()) {
JError::raiseError(500,
"Something went horribly wrong, the query returned the error ". $db->getErrorMsg());
} else {
JError::raiseError(404,
"404, Page does not Exists ". $db->getErrorMsg());
}
}
}
有关详细信息,请参阅此处:
https://docs.joomla.org/Selecting_data_using_JDatabase
更新
您在评论中询问了 Joomla!以下查询的翻译:
Select *, (select prod_name from #__newtoy_products where id=v.id) as prod_name
from #__newtoys_variants AS v Where v.state='1' and v.id = '".$v_prod_id."'
...基本与此相同:
SELECT v.*, p.prod_name
FROM #__newtoys_variants AS v
LEFT JOIN #__newtoy_products AS p
ON p.id = v.id
WHERE v.state='1' and v.id = '".$v_prod_id"'
...在 Joomla 中应该映射到这样的东西!:
$query->select(array('v.*', 'p.prod_name'))
->from($db->quoteName('#__newtoys_variants', 'v'))
->join('LEFT', $db->quoteName('#__newtoy_products', 'p'))
. ' ON (' . $db->quoteName('p.id') . ' = ' . $db->quoteName('v.id') . ')')
->where($db->quoteName('v.state')." = ".$db->quote(1), 'AND')
->where($db->quoteName('v.id')." = ".$db->quote($v_prod_id));