Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Array to string conversion error

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
julez
Smarty n00b


Joined: 22 Sep 2015
Posts: 3

PostPosted: Tue Sep 22, 2015 8:46 am    Post subject: Array to string conversion error Reply with quote

Hello I have the Array to string conversion erro with this code:

index.php:

Code:
$hoArr = array(1,2,3);
foreach ($hoArr as $hid) {
    $mysqli = mysqli_connect("localhost", "root", "", "zabbixtest");
    if (!$mysqli) {
        die("Connection failed: " . mysqli_connect_error());
    }
    $sql = "SELECT errors_from FROM hosts WHERE hostid = '$hid'";
    $re = mysqli_query($mysqli, $sql);
    while ($row = mysqli_fetch_assoc($re)) {
        $query_times[] = $row['errors_from'];
       
       
    }
   
     mysqli_free_result($re);
    mysqli_close($mysqli);

   
}
 $smarty->assign('query_times',$query_times);


and index.tpl:

Code:
<p>{$query_times}</p>


I have no idea whats wrong with it..
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Sep 22, 2015 3:26 pm    Post subject: Reply with quote

Everything wrong. From the very first line of code.
And the very last one is completely wrong, of which you the error message clearly tell.
You have $query_times array, and you are trying to access it as string. Won't work.
Back to top
View user's profile Send private message
julez
Smarty n00b


Joined: 22 Sep 2015
Posts: 3

PostPosted: Tue Sep 22, 2015 3:28 pm    Post subject: Reply with quote

Okay could you pls help me instead of saying that everything is wrong?!
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Sep 22, 2015 3:45 pm    Post subject: Reply with quote

Help as in "write the code for you"? No.
Start from http://php.net/manual/en/language.types.php
The error is very basic and indicates that you don't know the very fundamental concepts of the language you're using.

And onward to more complex concepts:
1. NEVER EVER USE A DATABASE ACCOUNT WITH "SUPER" PRIVILEGE IN PRODUCTION.
EVER.
Even worse, your root user don't have a password. THAT is an insult added to injury.
You're basically inviting anyone to destroy your entire database in the event that your application malfunction and expose you to injection attack.
2. Why the hell you're connecting to a databse in cycle?
3. Why you are using MySQLi? Why you are using it that way? You're not giving it even a bit of respect in your code. You've blindly replaced mysql_* functions with mysqli_* and think that it is "all good"? No, it is NOT good. It is worse than "bad".
Back to top
View user's profile Send private message
julez
Smarty n00b


Joined: 22 Sep 2015
Posts: 3

PostPosted: Tue Sep 22, 2015 4:00 pm    Post subject: Reply with quote

First of all MySQLi isn't the wrong way but you could use POD.
Second this is allhardcoded because itis local are you blind? This page is only for monitoring something in my server. And I never said u should write the code for me I just wanted to know why I cant assign this array with smarty.

You have no idea what you are talking about.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Sep 22, 2015 4:25 pm    Post subject: Reply with quote

julez wrote:
First of all MySQLi isn't the wrong way but you could use POD.

It is not a question of what you use, it is a question of how you use it.
Quote:
Second this is allhardcoded because itis local

Doesn't matter if local or not.

Quote:
And I never said u should write the code for me I just wanted to know why I cant assign this array with smarty.

It did assign it.
That's why it shows you the error it does.
You can get the same eror without Smarty - replace "$smarty->assign('query_times',$query_times);" with
"print $query_times;"…

And go back to reading language manual.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Sep 24, 2015 6:45 pm    Post subject: Reply with quote

$query_times is an array. So you must loop over it for output.

Code:
{foreach $query_times as $foo}
   {$foo}
{/foreach}

See http://www.smarty.net/docs/en/language.function.foreach.tpl
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP