Avoiding a Single Quote

I am using a function pg_query_paramsto add values ​​to a table vmobjectson my page 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)));

Now I use pg_fetch_arrayto retrieve a string in an array.

I am using this query:

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

But it only works for a simple string such as james, helton, discovere, and not for j'ames, h'elton, d'iscovere.

Actually I want to get a string in both formats.

0
source share
4 answers

Use this:

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

, htmlentities($str, ENT_QUOTES); htmlspecialchars($str, ENT_QUOTES); , $str , (, $row[0]). , , , : print "Here an apostrophe '";

+2

, , both:

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

, : INSERT guestostype, guestosname SELECT. , , , , psql, .

0

I wrote a test program that looks more or less a program that performs the key functionality described above. Everything seems to be working fine. Can you explain how your program differs from this test program or provides its own test program?

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

Running this for me generates:

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

As mentioned earlier, you do not insert a guest name when you insert a record, so the same output shown here does not contain an annotation (guestosname). But insertion and selection seem to work like a champion, so it's not clear what is wrong.

0
source

All Articles