select from a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dotphp
Forum Commoner
Posts: 25
Joined: Sat Nov 10, 2007 8:03 am

select from a function

Post 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
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: select from a function

Post by fugix »

do you receive any errors?
dotphp
Forum Commoner
Posts: 25
Joined: Sat Nov 10, 2007 8:03 am

Re: select from a function

Post 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";
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: select from a function

Post 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) ?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: select from a function

Post 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;
}
dotphp
Forum Commoner
Posts: 25
Joined: Sat Nov 10, 2007 8:03 am

Re: select from a function

Post 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>';	
}


User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: select from a function

Post 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:

Code: Select all

$this->unu[] = $data[$col];
dotphp
Forum Commoner
Posts: 25
Joined: Sat Nov 10, 2007 8:03 am

Re: select from a function

Post by dotphp »

Thanks for help.
Post Reply