criostage Posted August 24, 2011 Share Posted August 24, 2011 Hello, i m doing a small site for personal use where i m using php and mysql as a database. I m still learning how to proper doing things so far so good but i been bashing my head against this small issue. I have mainly 1 class for all MySQL queries inserted into functions for example connect() will start the mysql connection uppon the Class/Object creation, safequery() will clear out the mysql injection problem, so on so on ... what i m trying to do, and its where i m stuck, is get a list (array) of data from MySQL to build an drop down menu, but i m not able to pass the total of the array values into the external piece of code that builds the combobox. If i do this i can get the list of all the values inside the array (witch are correct): function getanimestatus(){ $query = mysql_query("SELECT designation FROM agi_status"); while($result = mysql_fetch_array($query)){ echo $result['designation']."<br>"; } return $result; } so i changed the function to send out the array: in the class function getanimestatus(){ $query = mysql_query("SELECT designation FROM agi_status"); $result = mysql_fetch_array($query); return $result; } where i m calling the function in the class $mysql = new mysql($dbhost,$dbuser,$dbpwd); var_dump( $mysql->getanimestatus()); But for the reason i dont understand i only get the 1st value of the array. array(2) { [0]=> string(6) "Airing" ["designation"]=> string(6) "Airing" } Any help or tips would be deeply apreciated. Full class code: <?php # Classe que contem todas as operações relacionadas com a interação do site com o Servidor MySQL class mysql{ # Váriaveis necessárias para a ligação á base de dados MySQL private $dbname = 'phpMyAnimeList'; private $dbhost = ''; private $dbuser = ''; private $dbpwd = ''; private $dblink = ''; private $tblink = ''; # Váriaveis necessárias para as funções da classe private $saltkey = '15e1065fea'; private $safequeryvar = ''; private $safequeryvar2 = ''; # Quando objecto da class mysql é criado as variaveis $dbhost,$dbuser e $dbpwd são alimentadas pelos valores # definidos pelo utilizador no ficheiro config.php function __construct($host,$user,$pwd) { $this->dbhost = $host; $this->dbuser = $user; $this->dbpwd = $pwd; $this->connect(); } # Função de ligação ao servidor de MySQL e selecção da base de dados phpMyAnimeList function connect(){ $this->dblink=mysql_connect($this->dbhost, $this->dbuser, $this->dbpwd); if( !$this->dblink ){ die( "<b>Unable to connect to the database</b>:" . mysql_error()); $this->close(); }else{ $this->tblink = mysql_select_db($this->dbname,$this->dblink); if( !$this->tblink ){ die( "<b>Unable to select the database</b>:" . mysql_error()); $this->close(); } } } # Pequena função para prevenir mysql injection function safequery($input){ $safeinput = mysql_real_escape_string(stripslashes($input)); return $safeinput; } # Função para limpar o conteudo das variaveis caso já não sejam necessárias function cleanvar(){ unset($this->safequeryvar); unset($this->safequeryvar2); } # Termina a ligação ao servidor MySQL function close(){ mysql_close( $this->dblink ); } # Verifica se o utilizador e password's inseridos pelo utilizador estão na base de dados e se estão correctos function logincheck($username,$password){ $this->safequeryvar = $this->safequery($username); $this->safequeryvar2 = sha1(md5($this->safequery($password).$this->saltkey)); $query = mysql_query("SELECT username FROM usm_user WHERE Username='$this->safequeryvar' AND Password='$this->safequeryvar2'"); $rows = mysql_num_rows($query); if($rows == 1){ $this->cleanvar(); return $rows; }else{ $this->cleanvar(); return false; } } function setcookieid($idcookie,$username){ $this->safequeryvar = $this->safequery($username); mysql_query("UPDATE usm_user SET Cookieid='$idcookie' WHERE Username='$this->safequeryvar'"); $this->cleanvar(); } # Queries relacionadas com o Theme do site & user function setsitetheme($themeid){ $this->safequeryvar = $this->safequery($themeid); mysql_query("UPDATE wsm_theme SET `Default`='0' WHERE `Default`='1'"); mysql_query("UPDATE wsm_theme SET `Default`='1' WHERE `ThemeID`='$this->safequeryvar'"); $this->cleanvar(); return 0; } function setusertheme($themeid,$cookieid){ $this->safequeryvar = $this->safequery($themeid); $this->safequeryvar2 = $this->safequery($cookieid); mysql_query("UPDATE usm_user SET `ThemeID`='$this->safequeryvar' WHERE `Cookieid`='$this->safequeryvar2'") or DIE(mysql_error()); $this->cleanvar(); } function getusertheme(){ $query = mysql_query("SELECT wsm_theme.Designation FROM wsm_theme LEFT OUTER JOIN usm_user ON ( usm_user.ThemeID = wsm_theme.ThemeID ) WHERE usm_user.ThemeID = wsm_theme.ThemeID"); $result = mysql_fetch_array($query); return $result['Designation']; } function getsitetheme(){ $query = mysql_query("SELECT Designation FROM wsm_theme WHERE `Default` = '1'"); $theme = mysql_fetch_array($query); return $theme['Designation']; } # Funções para ir buscar informação de categorização e identificação dos titulos function getanimestatus(){ $query = mysql_query("SELECT designation FROM agi_status"); $result = mysql_fetch_array($query); return $result; } # Funções relativas aos insert's na base de dados function insertanime(){ } function insertfansub(){ } function insertcategory(){ } function showanime(){ } function showfansub(){ } function showcategory(){ } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245584-functions-returning-arrays-with-mysql-data/ Share on other sites More sharing options...
WebStyles Posted August 24, 2011 Share Posted August 24, 2011 if you're expecting several results, you need to loop through them: function getanimestatus(){ $res = array(); $query = mysql_query("SELECT designation FROM agi_status"); while($result = mysql_fetch_array($query)){ $res[] = $result['designation']; } return $res; } Quote Link to comment https://forums.phpfreaks.com/topic/245584-functions-returning-arrays-with-mysql-data/#findComment-1261354 Share on other sites More sharing options...
criostage Posted August 24, 2011 Author Share Posted August 24, 2011 Yes you are right, i just tested it out and the out put was perfect on the 1st try ... tyvm for the help i cant belive i didnt saw that one ..... ;p Quote Link to comment https://forums.phpfreaks.com/topic/245584-functions-returning-arrays-with-mysql-data/#findComment-1261360 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.