0

as a php noob im teaching myself but have hit a little problem and could use a helping hand please.

I'm currently making a query to tbl_products to grab info as follows:

$mysql_hostname = "localhost";
$mysql_user     = "hidden";
$mysql_password = "hidden";
$mysql_database = "hidden";
$bd             = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Oops some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Oops some thing went wrong");// we are now connected to database

$result = mysql_query("SELECT * FROM tbl_products ORDER BY bankIds"); // selecting data through mysql_query()

while($data = mysql_fetch_array($result))
{

echo ''.$data['bankIds'].'|'.strip_tags($data['sku']).'|'.strip_tags($data['productTitle']).'|'.strip_tags($data['productTitle']).'|'.strip_tags($data['prodDesc']).'|'.strip_tags($data['seed']).',http://cdn.shopify.com/s/files/1/0709/2915/files/'.strip_tags($data['productImg']).'<br/>';
}

That works fine, but now i want to display the name of ['bankIds'] by making another query (if needed) to the table named "tbl_seedBank".

  • tbl_seedBank
  • id
  • bankTitle

At the moment "$data['bankIds']" will echo out the id number, i want to be able to grab the bankTitle from the id and echo it out instead of the id number...

Hope this makes sencse lol

Thank you so much for taking the time to read about my problem. ~ Rory

user1263909
  • 155
  • 9

2 Answers2

1

you can use the Inner Join for it.. try by executing below query

 $result = mysql_query("SELECT a.bankIds,a.sku,a.productTitle,a.productTitle,a.prodDesc,a.seed ,a.productImg,b.bankTitle FROM tbl_products a JOIN tbl_seedBank b ON a.bankIds=b.id ORDER BY bankIds");
Sarath
  • 2,318
  • 1
  • 12
  • 24
  • Ill try this, but could you also tell me how to echo the value out? – user1263909 Nov 29 '14 at 14:43
  • echo ''.$data['bankIds'].'|'.strip_tags($data['sku']).'|'.strip_tags($data['productTitle']).'|'.strip_tags($data['productTitle']).'|'.strip_tags($data['prodDesc']).'|'.strip_tags($data['seed']).',http://cdn.shopify.com/s/files/1/0709/2915/files/'.strip_tags($data['productImg']).'|'.strip_tags($data['bankTitle']).'
    ';
    – Sarath Nov 29 '14 at 14:46
  • So i would not need to use an if statement saying if bankids on tbl_products is qual to id on tbl_seedBank ? – user1263909 Nov 29 '14 at 14:51
  • no need of that please try to learn the uses of JOINS @user1263909 if the information is useful accept it .. http://stackoverflow.com/a/38578/3164682 – Sarath Nov 29 '14 at 14:53
  • So if i understand correctly i need to join the id from tbl_seedbank to bankids from tbl_products ? – user1263909 Nov 29 '14 at 15:05
  • yes sure.. try executing my query in your querybrowser.. you will get the list of rows @user1263909 .. if my answer is usefull please accept it – Sarath Nov 29 '14 at 15:07
  • GOT IT! Thank you so much for the help. I will be copying this to my clipboard for future use :) Thanks again Sarath! – user1263909 Nov 29 '14 at 15:09
  • Now that is sorted, i would like to add another final table that has variants relating to product id. By inner joining: ~ tbl_Bundles ~~ id ~~ productId ~~ bundleQty ~~ bundlePrice ~~ bundleSKU ~~ seed Tbl_bundles refers to product id to assign options to the product. So basically i need to match the product_id from tbl_bundles to the id on tbl_products and be able to echo out the info displayed above.. Does this make sense? – user1263909 Nov 30 '14 at 11:31
0

if i understand correct you need to make a join in your query

SELECT * FROM tbl_products ORDER BY bankIds INNER JOIN tbl_seedBank ON tbl_seedBank = bankIds

see more: http://dev.mysql.com/doc/refman/5.0/en/join.html

gmlv
  • 338
  • 4
  • 10