ajax调用中的图像和字符串数据


Image and string data in an ajax call?

我想有一个查询,将拉出图像和字符串数据从我的表在一个调用,并通过ajax调用发送该信息回服务器。我使用php作为后端。我想这会引出两个问题。因为我使用php 5,第一个问题将是php 5是面向对象的。在这种情况下,我在c#或Java中会做的是创建一个对象,带有两个字段。一个用于CLOB数据,另一个用于管道分隔的字符串。我能用php实现吗?如何在php中编写面向对象的代码?php不是用c++写的吗?

这些事情的任何帮助对我都是很大的帮助。

PHP 5(特别是从5.3开始)对对象有很好的支持。如果你想知道如何使用PHP对象,我建议你看一下官方文档,它非常好。

然而,我不认为发送一个对象到您的ajax请求是什么,你会想要做的最后。你很可能应该使用json序列化你的数据,并以这种方式发回。

这里有一个简单的例子

# 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);