Page 1 of 1

display data from 2 tables.

Posted: Thu Mar 25, 2004 9:21 am
by nevermiss9
I'm new too php.
ok here is my problem when i do a sql query i get the right data display on the screen.
but when i use the sql in php code for some reason it doesn't display the corrected data I needed. i cannot use tbl_products.fld_name(tablename n fieldname together) to point to that field in that table, it comes out blank. but if i use fld_name, it will only show the data from categories table for it. both table have the same field name. I don't want to change the field name for both table to fix this error. Is there any other method.

what I'am doing wrong here.?

Code: Select all

<?php
	require("config.php");
	$dbh1 = mysql_connect($dbhost, $dbuser, $dbpasswd) or die("Could not connect"); 
	mysql_select_db($dbname, $dbh1) or die("Could not select database"); 
	
	$result = mysql_query("SELECT tbl_products.fld_prodid, tbl_products.fld_barcode, tbl_products.fld_name, tbl_products.fld_description, tbl_products.fld_price, tbl_categories.fld_name, tbl_products.fld_manuid FROM tbl_products, tbl_categories WHERE tbl_products.fld_catid = tbl_categories.fld_catid");
	
	while ($row = mysql_fetch_array($result)) 
	{
		printf ("ID: %s  Barcode: %s Name: %s Des: %s Price: %s catid: %s manuid: %s", $row["'tbl_products.fld_prodid"], $row["fld_barcode"], $row["fld_name"],$row["fld_description"], $row["fld_price"], $row["fld_name"], $row["fld_manuid"]);
		echo "<br>";
	}

	mysql_free_result($result);

?>
Basical all I'm trying do here is if tbl_product.fld_catid=tbl_categoires.fld_catid
then get tbl_categories.fld_name to display with the rest of tbl_products data.

Re: display data from 2 tables.

Posted: Thu Mar 25, 2004 10:53 am
by TheBentinel.com
Use the AS keyword to give a temporary name to the field:

Code: Select all

<?php
	$result = mysql_query("SELECT tbl_products.fld_prodid, tbl_products.fld_barcode, tbl_products.fld_name AS PROD_NAME, tbl_products.fld_description, tbl_products.fld_price, tbl_categories.fld_name AS CAT_NAME, tbl_products.fld_manuid FROM tbl_products, tbl_categories WHERE tbl_products.fld_catid = tbl_categories.fld_catid");
	
	while ($row = mysql_fetch_array($result)) 
	{
		printf ("ID: %s  Barcode: %s Name: %s Des: %s Price: %s catid: %s manuid: %s", $row["'tbl_products.fld_prodid"], $row["fld_barcode"], $row["PROD_NAME"],$row["fld_description"], $row["fld_price"], $row["CAT_NAME"], $row["fld_manuid"]);
		echo "<br>";
	}

	mysql_free_result($result);

?>
Does that do what you need?

Posted: Thu Mar 25, 2004 3:52 pm
by nevermiss9
thanks you.
that was all i needed to make it work.