0

I want to have one query that will pull out image and string data out of my table in one call and send that information back to the server via an ajax call. I am using php as a back end. I guess that would bring up two questions. Since I am using php 5, the first question would be is php 5 object oriented. What I would do in C# or Java in a situation like this is create an object, with two fields. One for the CLOB data and the other for the pipe delimated string. Can I do that using php? How do you write object oriented code in php? Isn't php written in C++?

Any help with any of these things would be a great help to me.

hakre
  • 193,403
  • 52
  • 435
  • 836
SoftwareSavant
  • 9,467
  • 27
  • 121
  • 195

1 Answers1

1

PHP 5 (especially since 5.3) has very good support for objects. If you want to know how to work with PHP objects, I suggest you look at the official documentation, it is very good.

However, I do not think sending down an object to your ajax request is what you will want to do in the end. You most likely should serialize your data using json and send it back that way.

Heres a quick example

# Conncet to a database and run the query
$pdo = new PDO("mysql:host=your_host;dbname=your_db", "user", "password");
$stmt = $pdo->prepare("SELECT name, content FROM images WHERE id = ?");
$stmt->execute($_GET['image_id']);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$result = $result[0];
$result['content'] = base64_encode($result['content']);

# Send your name and content as a JSON result
echo json_encode($result);
Jimmy
  • 928
  • 2
  • 9
  • 17
  • http://stackoverflow.com/questions/1443158/binary-data-in-json-string-something-better-than-base64/3572114#3572114 What about this? – SoftwareSavant Nov 06 '11 at 20:55
  • Right. You'll have to base64 encode it, duh. I edited the answer. Anyhow, there might be a better way to achieve your goal (ie : avoiding storing images in a database) - it's hard to say without knowing your context et all. – Jimmy Nov 07 '11 at 13:54
  • hmmm. Interesting. I will give it a shot tonight after work. If this doesn't work, I will have to end up doing multiple queries. Shame. – SoftwareSavant Nov 07 '11 at 16:36