php - pdo query only works the first time -
i have searched , searched, can't find i'm doing wrong, here's first stackoverflow question.
i attempting make query entity table information entity, including id corresponding address on address table, can make 1 query. second query gives error "no database selected". testing, have tried using exact same function twice make sure it's not syntax error , result get:
name:wolf, dmitri
second try same query:
no database selected
from code:
$entity_id=1; $row = getentitybyid($entity_id); echo "<p>name:" . $row['entity_name'] . "</p>"; echo "<p>second try same query:</p>"; $row = getentitybyid($entity_id); echo "<p>name:" . $row['entity_name'] . "</p>"; function dbconnect () { require_once ('dogs.php'); try { $conn = new pdo("mysql:host=$host;dbname=$db", $user, $pwd); return $conn; } catch (pdoexception $e) { return null; } } function getentitybyid ($id) { unset($conn); $conn=dbconnect(); $sql = "select * entity id = $id"; $result = $conn->query($sql); $error = $conn->errorinfo(); if (isset($error[2])) die($error[2]); $numrows = $result->fetchcolumn(); $result->closecursor(); $therow = null; foreach ($conn->query($sql) $row) { $therow = $row; } return $therow; }
i have no idea i'm doing wrong. close cursor doesn't seem help. have ideas? thanks.
dmitri, change require_once ('dogs.php'); require ('dogs.php');
the problem requires dogs.php file everytime call function, ,as might know, require_once calls file once per execution time. change , should work fine.
cheers,
Comments
Post a Comment