Ver Mensaje Individual
  #3 (permalink)  
Antiguo 12/08/2015, 07:07
jabierreym
 
Fecha de Ingreso: agosto-2015
Ubicación: Santiago de Compostela
Mensajes: 8
Antigüedad: 8 años, 8 meses
Puntos: 1
Respuesta: Guardar datos de consulta en un Array

Muchas gracias por responder,

Tambien he probado con fetchcAll, tanto si lo hago como os habia enviado como con fetchAll me devuelve corrctamente el ARRAY. El problema es que no se como seguir, relmente con esto ya tengo el Array:

Array ( [id] => 8 [0] => 8 [username] => aaa [1] => aaa [password] => *A4B6157319038724E3560894F7F932C8886EBFCF [2] => *A4B6157319038724E3560894F7F932C8886EBFCF [firstName] => ASASA [3] => ASASA [lastName] => ASASA [4] => ASASA [joinDate] => 2015-08-05 [5] => 2015-08-05 [gender] => m [6] => m [favoriteGenre] => crime [7] => crime [emailAddress] => AAS [8] => AAS [otherInterests] => SASA [9] => SASA ) Array ( [id] => 6 [0] => 6 [username] => bigbill [1] => bigbill [password] => *6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4 [2] => *6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4 [firstName] => Bill [3] => Bill [lastName] => Swan [4] => Swan [joinDate] => 2007-06-11 [5] => 2007-06-11 [gender] => m [6] => m [favoriteGenre] => nonFiction [7] => nonFiction [emailAddress] => [email protected] [8] => [email protected] [otherInterests] => Tennis, judo, music [9] => Tennis, judo, music )

Pero para trabajar con estos datos deberia almacenarlo en un ARRAY PHP, no?


Os pongo todo el codigo del ejercicio. Es un ejemplo y lo que estoy intentando hacr es descifrar como funcona. Al estar escrito con Clases me cuesta un poco entenderlo por eso queria intentar hacerlo de una forma más rudimentaria para seguir...

DataObject.php


<?php

require_once "config.php";

abstract class DataObject {

protected $data = array(
"id" => "",
"username" => "",
"password" => "",
"firstName" => "",
"lastName" => "",
"joinDate" => "",
"gender" => "",
"favoriteGenre" => "",
"emailAddress" => "",
"otherInterests" => ""
);

public function __construct( $data ) {
foreach ( $data as $key => $value ) {
if ( array_key_exists( $key, $this->data ) ) $this->data[$key] = $value;
}
}

public function getValue( $field ) {
if ( array_key_exists( $field, $this->data ) ) {
return $this->data[$field];
} else {
die( "Field not found" );
}
}

public function getValueEncoded( $field ) {
return htmlspecialchars( $this->getValue( $field ) );
}

protected function connect() {
try {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$conn->setAttribute( PDO::ATTR_PERSISTENT, true );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
die( "Connection failed: " . $e->getMessage() );
}

return $conn;
}

protected function disconnect( $conn ) {
$conn = "";
}
}

?>

Member.class.php

<?php

require_once "DataObject.class.php";

class Member extends DataObject {



private $_genres = array(
"crime" => "Crime",
"horror" => "Horror",
"thriller" => "Thriller",
"romance" => "Romance",
"sciFi" => "Sci-Fi",
"adventure" => "Adventure",
"nonFiction" => "Non-Fiction"
);

public static function getMembers( $startRow, $numRows, $order ) {
$conn = parent::connect();
$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM " . TBL_MEMBERS . " ORDER BY $order LIMIT :startRow, :numRows";

try {
$st = $conn->prepare( $sql );
$st->bindValue( ":startRow", $startRow, PDO::PARAM_INT );
$st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
$st->execute();
$members = array();
foreach ( $st->fetchAll() as $row ) {
print_r($row);
}
$st = $conn->query( "SELECT found_rows() AS totalRows" );
$row = $st->fetch();
parent::disconnect( $conn );
return array( $members, $row["totalRows"] );
} catch ( PDOException $e ) {
parent::disconnect( $conn );
die( "Query failed: " . $e->getMessage() );
}
}

public static function getMember( $id ) {
$conn = parent::connect();
$sql = "SELECT * FROM " . TBL_MEMBERS . " WHERE id = :id";

try {
$st = $conn->prepare( $sql );
$st->bindValue( ":id", $id, PDO::PARAM_INT );
$st->execute();
$row = $st->fetch();
parent::disconnect( $conn );
if ( $row ) return new Member( $row );
} catch ( PDOException $e ) {
parent::disconnect( $conn );
die( "Query failed: " . $e->getMessage() );
}
}

public function getGenderString() {
return ( $this->data["gender"] == "f" ) ? "Female" : "Male";
}

public function getFavoriteGenreString() {
return ( $this->_genres[$this->data["favoriteGenre"]] );
}
}

?>