?
Solved

How to Use PHP Array to Another SQL Query?

Posted on 2014-09-12
9
Medium Priority
?
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Expert Comment

by:Ray Paseur
ID: 40320041
What does the query say now?  Where do you want to put the variables?
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the 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 111

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 111

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

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

719 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