Getting an array to string conversion error - php

Can someone help me what's the problem with this code? Im trying to store the fetched data to an array and i want to based on the values of that array. Im getting an error of Array to string conversion. The datatype value of an array is string
Here's the code.
$sql3 ="SELECT DISTINCT subj_descr FROM subj_enrolled WHERE enroll_ref = '$ref'";
$results = mysqli_query($con, $sql3);
$data = array();
while($row = mysqli_fetch_array($results)){
$data[] = array($row['subj_descr']);
}
$sql ="SELECT * FROM notification WHERE subj_descr IN ({implode(',', $data})";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);

Remove array inside your while loop:
$sql3 ="SELECT DISTINCT subj_descr FROM subj_enrolled WHERE enroll_ref = '$ref'";
$results = mysqli_query($con, $sql3);
$data = array();
while($row = mysqli_fetch_array($results)){
$data[] = $row['subj_descr'];
}
$sql ="SELECT * FROM notification WHERE subj_descr IN ({implode(',', $data})";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);

create a new variable and implode in it.
Try this
$implodeAray = implode(",", $data);
$sql ="SELECT * FROM notification WHERE subj_descr IN ($implodeAray)";

You are creating a multidimensional array and so change this statement
$data[] = array($row['subj_descr']);
to
$data[] = $row['subj_descr'];
As SQL IN statement always used a single dimensional array so also make change in query where clause.
I have changed all, please try below code:
<?php
$sql3 ="SELECT DISTINCT subj_descr FROM subj_enrolled WHERE enroll_ref = '$ref'";
$results = mysqli_query($con, $sql3);
$data = array();
while($row = mysqli_fetch_array($results)){
$data[] = $row['subj_descr'];
}
$dataStr = implode(',', $data);
$sql ="SELECT * FROM notification WHERE subj_descr IN (".$dataStr.")";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);
?>

You stored the element of your array inside another array while looping.
Do this:
$sql3 ='SELECT DISTINCT subj_descr
FROM subj_enrolled
WHERE enroll_ref = "$ref"';
$results = mysqli_query($con, $sql3);
$data = array();
while($row = mysqli_fetch_array($results)){
//Your error was here
//Each elements is escaped for security reasons
$data[] = mysqli_escape_string($con,$row['subj_descr']);
}
//This implodes and puts a single quote around each element
$dataIn= '\'' . implode( '\', \'', $data ) . '\'';
$sql ="SELECT * FROM notification
WHERE subj_descr IN ($dataIn)";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);

Related

The reason for not submitting one of the tables from the database

$sql = "select * from tbl_user";
$result = $this->doSelect($sql);
foreach ($result as $key => $row) {
$sql = "select * from tbl_comment order by id desc";
$result = $this->doSelect($sql);
$result[$key]['idduc'] = $result;
}
return $result;
}
You are looping on a variable called $result, from the first query.
In the second query you destroy $result by reusing it in the second query processing. Also you then destroy that $result by assigning things to it as an array you want to return.
So use a different variable for the inner look like this
$sql = "select * from tbl_user";
$result = $this->doSelect($sql);
foreach ($result as $key => $row) {
$sql = "select * from tbl_comment order by id desc";
$result1 = $this->doSelect($sql);
$return[$key]['idduc'] = $result1;
}
return $return;
}

Merging arrays to the one array from while loop

I need an information to put from DB to arrays and then make one array.
So I wrote a following code:
$sql0 = "SELECT * FROM ".$working_table." ORDER by id ASC";
$result = mysqli_query($conn, $sql0);
$num_rows = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result)){
${"emails_arr$row[id]"} = explode(",",$row['station_email']);
echo print_r(${"emails_arr$row[id]"})."<br>";
}
$sql0 = "SELECT * FROM ".$working_table." ORDER by id ASC";
$result = mysqli_query($conn, $sql0);
$num_rows = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result)){
$result_array = array_merge(${"emails_arr$row[id]"});
}
echo print_r($result_array);
the tricky part is that i don't know how to merge an arrays to one array from the while loop: $result_array = array_merge(${"emails_arr$row[id]"});
it only shows the last array, other arrays are being rewrote.
Thank you guys!
Change your while loop code with this one
while($row = mysqli_fetch_array($result)){
$result_array[] = array_merge(${"emails_arr$row[id]"});
}
Notice the Square Brackets.

Array to string conversion in mysql while loop

