postgresql中的数据库字符串比较无法识别字符串长度

时间:2013-03-19 14:40:45

标签: php string postgresql

我在服务器之间传输数据,需要更改我们的wiki数据库中的一些链接。我写了一个快速的脚本,应该能够做到这一点没问题。但是当我运行它时,if($datacount)条件永远不会运行。这可能是我想念的简单事情,但我看不出是什么问题。

$dbconn = pg_connect("host= localhost port=5432 dbname=wiki user=user password=password");
$sql = "SELECT old_text FROM wiki_public.pagecontent LIMIT 10";
$go = pg_query($dbconn,$sql);
$i=0;
$string = 'sometext';
while($data = pg_fetch_assoc($go)) {
  $info = $data['old_text'];
  $count = strlen($string);
  $datacount = strlen($info);
  echo "Length: ".$datacount."<br/>";
  if($datacount != 0) {
    $position = strpos($string,$info);
    if($position !== false) {
      echo "The string ".$string." was found in row ".$i." and exists at position ".$position."The original content was: ".substr($info, $position,$count)."<br/>";
    }
  }
  $i++;
}

谢谢! 的修改

当我删除$datacount条件时,这是我的输出

Length: 0

Warning: strpos(): Empty delimiter in D:\www\import\linksupadte.php on line 12
Length: 12
Length: 0

Warning: strpos(): Empty delimiter in D:\www\import\linksupadte.php on line 12
Length: 31
Length: 31
Length: 0

Warning: strpos(): Empty delimiter in D:\www\import\linksupadte.php on line 12
Length: 444
Length: 1614
Length: 153
Length: 125

1 个答案:

答案 0 :(得分:1)

我今天宣布对我来说是无脑日。

<?php
# Database connection snipped.

# Let's say the database contains at least one row that's a substring
# of $string. Something like 'John', or '/123/'.
$string = 'John/123/456/789/012';
while($data = pg_fetch_assoc($go)) {

  $position = strpos($string, $data['old_text']);

  if ($position !== false) {
    echo "$string contains ".$data['old_text'];
  }
  else {
    echo "$string does not contain ".$data['old_text'];
  }
}

?>