Solved

How to Use PHP Array to Another SQL Query?

Posted on 2014-09-12
9
296 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

820 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