Warning: Illegal string offset [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I new to oop and I have a image displaying when I had a single a array query (requesting the image url only) but when I added a second dimension to the query array (image id in the table) I just ended up getting a warning message 'Warning: Illegal string offset'. I have used mysqli_fetch_assoc in the database class and then running the result through a foreach loop.
This is the message I am getting.
Thanks in advance for any help given.
page code
$user = $_SESSION['user_name'];
$query = "SELECT gallery_image, 'gallery_id' FROM gallery WHERE user_name = '$user' ORDER BY gallery_id DESC";
$result = $obj_db->get_gallery($query);
foreach ($result as $image ) {
foreach ($image as $key => $value) {
echo '<div class="gallery_item"><img src="'.$value['gallery_image'].'"></div>';
}
}
Database class code
function get_gallery($sql){
$obj_res = mysqli_query($this->obj_db_conn, $sql);
if(mysqli_errno($this->obj_db_conn)){
die ("Failed query: $strSql".$this->obj_db_conn->error);
}
$arResults = array();
while($arRow = mysqli_fetch_assoc($obj_res)){
$arResults[] = $arRow;
}
return $arResults;

If your var_dump output on $result give this,
array(2) {
[0]=> array(2) {
["gallery_image"]=> ""
["gallery_id"]=> ""
}
[1]=> array(2) {
["gallery_image"]=> ""
["gallery_id"]=> ""
}
}
you don't need two nested foreach loops in your page code. The one foreach loop could be enough.
$query = "SELECT gallery_image, gallery_id FROM gallery WHERE user_name = '$user' ORDER BY gallery_id DESC";
$result = $obj_db->get_gallery($query);
foreach ($result as $image ) {
echo '<div class="gallery_item"><img src="'.$image['gallery_image'].'"></div>';
}

Related

How do I get my code to echo only once instead of twice? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
My code echo's Hey or Hello twice once the requirements are met.
This is supposed to check whether or not a person has something uploaded, depending on if they do or don't either message displays.
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$sqlImg = "SELECT * FROM users WHERE idUsers='$current'";
$resultImg = mysqli_query($conn, $sqlImg);
while ($rowImg = mysqli_fetch_assoc($resultImg)) {
if ($rowImg['profile_img'] == 0) {
echo "hey";
} else {
echo "Hello";
}
}
}
}
I expect the output to echo either word once, but the actual output is echoing it twice.
You don't need two loops. The outer loop is running for every user in the table, regardless of whether they match $current. For each of them you're doing another query that just gets the $current user, and echoing their status.
If idUsers is a unique key, you don't even need any loops. Just do one query and fetch the row.
You should also use a prepared statement to prevent SQL injection.
$stmt = $conn->prepare("SELECT profile_img FROM users WHERE idUsers = ?");
$stmt->bind_param("s", $current);
$stmt->execute();
$result = $stmt->get_result();
$rowImg = $result->fetch_assoc();
if ($rowImg['profile_img'] == 0) {
echo "hey";
} else {
echo "Hello";
}

