【问题标题】:php code to loop through databasephp代码循环遍历数据库
【发布时间】:2016-08-06 11:13:37
【问题描述】:

我是 初学者 使用 php,我有一个包含下表的 postgres 数据库:

--------------------------------------------------
| Phone_Number| User_ID| file_ID|  file_Path     |
--------------------------------------------------
| 123456789   | 12345  | abc123 | /picture_22.jpg|
--------------------------------------------------
| 987654321   | 67890  | xyz567 | /voice_33.mp3  |
--------------------------------------------------

我想遍历每一行,读取 file_ID 将它附加到 url 的末尾,转到 url 获取下载链接的结果,我不知道如何实现。我感谢任何帮助/协助。

P.S:这是我不完整的代码:

<?PHP
ini_set('error_reporting',E_ALL);

$website = "https://website.com;

//Connect to Database
$con = pg_connect( "postgres://................."  );

$query = "SELECT * FROM userdata";
$results = pg_query($con, $query);
$row_users = pg_fetch_array($results);

while ($row_users = pg_fetch_array($results)) {

    $fileID =    <---------------- Not sure what goes here
    $filePath = file_get_contents('$website/.getFile?file_id=.fileID');
    $fixedPath = str_replace("\\","","$filePath");

}

pg_close($con); 
?>

【问题讨论】:

    标签: php sql database postgresql


    【解决方案1】:

    我认为问题在于单引号

    $fileID =   $row_users['file_ID'];
    $filePath = file_get_contents($website."/getFile?file_id=".$fileID);
    $fixedPath = str_replace("\\","",$filePath);
    

    我认为您错过了带有变量$fileID 的“$”。 php解释器也不处理单引号内的语句,因此$filePath没有得到正确的值

    【讨论】:

      【解决方案2】:
      <?PHP
      ini_set('error_reporting',E_ALL);
      
      $website = "https://website.com;
      
      //Connect to Database
      $con = pg_connect( "postgres://................."  );
      
      $query = "SELECT * FROM userdata";
      $results = pg_query($con, $query);
      # NOT NEEDED You are losing a row here $row_users = pg_fetch_array($results);
      
      while ($row_users = pg_fetch_array($results)) {
      
          $fileID =   $row_users['file_ID']; # This is what goes here
          $filePath = file_get_contents("$website/.getFile?file_id={$fileID}");
          $fixedPath = str_replace("\\","","$filePath");
      
      }
      
      pg_close($con); 
      ?>
      

      除了标记您需要进行的更改以获取文件 ID 之外,我还在您的代码中标记了一个小错误,该错误会导致您丢失一行数据。并且在 file_get_contents 调用中使用你的变量。

      这里使用pg_query就可以了,但是如果需要使用参数,请使用pg_query_params

      【讨论】:

      • 感谢您的帮助,当我使用 XAMPP 运行代码时,出现以下错误:Call to undefined function pg_connect() in C:\xampp\htdocs\downloader.php on line 8 请提供有关如何解决此问题的任何提示?
      • 是的,您没有涉及php.net/manual/en/pgsql.installation.php 的扩展名(但这确实是一个不同的问题)
      • 我已经成功安装了扩展程序,但是,当我运行代码时,我收到以下通知:注意:未定义索引:C:\wamp64\www\downloader.php 中的文件_ID 看起来是 querypg_fetch_array($results) 部分导致了这种情况。
      • postgresql 列名应该是小写的。
      • 谢谢,我已经解决了这个问题,但我还有另一个问题:$filePath 得到以下值:{"ok":true,"result":{"file_id":"AwADBAADrAADmmT8BE_FGiduk6tZAg","file_size":106562,"file_path":"voice\/file_1.oga"}} 我似乎无法访问最后一部分来替换反斜杠(即@ 987654326@)
      【解决方案3】:
      <?PHP
          ini_set('error_reporting',E_ALL);
      
          $website = "https://website.com;
      
          //Connect to Database
          $con = pg_connect( "postgres://................."  );
      
          $query = "SELECT * FROM userdata";
          $results = pg_query($con, $query);
          $row_users = pg_fetch_array($results);
      
           while ($row_users = pg_fetch_array($results)) {
      
              $fileID = $row_users["file_ID"];
              $filePath = file_get_contents("$website/getFile?file_id=" . $fileID);
              $fixedPath = str_replace("\\","","$filePath");
      
            }
      
           pg_close($con); 
      ?>
      

      【讨论】:

        【解决方案4】:

        我认为可以:

        $fileID = $row_users['file_ID'];
        $filePath = file_get_contents("$website/.getFile?file_id=$fileID");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-23
          相关资源
          最近更新 更多