Following is my code,
$result1 = "SELECT emp_id FROM employee where manager_id=".$userID;
$array = mysql_query($result1);
$cnt = 0;
while ($row = mysql_fetch_array($array)) {
"emp_id: " . $row[0];
$myArrayOfemp_id[$cnt] = $row[0];
$cnt++;
}
var_dump($myArrayOfemp_id);
$sql = "SELECT emp_id FROM emp_leaves WHERE emp_id='$myArrayOfemp_id' ORDER BY apply_date DESC";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
When I'am trying to use $myArrayOfemp_id variable in $sql query, It shows that error:
Array to string conversion in..
How can I fix it?
You are trying to convert an array into a string in the following line:
$sql = "SELECT emp_id FROM emp_leaves
WHERE emp_id='$myArrayOfemp_id' ORDER BY apply_date DESC";
$myArrayOfemp_id is an array. That previous line of code should be changed to:
$sql = "SELECT emp_id FROM emp_leaves
WHERE emp_id={$myArrayOfemp_id[0]} ORDER BY apply_date DESC";
I placed 0 inside {$myArrayOfemp_id[0]} because I'm not sure what value want to use that is inside the array.
Edited:
After discussing what the user wanted in the question, it seems the user wanted to use all the values inside the array in the sql statement, so here is a solution for that specific case:
$sql = "SELECT emp_id FROM emp_leaves
WHERE ";
foreach ($myArrayOfemp_id as $value)
{
$sql .= " emp_id={$value) || ";
}
$sql .= "1=2";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
$sql = "SELECT emp_id FROM emp_leaves WHERE emp_id in
(SELECT GROUP_CONCAT(emp_id) FROM employee where manager_id=".$userID.")
ORDER BY apply_date DESC";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
just change your query like above might solve your problem.
you can remove following code now. :)
$result1 = "SELECT emp_id FROM employee where manager_id=".$userID;
$array = mysql_query($result1);
$cnt = 0;
while ($row = mysql_fetch_array($array)) {
"emp_id: " . $row[0];
$myArrayOfemp_id[$cnt] = $row[0];
$cnt++;
}
var_dump($myArrayOfemp_id);

sql json not working

i have created a php script which get the value from the database saves it in json
$sql = "SELECT detail FROM BUNK WHERE id = '".$id."'";
$sql2 = "SELECT yes FROM BUNK WHERE id = '".$id."'";
$sql3 = "SELECT depend FROM BUNK WHERE id = '".$id."'";
$sql4 = "SELECT no FROM BUNK WHERE id = '".$id."'";
$result1 = mysqli_query($link,$sql);
$json['detail'] = $result1;
echo json_encode($json);
$result2 = mysqli_query($link,$sql2);
$json['yes'] = $result2;
echo json_encode($json);
$result3 = mysqli_query($link,$sql3);
$json['depend'] = $result3;
echo json_encode($json);
$result4 = mysqli_query($link,$sql4);
$json['no'] = $result4;
echo json_encode($json);
but i'm getting result as:
{"detail":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}}{"detail":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null},"yes":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}}{"detail":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null},"yes":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null},"depend":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}}{"detail":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null},"yes":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null},"depend":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null},"no":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}}
Basic:
$sql = "SELECT detail, yes, depend, no FROM BUNK WHERE id = '".$id."'";
$result = mysqli_query( $link, $sql ) or die( $link->mysqli_error );
$rows = mysqli_fetch_all( $result, MYSQLI_ASSOC );
$json = json_encode( $rows );
This is only basic, because:
If your id come from user input (a HTML form, i.e.), you have to consider prepared statements;
If you want to customize JSON string, you have to use a loop with mysqli_fetch_assoc instead of using mysqli_fetch_all
$json is the resulting JSON string. To output it:
echo $json;

convert | separated values into new table using PHP

Edit: I forgot to add the explode part that I'm having the issues with. I need the query result exploded.
I have been messing with this for a while and have a workable procedure in mysql, however I want to accomplish this as part of a larger script. I have a table filled with IDs and several columns of data with "|" separated values. How can I use or edit the below PHP to query and insert normalized results into a new table?
If I run this with an actual string: "40|180|408|360|40|166|80|59"; It will insert values (not the ID, which I also need) but when I try to pass in query results, I get "Array to string conversion" errors. Any guidance would be appreciated.
$query = "Select id, imageSize from T1";
$result = mysqli_query($conn, $query);
$myArray = explode('|', $result);
foreach($myArray as $value) {
$sql = "INSERT INTO testExplode VALUES ($value)";
$result = mysqli_query($conn, $sql);
}
If you want to insert all of your results then:
$query = "Select id, imageSize from T1";
$myArray = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($myArray)) {
$sql = "INSERT INTO testExplode VALUES (" . mysqli_real_escape_string($conn, $row['imageSize']) . ")";
mysqli_query($conn, $sql);
}
//If just only one:
$query = "Select id, imageSize from T1";
$myArray = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($myArray);
$sql = "INSERT INTO testExplode VALUES (" . mysqli_real_escape_string($conn, $row['imageSize']) . ")";
mysqli_query($conn, $sql);
NOTE:
Avoid sql injecions by escaping your variables in your querys.
EDIT:
Based on the OP comment.
$query = "Select id, imageSize from T1";
$myArray = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($myArray)) {
$values = explode('|', $row['imageSize']);
foreach ($values as $value) {
$sql = "INSERT INTO testExplode VALUES (" . mysqli_real_escape_string($conn, $value) . ")";
mysqli_query($conn, $sql);
}
}

Categories