html - Run a call from a function PHP -


i'm building website using php , html, im used receiving data database, aka dynamic website, i've build cms own use. im trying "simplify" receiving process using php , functions.

my functions.php looks this:

function get_db($row){         $dsn = "mysql:host=".$globals["db_host"].";dbname=".$globals["db_name"];         $dsn = $globals["dsn"];         try {             $pdo = new pdo($dsn, $globals["db_user"], $globals["db_pasw"]);             $stmt = $pdo->prepare("select * lp_sessions");             $stmt->execute();             $row = $stmt->fetchall();             foreach ($row $row) {                 echo $row['session_id'] . ", ";             }         }         catch(pdoexception $e) {             die("could not connect database\n");         }     } 

where rows content this: $row['row']; i'm trying call this: snippet below index.php

echo get_db($row['session_id']); // line 22 

just show whats in rows. when run code snippet error:

notice: undefined variable: row in c:\wamp\www\wordpress ish\index.php on line 22

i'm using pdo know :)

any appreciated!

regards stian

edit: updated functions.php

function get_db(){         $dsn = "mysql:host=".$globals["db_host"].";dbname=".$globals["db_name"];         $dsn = $globals["dsn"];         try {             $pdo = new pdo($dsn, $globals["db_user"], $globals["db_pasw"]);             $stmt = $pdo->prepare("select * lp_sessions");             $stmt->execute();             $rows = $stmt->fetchall();             foreach ($rows $row) {                 echo $row['session_id'] . ", ";             }         }         catch(pdoexception $e) {             die("could not connect database\n");         }     } 

instead of echoing values db, function should return them string.

function get_db(){     $dsn = "mysql:host=".$globals["db_host"].";dbname=".$globals["db_name"];     $dsn = $globals["dsn"];     $result = '';     try {         $pdo = new pdo($dsn, $globals["db_user"], $globals["db_pasw"]);         $stmt = $pdo->prepare("select * lp_sessions");         $stmt->execute();         $rows = $stmt->fetchall();         foreach ($rows $row) {             $result .= $row['session_id'] . ", ";         }     }     catch(pdoexception $e) {         die("could not connect database\n");     }     return $result; } 

then call as:

echo get_db(); 

another option function return session ids array:

function get_db(){     $dsn = "mysql:host=".$globals["db_host"].";dbname=".$globals["db_name"];     $dsn = $globals["dsn"];     $result = array();     try {         $pdo = new pdo($dsn, $globals["db_user"], $globals["db_pasw"]);         $stmt = $pdo->prepare("select * lp_sessions");         $stmt->execute();         $rows = $stmt->fetchall();         foreach ($rows $row) {             $result[] = $row['session_id'];         }     }     catch(pdoexception $e) {         die("could not connect database\n");     }     return $result; } 

then use as:

$sessions = get_db(); // $sessions array 

and caller can make use of values in array, perhaps using them key in other calls instead of printing them.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -