如何转义单引号


How to escape a single quote

我正在使用pg_query_params函数在我的表格vmobjects addvm.php页面中添加值。

$query = "INSERT INTO vmobjects(guid, ipaddress, username, password, hostid, vmname, guestostype) VALUES($1, $2, $3, $4, $5, $6,
$7)";
        $result = pg_query_params($conn, $query, array($guid, $ip, $username, $password, $hostid, $name, strtolower($os)));

现在我使用 pg_fetch_array 来获取数组中的行。

我正在使用这个查询:

$query = "select vmname, guid, hostid, guestosname from vmobjects";
AddLog("infrastructure.php", "Query: ".$query, ERR_DEBUG_LOW);
$result = pg_query($conn, $query);
$no_records = pg_num_rows($result);
$j = $no_records;
$i = 0;
while($row = pg_fetch_array($result))
{
    if($row[3] == "")
    {
        $vmobj_Array[$i] = $row[0] . '***' . $row[1] . '***' . $row[2];
    }
    else
    {
        $vmobj_Array[$i] = $row[0] . ' ( ' . $row[3] . ' )' . '***' . $row[1] . '***' . $row[2];
    }
    $i++;
}

但它仅适用于像 james, helton, discovere 这样的简单字符串,不适用于 j'ames, h'elton, d'iscovere .

实际上,我想以两种格式获取该行。

根据如何编码单引号,htmlentities($str, ENT_QUOTES);htmlspecialchars($str, ENT_QUOTES);应该做一个技巧,$str应该被你想要转义的变量或字符串替换(例如,$row[0])。如果你只是想添加它,你需要做的就是添加它:print "Here's an apostrophe '";

尝试使用它来获取值以获取both

while($row = pg_fetch_array($result, null, PGSQL_BOTH)){

此外,您的查询也不同:在INSERT中插入guestostype,但在选择中选择guestosname。我猜您的查询根本没有返回任何值,因为您请求的行不存在,但psql验证所有数据是否都在表中。

我写了一个测试程序,它似乎或多或少是一个执行上述关键功能的程序。 它似乎工作得很好。 您能解释一下您的程序与该测试程序有何不同或提供您自己的测试程序吗?

<?php
$conn = pg_connect("host=localhost");
$result = pg_exec($conn,"drop table tvmobjects cascade;");
$result = pg_exec($conn,"create table tvmobjects (guid text not null, ipaddress text not null, username text not null, password text, hostid text not null, vmname text not null, guestostype text, guestosname text);");
function add_user($conn,$guid,$ip,$username,$password,$hostid,$name,$os)
{
  $query = "INSERT INTO tvmobjects(guid,ipaddress,username,password,hostid,vmname,guestostype) VALUES($1, $2, $3, $4, $5, $6, $7)";
  $result = pg_query_params($conn,$query,array($guid,$ip,$username,$password,$hostid,$name,strtolower($os)));
  $no_records=pg_num_rows($result);
  echo "Got $no_records in insert of $username'n";
}
add_user($conn,"james","1.2.3.4","james","semaj", "jamesid", "jamesvm", "jamesostype");
add_user($conn,"j'ames","1.2.3.5","j'ames","semaj", "j'amesid", "j'amesvm", "j'amesostype");
$query= "select vmname,guid,hostid,guestosname from tvmobjects";
$result = pg_query($conn,$query);
$no_records=pg_num_rows($result);
$j=$no_records;
$i=0;
while($row = pg_fetch_array($result))
{
  if ($row[3]=="")
  {
    echo $row[0].'***'.$row[1].'***'.$row[2]."'n";
  }
  else
  {
    echo $row[0].' ( '.$row[3].' )'.'***'.$row[1].'***'.$row[2]."'n";
  }
  $i++;
}
?>

为我运行它会生成:

Got 0 in insert of james
Got 0 in insert of j'ames
jamesvm***james***jamesid
j'amesvm***j'ames***j'amesid

如前所述,插入记录时不会插入 guestosname,因此此处显示的输出也没有 (guestosname) 注释。 但是插入和选择似乎像冠军一样工作,所以不清楚出了什么问题。

使用这个:

while($row = pg_fetch_array($result))
{
    if($row[3] == "")
    {
        $vmobj_Array[$i] = htmlentities($row[0], ENT_QUOTES) . "***" . $row[1] . "***" . $row[2];
    }
    else
    {
        $vmobj_Array[$i] = htmlentities($row[0], ENT_QUOTES) . "***" . $row[1] . "***" . $row[2];
    }
    $i++;
}