简体   繁体   English

JavaScript获取所有信息

[英]JavaScript get all information

I need to receive information from php, my first parameter is the number of lines it will get, the second one is the name, the third is the socket, all i have now is this : 我需要从php接收信息,我的第一个参数是它将获得的行数,第二个是名称,第三个是套接字,我现在所拥有的就是:

( JavaScript code ) JavaScript代码

    $.ajax({

        url: 'compatibilidades.php',
        data: { 
            socket: $("#board option:selected").attr('value') 
        },
        success: function(dadosRecebidos) {

            for (i = 0; i < dadosRecebidos.substr(0, dadosRecebidos.indexOf('_')) ; i++ ) {
                var selectorID = dadosRecebidos.substr(0, dadosRecebidos.indexOf('-'));
                dadosRecebidos = dadosRecebidos.slice(dadosRecebidos.indexOf('-'),999);

                var nome = dadosRecebidos.substr(1, dadosRecebidos.indexOf('<') - 1);
                dadosRecebidos = dadosRecebidos.slice(dadosRecebidos.indexOf('<'),999);                 

                var socket = dadosRecebidos.substr(1, dadosRecebidos.indexOf('>') - 1);
                dadosRecebidos = dadosRecebidos.slice(dadosRecebidos.indexOf('>'),999);

                console.log(selectorID);
            }               

        }   

    });}}

( php code ) php代码

<?php 

$socket = $_GET["socket"];

$bd = new PDO("mysql:host=localhost;dbname=gestao_utilizadores" , "root" , "");

$getComponentes = $bd->prepare("SELECT * FROM componentes WHERE ( Socket=:socket )");
$getComponentes->bindValue(':socket' , $socket);
$getComponentes->execute();
$resultado = $getComponentes->fetchAll();

For ($i = 0; $i < $getComponentes->rowCount() ; $i++) { 
        echo $getComponentes->rowCount() . "_" . $resultado[$i]["Tipo"] . "-" . $resultado[$i]["Nome"] . "<" . $resultado[$i]["Socket"] . "> "; 
}   

but it is only printing once 但是只打印一次

The PDO::rowCount is guaranteed to work with DELETE, INSERT and UPDATE queries. 保证PDO :: rowCount与DELETE,INSERT和UPDATE查询一起使用。 It may work properly with SELECT on some databases but you shouldn't really rely on this. 在某些数据库上,它可能可以与SELECT一起正常工作,但您不应真正依赖于此。

The PDO::fetchAll funtion returns an array of rows (or FALSE if there's been an error) so if you need to know the count of rows returned simply use the count($resultado) or add COUNT(*) call in your query. PDO :: fetchAll函数返回一个行数组(如果发生错误,则返回FALSE),因此,如果您需要知道返回的行数,只需在查询中使用count($ resultado)或添加COUNT(*)即可。 If you don't need to know the exact count but only to perform some operations on each row, the Php foreach function is your friend, as mentioned by @Julio Soares 如果您不需要知道确切的计数而只需要对每一行执行一些操作,那么@@ liolio Soares提到了Php foreach函数是您的朋友。

UPDATE: It seems I didn't quite understood the problem - anyway, the first part of my answer still stands. 更新:看来我不太了解这个问题-无论如何,我回答的第一部分仍然有效。 Now, addressing the real problem - it looks for me like you're overcomplicating a simple thing. 现在,解决真正的问题-在我看来,您正在使一件简单的事情变得过于复杂。 Have you considered using JSON as the data format? 您是否考虑过使用JSON作为数据格式? It's very straightforward to json_encode an array and later to have it as an js object/array, so you can easily access each column from each row without any hassle. 对数组进行json_encode并随后将其作为js对象/数组非常简单,因此您可以轻松地访问每一行的每一列而无任何麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM