如何逃避单一报价

时间:2011-05-19 12:19:27

标签: php arrays postgresql fetch

我正在使用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

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

4 个答案:

答案 0 :(得分:2)

根据 how to encode single quoteshtmlentities($str, ENT_QUOTES);htmlspecialchars($str, ENT_QUOTES);应该使用你想要转义的变量或字符串替换$ str的技巧(例如,$row[0]) 。如果您只想添加它,您只需添加它:print "Here's an apostrophe '";

答案 1 :(得分:0)

尝试使用此方法获取值以获取both

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

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

答案 2 :(得分:0)

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

<?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)注释。但插入和选择似乎像冠军一样,所以不清楚是什么问题。

答案 3 :(得分:0)

使用此:

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++;
}