function getTableData($table){}

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
zerolinux
Forum Newbie
Posts: 1
Joined: Sun Dec 22, 2013 7:39 am

function getTableData($table){}

Post 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!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: function getTableData($table){}

Post 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?
stanliwise
Forum Newbie
Posts: 5
Joined: Mon Dec 23, 2013 5:00 am

Re: function getTableData($table){}

Post 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;
Post Reply