codeigniter where = does not work with words [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The following code is not working properly:
$query = $this->db->query("SELECT accountType FROM users WHERE id = $loggedID" );
It errors if the $loggedID is words like "justin", but if its only number like 201110523, it works. I don't know what is wrong. The datatype of the id in users is varchar.
public function account_type_student(){
$loggedID = $this->input->post('id');
$query = $this->db->query("SELECT accountType FROM users WHERE id = $loggedID" );
foreach ($query->result() as $row)
{
$query = $row->accountType;
}
if($query=="student"){
return true;
}
else{
return false;
}
}
When it is a word / string like "justin" then you have to escape your variable:
$query = $this->db->query("SELECT accountType FROM users WHERE id = '$loggedID' " );
Or use the active pattern syntax:
$this->db->select('accountType');
$this->db->where('id', $loggedID);
$query = $this->db->get('users');

Array to string conversion [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
There is an error in my code but the code works perfectly. I mean all the values are inserted in the database but there is an error like this on the screen:
Severity: Notice
Message: Array to string conversion
Filename: models/some_model.php
Line Number: 106
This is my code:
View:
<?php foreach($app as $row){
echo "<tr><td><input type=checkbox name=appname[] value='".$row->app_name."'/>".$row->id."</td><td>".$row->app_name."</td><tr>".
?>
Controller:
public function hide(){
$this->load->model('some_model');
$visi = $this->input->post('appname');
$success = $this->some_model->hideApp($visi);
foreach($visi as $key=>$value)
{
$success = $this->some_model->hideApp($visi[$key]);
}
if($success == TRUE)
$this->hideApp_page(TRUE);
else $this->hideApp_page(FALSE);
}
Model:
public function hideApp($visi){
$visi = $this->db->escape_str($visi);
$queryStr = "UPDATE appwarehouse.application_table SET visibility='hidden' where app_name='$visi';"; /* this is line 106*/
$query = $this->db->query($queryStr);
return $query;
}
$visi is array like [1,2,3,4]
when you put $visi in hideApp()
it will be display "array to string error"
so maybe you can remove this line $success = $this->some_model->hideApp($visi);
you have already do some_model->hideApp($visi[$key]) in the foreach loop
so i don't know why you write this$success = $this->some_model->hideApp($visi);
if you still want to run $success = $this->some_model->hideApp($visi);
you have to put $visi into a string

Having issue using shuffle with array and mysql query [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I' am running into a problem where an array is not being able to shuffle. I want the array to randomized for each page loads. I followed the documentation but something still wrong over here
Error Message
Warning: Invalid argument supplied for foreach() in E:\xampp\htdocs\projects\snppets\sidebar.php on line 65
This is the code that gets the friends list, makes the sting into array, counts the array and shuffles it.
$friends = get_friends_IDS($profile_id);
$friends = make_string_to_ARRAY($friends);
$friends_count = count($friends);
$friends = shuffle($friends);
This is the code that is in line 65
<?php
foreach($friends as $id){
$sql = "SELECT profile_id, profile_photo, profile_username, profile_name FROM profile WHERE profile_id='$id' LIMIT 1";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT();
if($db->NUM_ROWS() > 0){
foreach($rows as $row){
$friends_profile_id = $row->profile_id;
$friends_profile_photo = $row->profile_photo;
$friends_profile_username = $row->profile_username;
$friends_profile_name = $row->profile_name;
}
}
}
?>
This function gets the friends list from the database
function get_friends_IDS($profile_id){
global $db;
$sql = "SELECT profile_id, profile_friends FROM profile WHERE profile_id='$profile_id' LIMIT 1";
$query = $db->SELECT($sql);
if($db->NUM_ROWS()){
foreach($db->FETCH_OBJECT() as $row){
return $row->profile_friends;
}
}
}
This is the function makes a string into array
function make_string_to_ARRAY($array){
$array = explode(',', $array);
return $array;
}
When I remove the shuffle then it works fine...
shuffle() returns boolean value.
So $friends = shuffle($friends); makes $friends as a boolean variable.
just use shuffle($friends); to shuffle it

What is wrong with this PDO statement? Cannot use object of type PDOStatement as array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
I want to fetch every result from table 'Themes'.
function display_all_themes()
{
global $pdo;
$select = $pdo->prepare("SELECT * FROM themes");
$select->execute();
while ($row = $select->fetch(PDO::FETCH_ASSOC))
{
echo $select['theme_name'].'<br />';
}
}
Getting this error:
Fatal error: Cannot use object of type PDOStatement as array in C:\xampp\htdocs\driptone\inc\functions.inc.php on line 137
Line 137:
echo $select['theme_name'].'<br />';
What is the problem?
Thanks.
You're using $select instead of $row inside loop.
while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
echo $row['theme_name'].'<br />';
}
use $row, except $select
echo $row['theme_name'].'<br />';
You assigned it to $row but you're calling $select.
Should be:
while ($row = $select->fetch(PDO::FETCH_ASSOC))
{
echo $row['theme_name'].'<br />';
}

Categories