R: DB result and array

"Gaetano Giunta" <[email protected]>
Newsgroups gmane.comp.php.xml-rpc
Message-ID <[email protected]>
Uhm, the code looks somewhat broken, as far as I can tell.
Comments below (in italiano)

> -----Messaggio originale-----
> Da: [email protected]
> [mailto:[email protected]]Per conto di TeddyZ
> Inviato: lunedi 21 luglio 2003 22:27
> A: [email protected]
> Oggetto: [phpxmlrpc] DB result and array
> 
> 
> Hi, sorry for my english..i'm going to expose you what's my problem.
> I have client and server . the client call a method . this 
> method retrieve 
> values from a mysql db but i can't see these values. i'm very 
> newbie :|
> 
> here is my code, i thing i'm in error with the array of my 
> method...can you 
> help me please?
> sorry for the stupid question (i think..uhm..i'm sure)
> bye!
> 
> client.php
> 
> 
> include("xmlrpc.inc");
> $message=new xmlrpcmsg('examples.conn');
> $client=new xmlrpc_client("/test/server.php", "xmlrpc.datto.lan", 80);
> $response=$client->send($message);

innanzitutto ti conviene PRIMA testare $response per evedre che non sia null, POI il faultcode e INFINE recuperare il valore

> $value=$response->value();
> if (!$response->faultCode()) {
> 	print "Queste sono le sezioni trovate".
> 	$value->scalarval() . "<BR>";
> 	print "<HR>vedi:<BR><PRE>" .
> 	htmlentities($response->serialize()). "</PRE><HR>\n";
> } else {
> 	print "Fault: ";
> 	print "Code: " . $response->faultCode() .
> 		" Reason '" .$response->faultString()."'<BR>";
> }
> 
> 
> server.php
> 
> include("xmlrpc.inc");
> include("xmlrpcs.inc");
> 
> function conn() {
> 	mysql_connect('localhost','root');
> 	mysql_select_db('publish');
> 	$sql="select * from sezioni";
> 	$res=mysql_query($sql);
> 	$count=mysql_num_rows($res);
> 	for ($i=0;$i<$count;$i++){
> 		$row=mysql_fetch_array($res);
> 		$sezioni[]=new xmlrpcval($row['nome_sezione'],"array");

Qui crei un nuovo valore xmlrpc di tipo array e lo aggiungi a un array php.
Il problema e' che $row['nome_sezione'] non e' un array (la riga del db), ma solo il singolo campo 'nome_sezione' di tale array.
I casi sono 2:

Se volevi recuperare solo il nome sezione per spedirlo, avresti dovuto fare (supponendo che il nome sezione sia una stringa):

  $sezioni[] = new xmlrpcval($row['nome_sezione'], "string");

P.S. Inoltre tanto valeva nella query recuperare solo il nome sezione: con 'select nome_sezione from sezioni' hai meno dati traferiti tra DB e PHP e maggiore velocita' di esecuzione nella query nel DB.

Se invece volevi spedire tutta la riga relativa alla sezione, avresti dovuto scrivere
  $valori = array();
  foreach($row as $key=> $val)
    $valori[] = new xmlrpcval($val, "string"); // supponendo che tutti i campi vadano bene come stringa
  $sezioni[] = new xmlrpcval($valori, "array");

oppure 

  $valori = array();
  foreach($row as $key=> $val)
    $valori[$key] = new xmlrpcval($val, "string"); // supponendo che tutti i campi vadano bene come stringa
  $sezioni[] = new xmlrpcval($valori, "struct");

per utilizzare una xmlrpc struct invece di un array e tenerti i nomi dei campi.

NB: utilizzando foreach($row) recuperi tutti i valori di $row. Di default mysql_fetch_array crea un array con il doppio degli elementi desiderati, una volta con indice posizionale e una volta con indice nominale. Per avere i valori listati una volta sola per nome, dovresti fare mysql_fetch_array($res, MYSQL_ASSOC).

> 	}
> 	return new xmlrpcresp(new 
> xmlrpcval("$row[nome_sezione]", "array"));
> 
> }

ma qui ritorni un xmlrpcresp a cui hai aggiunto solo una variabile (di tipo stringa?) codificata come array xmlrpc!!!

forse quello che volevi fare era:
 	return new xmlrpcresp(new xmlrpcval($sezioni, "array"));

> 
> 
> $server=new xmlrpc_server( array("examples.conn" =>
> 				array("function" => "conn")));
> 
> 

Ciao, spero di essere stato d'aiuto
Gaetano
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.