Page 1 of 1
select from a function
Posted: Mon Apr 25, 2011 10:33 am
by dotphp
I want to make a select in a class:
Code: Select all
class test {
var $unu = array();
function __construct(){
$link = mysql_connect("", "", "") or die("Couldn't make connection.");
$db = mysql_select_db("", $link) or die("Couldn't select database");
//$this->unu = $textin;
}
function show($col){
$q = mysql_query("SELECT $col FROM categorie");
$qw = mysql_fetch_array($q);
$this->unu = $qw;
return $this->unu;
}
}
If I want to show how many rows are in that table with the following code:
Code: Select all
$box = new test();
echo count($box->show("titlu"));
it show me that are only 2, although in "categorie" table are 10 rows.
Can you please tell me where is the error ?
Thanks
Re: select from a function
Posted: Mon Apr 25, 2011 10:36 am
by fugix
do you receive any errors?
Re: select from a function
Posted: Mon Apr 25, 2011 11:06 am
by dotphp
I use
Code: Select all
error_reporting(E_ALL);
ini_set('display_errors', '1');
but no error.
It's show me only "2";
Re: select from a function
Posted: Mon Apr 25, 2011 11:14 am
by social_experiment
Code: Select all
<?php
function show($col){
$q = mysql_query("SELECT $col FROM categorie");
$qw = mysql_fetch_array($q);
$this->unu = $qw;
return $this->unu;
}
?>
Your function is incorrect. You are returning an array which contains (probably) the value for $col (in your example 'titlu') and the field value of 'titlu'.
dotphp wrote:If I want to show how many rows are in that table with the following code:
Do you want to show the amount of rows in the table (in total) ?
Re: select from a function
Posted: Mon Apr 25, 2011 11:18 am
by superdezign
mysql_fetch_array() will give you an array representation of the next row in your result resource. The first time you use it, you get the first row. mysql_fetch_array() is special in the sense that it gives you both an indexed array and an associative array in one. So, what you get is something along the lines of:
Code: Select all
Array(
[0] => 'some_value',
[titlu] => 'some_value',
)
What you want to do is get the every row from the result resource instead of one.
Code: Select all
function show($col){
$q = mysql_query("SELECT $col FROM categorie");
$qw = array();
while ($data = mysql_fetch_assoc($q)) $qw[] = $data; // This pulls all data instead of just one
$this->unu = $qw;
return $this->unu;
}
Re: select from a function
Posted: Mon Apr 25, 2011 1:06 pm
by dotphp
The function was good and I change a little bit
Code: Select all
function show($col){
$q = mysql_query("SELECT $col FROM categorie");
while ($data = mysql_fetch_assoc($q)) $this->unu[] = $data;
return $this->unu;
}
I try to show the value form column $col, but for each "echo" it show me array not the value from mysql. It's an array in array ?
Code: Select all
$box = new test();
//print_r ($box->show("titlu"));
foreach ($box->show("titlu") as $val){
echo $val . '<br>';
}
Re: select from a function
Posted: Mon Apr 25, 2011 2:00 pm
by McInfo
$val is an array because $data is an array. It looks like you want show() to return an array of values instead of an array of arrays, so make this modification:
Re: select from a function
Posted: Mon Apr 25, 2011 2:35 pm
by dotphp
Thanks for help.