Solved

How to Use PHP Array to Another SQL Query?

Posted on 2014-09-12
9
291 Views
Last Modified: 2014-09-13
Using a web service call, my response comes back as an array:

 stdClass Object ( [contractVehicle] => Array ( [0] => ITSchedule70 [1] => ITCommodityProgram ) )

I then need to use this in another SQL query to get results from other data.

1) How do I port this Object code over to a SQL query?
2) Should I, or do I need, to change the Object name? Or  keep it as is? Does it matter?
0
Comment
Question by:sandshakimi
  • 5
  • 4
9 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40319728
The object name does not matter, so long as you use it consistently.  Here is the data that created that object and some ways to use it.

<?php // demo/temp_sandshakimi.php
error_reporting(E_ALL);

// stdClass Object ( [contractVehicle] => Array ( [0] => ITSchedule70 [1] => ITCommodityProgram ) )

// CREATE THE ARRAY AND PUT IT INTO THE OBJECT
$arr = array();
$arr[0] = 'ITSchedule70';
$arr[1] = 'ITCommodityProgram';
$obj = new StdClass;
$obj->contractVehicle = $arr;

// SHOW THE DATA
print_r($obj);

// SHOW HOW TO ACCESS ONE OF THE DATA ELEMENTS
echo PHP_EOL . $obj->contractVehicle[0];

// SHOW HOW TO ACCESS ALL OF THE DATA ELEMENTS
foreach ($obj->contractVehicle as $str)
{
    echo PHP_EOL . $str;
}

Open in new window

0
 

Author Comment

by:sandshakimi
ID: 40320026
That's great. How do I insert this into a SQL query. I use phpMyAdmin to test my queries.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40320041
What does the query say now?  Where do you want to put the variables?
0
 

Author Comment

by:sandshakimi
ID: 40320116
Simple one I'm testing with:

SELECT * FROM PRODUCT ORDER BY product_id ASC

A complex one from my DBA (the one I will actually deploy on production)

SELECT P.product_desc, P.manuf_part_nbr, V.vendor_name from CONFIGURED_PRODUCT CP LEFT JOIN CONFIGURED_PRODUCT_HAS_PRODUCT_OPTION CPPOX ON CPPOX.configured_product_id = CP.configured_product_id LEFT JOIN PRODUCT P ON CP.product_id = P.product_id LEFT JOIN VENDOR V ON CP.vendor_id =V.vendor_id LEFT JOIN CONTRACT C ON CP.contract_id = C.contract_id LEFT JOIN PRODUCT_OPTION POM ON CPPOX.product_option_id = POM.product_option_id AND POM.option_name = 'Memory' LEFT JOIN PRODUCT_OPTION POD ON CPPOX.product_option_id = POD.product_option_id AND POD.option_name = 'Display Size' LEFT JOIN PRODUCT_OPTION POC ON CPPOX.product_option_id = POC.product_option_id AND POC.option_name = 'Processor
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:sandshakimi
ID: 40320118
Clarify:

I want to sort by Contract Vehicles.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40320143
Those queries do not have any place to put the information from the PHP object.  They have no variables or prepared-statement placeholders.

When you want to sort by anything in a query results set, you use the ORDER BY clause.

If you're new to PHP and MySQL, this article can help you find some good learning resources, both in books and online.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 

Author Comment

by:sandshakimi
ID: 40320159
Is there some a code example that help guide me? Don't have time to sift through a lot of documentation.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40320440
Don't have time to sift through a lot of documentation.
Yeah, I understand.  Good luck.
0
 

Author Comment

by:sandshakimi
ID: 40321366
Ray, the documents at the link were good.

I formulated a new question based on that.

Please consider: http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/Q_28517944.html
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now