0

我正在服务器之间传输数据,并且需要更改我们 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
4

1 回答 1

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'];
  }
}

?>
于 2013-03-19T16:07:24.063 回答