dxshindeo Posted July 25, 2012 Report Share Posted July 25, 2012 (edited) Sveiki, ejošs kods: index.php //---------connect part----------------- try { $hostname = "localhost"; $dbname = "resursi"; $username = "root"; $pw = "root"; $pdo = new PDO ("mysql:host=$hostname;dbname=$dbname","$username","$pw"); } catch (PDOException $e) { echo "Failed to get DB handle: " . $e->getMessage() . "\n"; exit; } //-----------Fetching part----------- $from = 0; $limit = 10; $query = $pdo->prepare("SELECT * FROM archive ORDER BY id desc LIMIT $from, $limit"); $query->execute(); for($i=0; $row = $query->fetch(); $i++) { echo $i." - ".$row['author']."<br/>"; } Es vēlos šo kodu sadalīt index.php un connect.php. connect.php //---------connect part----------------- try { $hostname = "localhost"; $dbname = "resursi"; $username = "root"; $pw = "root"; $pdo = new PDO ("mysql:host=$hostname;dbname=$dbname","$username","$pw"); } catch (PDOException $e) { echo "Failed to get DB handle: " . $e->getMessage() . "\n"; exit; } index.php include "connect.php"; //-----------Fetching part----------- $from = 0; $limit = 10; $query = $pdo->prepare("SELECT * FROM archive ORDER BY id desc LIMIT $from, $limit"); $query->execute(); for($i=0; $row = $query->fetch(); $i++) { echo $i." - ".$row['author']."<br/>"; } Throws "Fatal error: Call to a member function prepare() on a non-object ". Vai ir kāds veids kā to paveikt? Edited July 25, 2012 by dxshindeo Quote Link to comment Share on other sites More sharing options...
Kavacky Posted July 25, 2012 Report Share Posted July 25, 2012 die(); @ connect.php var_dump($pdo); @ index.php Quote Link to comment Share on other sites More sharing options...
v3rb0 Posted July 25, 2012 Report Share Posted July 25, 2012 connect.php pēdējā rinda: return $pdo; index.php sākumā: $pdo = include 'connect.php'; Quote Link to comment Share on other sites More sharing options...
dxshindeo Posted July 25, 2012 Author Report Share Posted July 25, 2012 Paldies :) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.