如何从数据库中获取所有值并返回到XML字段中的PHP代码?
P粉563446579
P粉563446579 2024-03-30 16:09:24
0
1
468

我的应用程序需要一些帮助。我有这个代码:

实时.php

$jogador = Jogador::getSaldoJogadorByEmail($email);
$premio = Premio::getValorPremioByTipo($tipo);
$participante = Participantes::getATotalParticipantesFromPA1();
$doc = new DOMDocument("1.0");
$action = $doc->createElement('action');
$action = $doc->appendChild($action);
$jogador = $doc->createElement('jogador1', $jogador);
$jogador = $action->appendChild($jogador);
$premio = $doc->createElement('premio1', $premio);
$premio = $action->appendChild($premio);
$participante = $doc->createElement('participante1', $participante);
$participante = $action->appendChild($participante);
$output = $doc->saveXML();
header("Content-type: application/xml");
echo $output;

我有这个函数,它只是 p assing 一个值,而不是传递数据库的所有内容:

static function getTotalParticipantesFromPA1(){
    $db = new mysqli("localhost","******","****","participantes");
    $query = "SELECT * FROM premioacumulado1";
    $result = $db->query($query);
    $row = $result->fetch_array(MYSQLI_ASSOC);
    
    if(mysqli_num_rows($result) > 0 ){
        while($row = $result->fetch_array(MYSQLI_ASSOC)){
        $id = $row['id'];
        $username = $row['username'];
        $m = "ID: " . $id . " " . "User: " . $username . "\n";
        return $m;
        } 
    }else{
        $db->close();
        return NULL;
    }
    
}

所有其他字段都在实时工作,但这个字段仅从数据库中获取一个值......

如何从数据库中获取所有值?

已编辑 - 2022 年 5 月 22 日

static function getTotalParticipantesFromPA1(){
    $db = new mysqli("localhost","*****","*****","participantes");
    $query = "SELECT * FROM premioacumulado1";
    $result = $db->query($query);
    $row = $result->fetch_array(MYSQLI_ASSOC);
    
    if(mysqli_num_rows($result) > 0 ){
        while($row = $result->fetch_array(MYSQLI_ASSOC)){
        $id = $row['id'];
        $username = $row['username'];
        $m = "ID: " . $id . " " . "User: " . $username . "\n";
        
        }
        return $m; // if i put it here it only returns the last value in the data base .
    }else{
        $db->close();
        return NULL;
    }
    
}

static function getTotalParticipantesFromPA1(){
    $db = new mysqli("localhost","*****","*****","participantes");
    $query = "SELECT * FROM premioacumulado1";
    $result = $db->query($query);
    $row = $result->fetch_array(MYSQLI_ASSOC);
    
    if(mysqli_num_rows($result) > 0 ){
        while($row = $result->fetch_array(MYSQLI_ASSOC)){
        $id = $row['id'];
        $username = $row['username'];
        $m = "ID: " . $id . " " . "User: " . $username . "\n";
        echo $m;// if i do a echo i get an error in the xml $participante variable(realtime.php) , maybe convert it here so it can take the result , but how ?
        } 
    }else{
        $db->close();
        return NULL;
    }
    
}

这是脚本 Js:

function getRealTime(){
  //retrieve the DOM objects to place content
  var domjogador1 = document.getElementById("saldo");
  var dompremiovaloracu1 = document.getElementById("valorActualPremioAcu1");
  var domparticipantes1 = document.getElementById("areaParticipantesAcu1");
  

  //send the get request to retrieve the data
  var request = new XMLHttpRequest();
  request.open("GET", "realtimegame.php", true);
  request.onreadystatechange = function(){
    if(request.readyState == 4 && request.status == 200){
      //parse the xml document to get each data element
      var xmldoc = request.responseXML;

      var xmljogador1 = xmldoc.getElementsByTagName("jogador1")[0];
      var jogador1 = xmljogador1.childNodes[0].nodeValue;

      var xmlpremio1 = xmldoc.getElementsByTagName("premio1")[0];
      var premio1 = xmlpremio1.childNodes[0].nodeValue;

      var xmlparticipante1 = xmldoc.getElementsByTagName("participante1")[0];
      var participante1 = xmlparticipante1.childNodes[0].nodeValue;

     

      domjogador1.innerHTML = jogador1;
      dompremiovaloracu1.innerHTML = premio1;
      domparticipantes1.innerHTML = participante1;
      
    }
  };
  request.send();
}

P粉563446579
P粉563446579

全部回复(1)
P粉947296325

这是有效的函数,现在有一个更漂亮的结果:

static function getBTotalParticipantesFromPA1(){
    $db = new mysqli("localhost","root","","participantes");
    $query = "SELECT * FROM premioacumulado1";
    $result = $db->query($query);
    $row = $result->fetch_array(MYSQLI_ASSOC);
    $s = "
"; $data1 = array(); if(mysqli_num_rows($result) > 0 ){ do{ $data1[] = $row['username'] . " " . $row['id'] . " "; }while($row = $result->fetch_array(MYSQLI_ASSOC)); return json_encode($data1); }else{ $db->close(); return NULL; } }

我仍然想打印一些更漂亮的东西供最终用户查看。这样我就得到了每个值中的换行符,但我仍然想从输出中删除“,”...如果有人有可以更好使用的东西,那么请发布它,谢谢。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!