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

MySQL Abfrage

 
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 -> Language: German
View previous topic :: View next topic  
Author Message
Juergen56
Smarty Rookie


Joined: 23 Aug 2015
Posts: 5

PostPosted: Sun Aug 23, 2015 6:00 pm    Post subject: MySQL Abfrage Reply with quote

Hallo zusammen,

eins vorweg, ich bin wirklich absoluter Laie auf dem Gebiet der Programmierung. Dank der Smarty Doku bin ich aber schon relativ weit gekommen.

Code:
$db = new PDO($host, $user, $passwd, array(
 PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));

$res = $db->prepare("SELECT * FROM computers");
$res->execute();
$res->setFetchMode(PDO::FETCH_LAZY);

$smarty->assign('res',$res);

$smarty->display('templates/computers.tpl');


Hiermit frage ich die Daten aus meiner MySQL Datenbank ab. Das Resultat übergebe ich auch erfolgreich an mein Template und bekomme die Daten dort auch korrekt angezeigt. Soweit so gut...

Jetzt benötige ich Teile meiner Abfrage aber nicht im Template, sondern bereits in der PHP Datei, um es als Quelle für eine Chart zu verwenden.

Das bekomme ich zum verrecken aber nicht hin...
Das SQL Statement müsste ich wie folgt anpassen:

Code:
SELECT computername FROM computers WHERE id='2'


Aber wie bekomme ich an das Reultat?

Und ja, ich habe keine Ahnung von dem was ich da tue, aber bislang funktioniert es so wie ich es brauche... Rolling Eyes


Gruß
Jürgen
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Mon Aug 24, 2015 6:01 am    Post subject: Reply with quote

Hey,

du übergibst doch $res an dein Template:

Quote:

$smarty->assign('res',$res);


und dort arbeitest du dann ja irgendwie damit, z. B.: so
Quote:
{$res.Title} //bzw. {$res["Title"]} oder {$res->Title} je nachdem wie du es bekommst, als Array oder Objekt


Und so kannst du doch auch im Code (also bevor du es an Smarty übergibst bzw. theoretisch auch danach) arbeiten.

Oder vertsehe ich da was falsch?
Back to top
View user's profile Send private message Visit poster's website
Juergen56
Smarty Rookie


Joined: 23 Aug 2015
Posts: 5

PostPosted: Mon Aug 24, 2015 7:04 am    Post subject: Reply with quote

Hi,

zunächst mal vielen Dank für deine Antwort.
Ich hatte es so probiert:
Code:
echo $res['computername'];


Liefert folgenden Fehler:
Quote:
Fatal error: Cannot use object of type PDOStatement as array in /var/www/...


BTW: Macht meine SQL Abfrage so überhaupt Sinn? Das es funktioniert heißt ja nicht zwangsläufig das es auch die "beste" Lösung ist. Laughing


Gruß
Jürgen
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Mon Aug 24, 2015 9:22 am    Post subject: Reply with quote

Ja okay, das liegt jetzt allerdings daran (und da hat die Fehlermeldung Recht xD) dass du versuchst auf ein Array zuzugreifen, obwogl es keins ist.

Du müsstest es als Objekt behandeln:

Code:
$res->computername;


Auf deine "By the way"-Frage kann ich schlecht antworten, wenn ich nicht das gesamte Konzept/System bzw. "etwas mehr" erfahre.
Back to top
View user's profile Send private message Visit poster's website
Juergen56
Smarty Rookie


Joined: 23 Aug 2015
Posts: 5

PostPosted: Mon Aug 24, 2015 9:59 am    Post subject: Reply with quote

Hi,
Code:
echo $res->computername;

liefert mir kein Ergebnis. Crying or Very sad

Quote:
Auf deine "By the way"-Frage kann ich schlecht antworten, wenn ich nicht das gesamte Konzept/System bzw. "etwas mehr" erfahre.

Das Projekt dient mir persönlich zum verwalten diverser PC, Notebooks, Software etc.
Eine MySQL Datenbank mit aktuell 5 Tabellen.

Ist also kein XXL Projekt o.ä.

Gruß
Jürgen
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Tue Aug 25, 2015 5:45 am    Post subject: Reply with quote

Du kannst dir z. B. mittels
Code:
var_dump($deineVariable)
anzeigen lassen (alternative u. a. auch print_r) was in der Varibale drinnen steht, also z. B. so:

Code:
var_dump($res);


Ich kenne mich jetzt persönlich nicht mit PDO aus, aber ich denke, dass da noch ein zwischenschritt fehlt. Du musst nach dem

Code:
$res->execute();


sowas hier machen (Rückgabe als Objekt):
Code:

$result = $res->fetchObject();


bzw. als Array:

Code:
$result = $res->fetch();


und dann weiter mit dem $result arbeiten, also sprich so:

Code:
$result->computername


bzw. als Array:

Code:
$result['computername'];


Last edited by Grizzly on Tue Aug 25, 2015 11:37 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
Juergen56
Smarty Rookie


Joined: 23 Aug 2015
Posts: 5

PostPosted: Tue Aug 25, 2015 7:55 am    Post subject: Reply with quote

Hi,

vielen Dank! Jetzt funktioniert es. Ich glaube ich sollte mir meine MySQL Abfrage nochmal anschauen. Diese PDO Lösung scheint für meinen Gebrauch nicht die gängigste zu sein.

Viele Grüße
Jürgen
Back to top
View user's profile Send private message
Juergen56
Smarty Rookie


Joined: 23 Aug 2015
Posts: 5

PostPosted: Tue Aug 25, 2015 6:08 pm    Post subject: Reply with quote

Hi,
ich bins nochmal mit einer Verständnis Frage:

Folgender Code liefert mir folgendes Ergebnis:
Code:
var_dump($comp);


Code:
array(4) {
  [0]=>
  object(computers)#3 (3) {
    ["id":"computers":private]=>
    string(1) "1"
    ["computername":"computers":private]=>
    string(14) "PC001"
    ["username":"computers":private]=>
    string(2) "xy"
  }
  [1]=>
  object(computers)#4 (3) {
    ["id":"computers":private]=>
    string(1) "2"
    ["computername":"computers":private]=>
    string(15) "PC002"
    ["username":"computers":private]=>
    string(2) "yx"
  }
  [2]=>
  object(computers)#5 (3) {
    ["id":"computers":private]=>
    string(1) "3"
    ["computername":"computers":private]=>
    string(15) "PC003"
    ["username":"computers":private]=>
    string(2) "xx"
  }
  [3]=>
  object(computers)#6 (3) {
    ["id":"computers":private]=>
    string(1) "4"
    ["computername":"computers":private]=>
    string(9) "PC004"
    ["username":"computers":private]=>
    string(20) "testuser"
  }
}


Ich hätte erwartet das ich wie folgt auf das erste Ergebnis zugreifen kann:

Code:
$comp[0]['computername']


Dem ist aber nicht so. Was mache ich falsch?


Gruß
Jürgen[/quote]
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Aug 25, 2015 11:10 pm    Post subject: Reply with quote

Benutze nicht $result = $res->fetchObject();

Code:
$result = $res->fetchAll();


Liefert Dir alles als array zurück.
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Wed Aug 26, 2015 5:37 am    Post subject: Reply with quote

Um deinen "Fehler" zu erklären:

du hast ein Array von Objekten, deshalb müsstest du in deinem Fall so darauf zugreifen:

Code:
$comp[0]->computername
Back to top
View user's profile Send private message Visit poster's website
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 -> Language: German 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