Getting empty response, If blob field is there [PHP] - php

I have beem retrieving the data from mysqli db by php.
$query = "SELECT id, parentId, firstName, middleName, lastName, mobileNumber, photo FROM person WHERE orgId='".$orgId."' ";
$sql = mysqli_query($con, $query);
$data=array();
if($rows_count!=0){
while ($rows_fetch = mysqli_fetch_assoc($sql))
{
var_dump($rows_fetch);
$info = $rows_fetch;
array_push($data, $info);
$isSuccessful = true;
$code = 200;
}
}else{
$isSuccessful = true;
$code = 202;
}
$response = array("resp"=>$data, "code"=>$code, "isSuccessful"=>$isSuccessful);
If blob field is empty, I am getting response. If blob is there, I am getting empty response

while ($rows_fetch = mysqli_fetch_assoc($sql))
{
//var_dump($rows_fetch);
$info = $rows_fetch;
$info['photo'] = base64_encode($rows_fetch['photo']);
array_push($data, $info);
$isSuccessful = true;
$code = 200;
}
I have changing blob to base64 code.

Related

Import a single CSV file into multiple tables in MySQL

I would like to import a single CSV with 7 columns into 2 tables in MySQL.
Columns 1, 2 and 3 go into a single row in table1. Columns 4 and 5 go as a row in table2. Columns 6 and 7 go as a row again in same table2.
How can this be done using PHP or mysql directly?
First fetch all values from csv and store it in an array. Then maintain your array as per your requirement and then start looping and inserting.
<?php
$efected = 0;
$file_temp = $_FILES["file"]["tmp_name"];
$handle = fopen($file_temp, "r"); // opening CSV file for reading
if ($handle) { // if file successfully opened
$i=0;
while (($CSVrecord = fgets($handle, 4096)) !== false) { // iterating through each line of our CSV
if($i>0)
{
list($name, $gender, $website, $category, $email, $subcat) = explode(',', $CSVrecord); // exploding CSV record (line) to the variables (fields)
//print_r($subcat); sub_cat_ids
if($email!='')
{
$chk_sql = "select * from employees where employee_email='".$email."'";
$chk_qry = mysqli_query($conn, $chk_sql);
$chk_num_row = mysqli_num_rows($chk_qry);
$cat_array = array(trim($category));
$subcat_array = explode( ';', $subcat );
if(count($subcat_array)>0)
{
$subcat_array_new = array();
foreach($subcat_array as $key => $val)
{
$subcat_array_new[] = trim($val);
}
}
$cat_sub_cat_merge = array_merge($cat_array, $subcat_array_new);
$cat_subcat_comma = implode( "','", $cat_sub_cat_merge );
foreach($cat_array as $cat_key => $cat_val)
{
$chk_cat_sql = "select * from employee_cats where cats_name = '".$cat_val."'";
$chk_cat_qry = mysqli_query($conn, $chk_cat_sql);
$chk_cat_num_row = mysqli_num_rows($chk_cat_qry);
//$all_cat_array = array();
if($chk_cat_num_row == 0)
{
$new_cat_ins = "insert into employee_cats set cats_name = '".$cat_val."', parent_cat_id = '0' ";
$new_cat_ins_qry = mysqli_query($conn, $new_cat_ins);
}
}
foreach($subcat_array_new as $subcat_key => $subcat_val)
{
$chk_subcat_sql = "select * from employee_cats where cats_name = '".$subcat_val."'";
$chk_subcat_qry = mysqli_query($conn, $chk_subcat_sql);
$chk_subcat_num_row = mysqli_num_rows($chk_subcat_qry);
//$all_cat_array = array();
if($chk_subcat_num_row == 0 && trim($subcat_val)!='')
{
//$category
$get_catid_sql = "select * from employee_cats where cats_name = '".trim($category)."'";
$chk_catid_qry = mysqli_query($conn, $get_catid_sql);
$fetch_cat_info = mysqli_fetch_array($chk_catid_qry);
$fetch_cat_id = $fetch_cat_info['cats_id'];
$new_subcat_ins = "insert into employee_cats set cats_name = '".$subcat_val."', parent_cat_id = '".$fetch_cat_id."' ";
$new_subcat_ins_qry = mysqli_query($conn, $new_subcat_ins);
}
}
$get_cat_sql = "select * from employee_cats where cats_name in ('".$cat_subcat_comma."')";
$get_cat_qry = mysqli_query($conn, $get_cat_sql);
$get_cat_num_row = mysqli_num_rows($get_cat_qry);
$sub_category_ids = array();
if($get_cat_num_row>0)
{
while($fetch_cat_id = mysqli_fetch_array($get_cat_qry))
{
if($fetch_cat_id['parent_cat_id']==0)
{
$category_id = $fetch_cat_id['cats_id'];
}
else
{
$sub_category_ids[] = $fetch_cat_id['cats_id'];
}
}
$sub_cat_id_vals_comma = implode(",", $sub_category_ids);
}
else
{
$category_id = 0;
$sub_cat_id_vals_comma = "";
}
if($chk_num_row>0)
{
// and here you can easily compose SQL queries and map you data to the tables you need using simple variables
$update_sql = "update employees set
employee_name='".$name."',
employee_gender='".$gender."',
employees_website='".$website."',
employees_cat_id='".$category_id."',
sub_cat_ids='".$sub_cat_id_vals_comma."'
where employee_email='".$email."'";
$mysqli_qry = mysqli_query($conn, $update_sql);
}
else
{
// and here you can easily compose SQL queries and map you data to the tables you need using simple variables
$insert_sql = "insert into employees set
employee_name='".$name."',
employee_gender='".$gender."',
employees_website='".$website."',
employees_cat_id='".$category_id."',
sub_cat_ids='".$sub_cat_id_vals_comma."',
employee_email='".$email."'";
$mysqli_qry = mysqli_query($conn, $insert_sql);
}
$efected = 1;
}
}
$i++;
}
fclose($handle); // closing file handler
}
if($efected==1)
{
header('location:'.$site_url.'?import=success');
}
else
{
header('location:'.$site_url.'?import=failed');
}
?>

