Page 1 of 1

Associating Colums and tables

Posted: Fri Jun 16, 2006 10:18 am
by franknu
Ok, I have a database and i want to associate colums to be display when a user type in something in categories. I created a diffrent table for almost everything. I was just wondering if i have to create a full table with the information i want to associate or if it is ok to have in diffrent tables

here is the code

Code: Select all

<?php 
print_r($_POST); 

if(isset($_POST['submit'])) 


$host = "localhost"; 
$username = "localhost"; 
$password = "abc123"; 
$database = "contacts"; 

if(isset($_POST['Categories'])) 
{ 
    $Categories = trim(addslashes($_POST['Categories'])); 
} 
else 
{ 
    echo 'No Category Passed'; 
    exit; 
} 

$db = mysql_connect($host, $username, $password); 
mysql_select_db($database); 
$query = "SELECT * FROM Categories WHERE Categories LIKE '%$Categories%'"; 
$result = mysql_query($query) or die (mysql_error()); 
$num_result = mysql_num_rows($result); 

if($num_result == 0) 
{ 
    echo("No Results found."); 
    exit; 
} 

echo "<p>Number of business found: $num_result </p>"; 
while($row = mysql_fetch_assoc($result)) 
{ 
     echo '<p><strong>Business Name (' . stripslashes($row['BusinessName']) . ')'; 
     echo '(' . $row['slogan'] . ')</strong><br />'; 
     echo 'Address (' . stripslashes($row['BusinessAddress']) . ')<br />'; 
     echo 'State (' . stripslashes($row['make']) . ')<br />'; 
     echo 'City (' . stripslashes($row['type']) . ')<br />'; 
     echo 'Tel (' . stripslashes($row['tel']) . ')<br /></p>'; 
} 

?>
this is what i am displaying

Array ( [make] => Massachusetts [type] => Lawrence [Categories] => printing [textfield] => [submit] => Search )
Number of business found: 1

BusinessName ()()
Address ()
State ()
City ()
Tel ()

i have a business name MASS AD
Address 19 Central St
In MAKE MASSACHUSETTS
TYPE LAWRENCE
978.327.5139

how can i make like that for now it is just showing that i have a category Name "Printing"

Posted: Fri Jun 16, 2006 10:48 am
by John Cartwright
If I understood correctly, you want to join the information of your "categories" from another table. You should then look up INNER JOIN in the mysql manual, and join the two tables likely by the primary key.

Posted: Fri Jun 16, 2006 2:36 pm
by franknu
isnt it better then if i just build everything in one table

Posted: Fri Jun 16, 2006 3:07 pm
by printf
No, example, just because you have a user table, does not mean you would store topics the user made in the user table! Table relationships is a powerful tool that should never be neglected! What is important in doing this is that you maintain the correct indexing and the correct column types that your relationship key can relate to. In explaining it, it can seem hard to understand, but once you do a few cross joins with a union, you will understand so it makes more sense. But remember this, just because a database gives you shortcuts like a scripting language does, does not mean you should ever use them, because when you do, you will never fully see with your eyes eactly what the code is doing. Which makes it more difficult to learn.

What I am trying to say, is just because you can do....

//PHP

Code: Select all

if ($foo == $bar) //do stuff

// or

if ($foo == $bar)
//do stuff
instead of writing it fully out...

Code: Select all

if ( $foo == $bar )
{
	// do stuff
}

Doesn't mean you should ever do it. This goes for databases to. Sure the object is doing what you want, but maintaining a good standard will make your code easier to follow, which makes it much easier to understand. If you want a example of doing lame shortcuts in a query, compared to writing it out so it can be easily understood, tell me and I will give you one!


pif!