Solved

How to Use PHP Array to Another SQL Query?

Posted on 2014-09-12
9
297 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 110

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 110

Expert Comment

by:Ray Paseur
ID: 40320041
What does the query say now?  Where do you want to put the variables?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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 110

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 110

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…

739 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