"array_push() expects parameter 1 to be array" error message

I'm working on an Activation Queue for new accounts on my website.
The system will work by iterating through an array with each new user account details in and then display them so the Admin can either accept or deny the account. To collect the account details and display them in an array I'm using the following piece of code, however I get the error "array_push() expects parameter 1 to be array, null given". I have no clue why this is being caused and I have tried various things previously suggested. Thanks in advance.
<?php
session_start();
require "classes.php";
$TF = new TF_Core ();
$ActQueueQuery = "SELECT username, surname, forename, joined FROM users
WHERE rank = 'Unactivated'";
if ($statement = TF_Core::$MySQLi->DB->prepare($ActQueueQuery)) {
$statement->execute();
$results = $statement->get_result();
}
if($results->num_rows == 0){
$data = 1;
}
else{
$_SESSION["ActQueue"] = "";
while($row = $results->fetch_assoc()){
$_SESSION["ActQueue"] = array_push($_SESSION["ActQueue"], array($row["username"], $row["surname"], $row["forname"], $row["joined"]));
}
$data = 0;
}
echo $data;
?>
<?php
session_start();
$_SESSION["ActQueue"] = array(); // define an empty array
require "classes.php";
$TF = new TF_Core ();
$ActQueueQuery = "SELECT username, surname, forename, joined FROM users
WHERE rank = 'Unactivated'";
if ($statement = TF_Core::$MySQLi->DB->prepare($ActQueueQuery)) {
$statement->execute();
$results = $statement->get_result();
}
if($results->num_rows == 0){
$data = 1;
}
else{
while($row = $results->fetch_assoc()){
$_SESSION["ActQueue"][] = array($row["username"], $row["surname"], $row["forname"], $row["joined"]); // check the change
}
$data = 0;
}
echo $data;
?>
"You are overwriting $arr_foundits on $arr_foundits = array_push($arr_foundits, $it->ID);. Remove the $arr_foundits = as array_push does not return the array but an int." Musa

PHP JSON generation problems

