Page 1 of 1

function getTableData($table){}

Posted: Sun Dec 22, 2013 7:53 am
by zerolinux
hey guys..
Im trying to create a function that gets any table's data.. I dont seem to code it logically..

Code: Select all


	function getTable($table) 
	{
		//fetch data
		$sql  = "SELECT * FROM " . $table;
		$q 	  = mysql_query($sql);

		while($row = mysql_fetch_array($q))
		{
			echo $row[0] . "<br>";
			echo $row[1] . "<br>";
		}
	}

I need help with the logic of replacing $row[0] / $row[1] for something more dynamic based on the table's row number.. and I can't seem to figure it out..
any tips would be much appreciate it!

Re: function getTableData($table){}

Posted: Sun Dec 22, 2013 8:53 am
by Celauran
First of all, don't use the mysql extension. It's deprecated and generally terrible and will be removed from PHP. Look at PDO instead. Secondly, why is your function echoing values (and line breaks!) rather than returning an array?

Re: function getTableData($table){}

Posted: Mon Dec 23, 2013 5:18 am
by stanliwise
I think you should use.while loop and a variable increment e.g


Code: Select all

$i = 0;
While(-------) {
echo $row[$i].'<br/>';
//increment I for every loop
$i++;
}

That's a dynamic loop;