从MYSQL中获取字符串并在Select stament

时间:2017-01-15 14:02:50

标签: php mysql

我试图从数据库中获取一个字符串并在Select语句中使用它。

$netpay1="netpay";
$tbp="Paye_deductions";
$tbq="Extra_deductions";

// Connect to server and select databse.
$conn = mysqli_connect("$host", "$username", "$password")or die("cannot connect"); 
mysqli_select_db($conn,"$db_name")or die("cannot select DB");

// Construct our join query
// sending query

下面的代码从MYSQL数据库中获取字符串

$rql = "Select * FROM $netpay1 WHERE Employee_Number = '$user1'";



// output data of each row
$result1 = mysqli_query($conn, $rql);
$row2 = mysqli_fetch_array($result1, MYSQLI_BOTH);
$Tax1 = $row2["Taxable_Deduction"];
$non = $row2["non_tax_deduction"];

获取字符串后,我想用它来选择字符串中的项目,但它显示错误“您的SQL语法中有错误;请查看与您的MySQL服务器版本对应的手册,以便在附近使用正确的语法'FROM Paye_deductions' 有关如何执行此操作的任何想法?

我查看了数据库,这就是字符串的保存方式“Company_Car,Housing_Allowance”。

$result6 =  "SELECT $tax1 FROM $tbp WHERE Employee_Number='$user1' AND Month='$months1' AND Year='$years1'";

$result55 = mysqli_query($conn, $result6);
$row55 = mysqli_fetch_assoc($result55, MYSQLI_BOTH);
$sum = array_sum($row55);

if (!$result55) {
    die('Invalid query: ' . mysqli_error($conn));
}

echo $sum;
echo $Tax1;

1 个答案:

答案 0 :(得分:-1)

您的查询中存在错误。试试这个:

  $rql = "Select * FROM ".$netpay1." WHERE Employee_Number = ".$user1;

每当你使用php变量时,不要将它包含在双引号中。