The code below should send back an json with informations in a Database.
It takes two parameters grade and subject. The problem ist, when I use parameters are not in the database behind everything works as expected no entry, but if it would get an answer from the database nothing appears. I mean really nothing. The values i need are there i tried this and no errors are logged into the logging file. As server runs apache2 with php5.6.22 on Debian. I don't know what i did wrong. Hopefully someone can help me.
The Code:
case 'get_books':
$grade = $_GET['grade'];
$subject = $_GET['subject'];
$sqlt = "SELECT * FROM book_type WHERE subject=".$subject." AND grade=".$grade;
$sql = mysqli_query($db, $sqlt);
if(!$sql){
print(json_encode(array('response' => 2)));
die();
}
$response = array();
$response['books'] = array();
while($row=mysqli_fetch_assoc($sql)) {
$book = array();
$book['fullname'] = $row ["fullname"];
$book['ISBN'] = $row ["ISBN"];
$book['id'] = $row ["id"];
array_push($response['books'], $book);
}
$response['response'] = "1";
print(json_encode($response));
die();
I think this might be your problem:
array_push($response['books'], $book);
as far as I know you can't push a variable into a specific index of an array since no key is provided for the item being pushed.
It would be better to do this as follows:
case 'get_books':
$grade = $_GET['grade'];
$subject = $_GET['subject'];
$sqlt = "SELECT * FROM book_type WHERE subject=".mysqli_real_escape_string((htmlspecialchars_decode($subject, ENT_QUOTES)))." AND grade=".mysqli_real_escape_string((htmlspecialchars_decode($grade, ENT_QUOTES)));
$sql = mysqli_query($db, $sqlt);
if(!$sql){
print(json_encode(array('response' => 2)));
die('sql failed');
}
$response = array();
$response['books'] = array();
$response['validator'] = 'valid';
$i = 0;
while($row=mysqli_fetch_assoc($sql)) {
$book = array();
$book['fullname'] = $row["fullname"];
$book['ISBN'] = $row["ISBN"];
$book['id'] = $row["id"];
$response['books'][$i] = $book;
$i++;
}
$response['response'] = "1";
var_dump($response);
//echo json_encode($response);
die();

Sending Variable with Post without formular

I execute some PHP code .
After this, I want to send a value over post to another PHP site, which I want use it via $_POST. I don't need sessions or cookies or anyway.
else if ($tag == 'in_accepted') {
$coni=mysqli_connect("localhost","LOGIN","PW","movement");
$tablename = $_POST['tablename'];
$jsonString = $_POST['json'];
$jArray = json_decode($jsonString, true);
$comment = $_POST['comment'];
$result2 = mysqli_query($coni, "ALTER TABLE `$tablename` COMMENT = '$comment'");
$response["comment_succes"] = 1;
foreach($jArray as $key => $value){
$ean = $value["ean"];
//$trimmedean = substr($ean, 0, 3) .".". substr($ean, 3, 4) .".". substr($ean, 7, 4);
//$result = mysqli_query($coni,"INSERT INTO `$tablename` (unique_id, ean, accepted) VALUES ('$uuid', '$trimmedean', '0')"); // or die(mysql_error());
$result = mysqli_query($coni, "UPDATE `$tablename` SET accepted='1' where ean='$ean'");
}
if ($result > 0) {
$response["success"] = 1;
!!!!SEND $tablename to URL.php
echo json_encode($response);
}
else {
$response["error"] = 1;
echo json_encode($response);
}
Use CURL to post the data to the specified url.
Make a dynamic form and submit it with the js script/jquery automatically.

how to json encode with db oracle 10g?

this is my code :
<?php
$response = array();
include("konek.php");
$result = "SELECT NAMA_RS, ALAMAT, LATITUDE, LONGITUDE FROM RUMAH_SAKIT";
$statement = oci_parse($c, $result);
oci_execute($statement, OCI_DEFAULT);
if (oci_num_rows($statement) > 0) {
$response["daftar_rs"] = array();
while ($row = oci_fetch_array($statement)) {
$daftar_rs = array();
$daftar_rs["nama_rs"] = stripslashes($row["NAMA_RS"]);
$daftar_rs["alamat_rs"] = stripslashes($row["ALAMAT"]);
$daftar_rs["latitude_rs"] = stripslashes($row["LATITUDE"]);
$daftar_rs["longitude_rs"] = stripslashes($row["LONGITUDE"]);
array_push($response["daftar_rs"], $daftar_rs);
}
}
$response["success"] = 1;
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "error";
echo json_encode($response);
}
?>
but json is not running properly,
display in php empty.
what should I do?
You never add the data to the response variable.
$response["daftar_rs"] = array();
while ($row = oci_fetch_array($statement)) {
$daftar_rs = array();
$daftar_rs["nama_rs"] = stripslashes($row["NAMA_RS"]);
$daftar_rs["alamat_rs"] = stripslashes($row["ALAMAT"]);
$daftar_rs["latitude_rs"] = stripslashes($row["LATITUDE"]);
$daftar_rs["longitude_rs"] = stripslashes($row["LONGITUDE"]);
$response["daftar_rs"][] = $daftar_rs;
}

Categories