I have an Android application that takes a photo, converts the bitmap to Base64 and submits the Base64 string to a MySQL database (via PHP) to be stored as a longblob. This part works great! In fact, I can download the longblob from phpMyAdmin as a perfect Base64 string and easily convert to a JPEG photo.
The problem is my PHP code for getting the blob returns an empty string:
{
"owner":"Unknown",
"pet_name":"Unknown",
"last_seen":"2019-04-09 11:17:19",
"contact":"999-888-7654",
"description":"rubber ducky, lotsa fun",
***"photo":""***,
"location":"Some location"
}
The PHP getter:
function getReports() {
$stmt = $this->con->prepare("SELECT owner, pet_name, last_seen, contact, description, photo, location FROM Pets");
$stmt->execute();
$stmt->bind_result($owner, $pet_name, $last_seen, $contact, $description, $photo, $location);
$reports = array();
while($stmt->fetch()) {
$report = array();
$report['owner'] = $owner;
$report['pet_name'] = $pet_name;
$report['last_seen'] = $last_seen;
$report['contact'] = $contact;
$report['description'] = $description;
$report['photo'] = $photo;
$report['location'] = $location;
array_push($reports, $report);
}
return $reports;
}
An interesting side note, if instead of the above code I use the below code I DO get the complete Base64 string, but with added escape () and newline (\n) characters all throughout:
//Select everything from table
$sql= "SELECT * FROM Pets";
//Confirm results
if($result = mysqli_query($con, $sql)) {
//Results? Create array for results and array for data
$resultArray = array();
$tempArray = array();
//Loop through results
while($row=$result->fetch_object()) {
// Add each result in results array
$tempArray=$row;
array_push($resultArray,$tempArray);
}
//Encode array to JSON and output results
echo json_encode($resultArray);
}
I would like to find a way to fix the above PHP code. I'm thinking perhaps my string is too long for the $photo value? Any advice would be extremely appreciated.
Update: from Selecting Blob from MYSQL, getting null I did manage to the Base64 to output now instead of an empty string. however, I still have the problem of the escape and newline characters.
Any help here?
mysqliis significantly less verbose, making code easier to read and audit, and is not easily confused with the obsoletemysql_queryinterface where missing a singleican cause trouble. Example:$db = new mysqli(…)and$db->prepare("…")The procedural interface is largely an artifact from the PHP 4 era whenmysqliAPI was introduced and should not be used in new code.it still contains the escape and newline chars- most likely it's because you put it in that form into the database in first place.