Solved

How to Use PHP Array to Another SQL Query?

Posted on 2014-09-12
9
294 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 109

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 109

Expert Comment

by:Ray Paseur
ID: 40320041
What does the query say now?  Where do you want to put the variables?
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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
 

Author Comment

by:sandshakimi
ID: 40320118
Clarify:

I want to sort by Contract Vehicles.
0
 
LVL 109

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 109

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

831 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