I just discover a shortcut with PHP and MySQL. First I`ll discuss my problem with you. My problem was I wanted to create a array in my database that would support all the categories my post would need.
For example this post is under the category PHP+MySQL and Tips & Tricks, and I don`t want to create a new table for that so what I did was put everything under 1 field kinda like an array on your database.
For example PHP+MySQL has an ID of 1 and Tips & Trick has and ID of 2. So when I save it it looks like this ,1,2,. Now the data type of this is varchar. So it would be saved in my database like that. So when I want to use that data to view all my categories I simply use the explode function. And since its a varchar after the using the explode function it would automatically be transformed to an integer which I can use as a foreign key to access the ID number on the category table
<?php
//Im using the example above under the variable example.
//Im using the explode function that would break the string every occurrence of a comma (,)
$example = `,1,2,`;
$temp=explode(`,`,$example);
//Use the sizeof function to get the array size of the array $temp
$size=sizeof($temp);
//Increase the $size due to excess of , in the string
$size--;
//I use connect.php to connect directly to my database. Its much easier that way.
include(`connect.php`);
//start with 1 because the string started with a comma.
for ($a=1; $a<$size; $a++)
{
//Your query.
$q="select * from category where cat_id like `$temp[$a]`;
$r=mysql_query($q);
$row=mysql_fetch_array($r,MYSQL_ASSOC);
//Output
echo $row[`cat_name`].`<br>`;
}
?>
By this code I simple broke the string $example to be used as a foreign key for my category table. Take note that the $example variable also came from my database which is my way of saving an array. Hope you get the idea!