我试图弄清楚如何在PDO预处理语句中使用php变量。以下代码不返回任何内容。我不确定的部分是
$stmt = $conn->prepare("SELECT * FROM laptop WHERE '$ckey' = :avalue");
我正在尝试使用变量作为键和值,它似乎没有返回结果。 switch语句用于查找用户填写的字段。这是对基本库存系统的基本搜索。我知道有可用的开源解决方案,但我的公司不会让我们在网络上使用开源软件。我也意识到输出没有格式化成表格,但我想我会让查询工作,然后担心提高输出。我也意识到有一些类似的问题,但我还没有看到有人试图在语句的WHERE部分使用变量。如果是因为它不起作用,除了我,每个人都知道这一点,我道歉。
这是整个代码块。
function mkquery($ckey,$cdata)
{
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$stmt = $conn->prepare("SELECT * FROM laptop WHERE '$ckey' = :avalue");
//$stmt is the prepared query, you execute it to perform the query
$stmt->bindParam(':avalue',$ckey, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $val ) {
echo $val['Model'] . " ";
echo $val['Name'] . " ";
echo $val['DellTag'] . " ";
echo $val['HHSCTag'] . " ";
echo $val['OS'] . " ";
echo $val['KBOX'] . " ";
echo $val['SB'] . " ";
echo $val['Issued'] . " ";
echo $val['Tech'] . " ";
echo "<br>";
}
}
switch (isset($_POST)) {
case isset($_POST['name']):
$selector = 'name';
mkquery($selector,$_POST['name']);
break;
case isset($_POST['model']):
$selector = 'model';
mkquery($selector,$_POST['model']);
break;
case isset($_POST['delltag']):
$selector = 'delltag';
mkquery($selector,$_POST['delltag']);
break;
case isset($_POST['hhsctag']):
$selector = 'hhsctag';
mkquery($selector,$_POST['hhsctag']);
break;
case isset($_POST['city']):
$selector = 'city';
mkquery($selector,$_POST['city']);
break;
case isset($_POST['kbox']):
$selector = 'kbox';
mkquery($selector,$_POST['kbox']);
break;
case isset($_POST['sb']):
$selector = 'sb';
mkquery($selector,$_POST['sb']);
break;
case isset($_POST['issued']):
$selector = 'issued';
mkquery($selector,$_POST['issued']);
break;
case isset($_POST['tech']):
$selector = 'tech';
mkquery($selector,$_POST['tech']);
break;
default:
echo 'No input on form detected';
break;
}
a
$conn = null;