Jump to content
php.lv forumi

PDO koda sašķelšana


dxshindeo

Recommended Posts

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 by dxshindeo
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...