【发布时间】:2014-05-27 04:11:16
【问题描述】:
我有一个这样的 php 文件:
<?php define('AREA', 'C');
require ('prepare.php');
require ('init.php');
$con = mysql_connect(Registry::get ( 'config.db_host' ), Registry::get ( 'config.db_user' ), Registry::get ( 'config.db_password' ))or die("Unable to connect to MySQL");
mysql_select_db(Registry::get ( 'config.db_name' ));
if (!empty($_POST['dealer_company']))
{
$dealerCompany = 'Your company'; //$_POST['dealer_company'];
$result = mysql_query("SELECT distinct a.firstname, a.lastname, a.email, a.user_id
FROM cscart_users a,cscart_usergroup_links b, cscart_usergroup_descriptions c
WHERE b.usergroup_id=c.usergroup_id and b.status = 'A' and a.user_id=b.user_id and
c.usergroup='Dealer' and a.company = 'Your company'",$con) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$d_Infos[]=$row;
}
}
if (!empty($_POST['dealer_company']))
{
$dealerInfos = '<select id="as_dealer_id" name="as_dealer_id" class="input-text">
<option value="">Please select</option>';
foreach ($d_Infos as $k => $v)
{
$dealerInfos .= '<option value="'.$v['user_id'].'">'.$v['firstname'].'</option>';
}
$dealerInfos .= '</select>';
echo $dealerInfos;
}
?>
如您所见,我在 SQL 查询中有一个条件是 a.company = 'Your company'。 但它不能选择具有该值的列。结果是空的。如果“您的公司”中没有空格,它将起作用,例如:“您的公司”。 所以我的问题是: 是否可以选择条件包含空格的行? 非常感谢。
【问题讨论】:
-
您可以尝试使用 PHP 的
trim()函数,这是一种方法。我确定有一个 SQL 等价物,但我不记得它是什么了。所以试试$dealerCompany = trim('Your company ');然后and a.company = '".$dealerCompany."' -
@Fred-ii- 你喝醉了。是
a.company = 'Your company'没问题,你确定数据库值吗? -
感谢您的帮助,但问题是我的数据库中的数据也包含空间。在公司栏中,它也是“您的公司”。 :(
-
我测试了一段类似的代码,我在这里玩过,使用
trim()效果很好。它将删除在 POST 变量的左侧和/或右侧找到的任何空格。即:$var=trim($_POST['var']); -
更具体地说是
$dealerCompany = trim($_POST['dealer_company']);,专为您定制。
标签: php mysql sql sql-server