无效的数据源名称

时间:2019-06-05 02:27:12

标签: php

试图使网站博客正常运行,并得到无效的数据源名称错误。

试图对代码进行一些更改,并更正了以前的一些错误。

db_connection.php

<?php
function OpenCon()
 {
 $dbhost = "localhost";
 $dbuser = "root";
 $dbpass = "123";
 $db = "db";
 $conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect 
failed: 
%s\n". $conn -> error);

 return $conn;
 }

function CloseCon($conn)
 {
 $conn -> close();
 }

?>

index.php

try {
           

            // $stmt = db->query('SELECT postID, postTitle, postDesc, postDate FROM blog_posts ORDER BY postID DESC');
            $conn = new PDO('SELECT postID, postTitle, postDesc, postDate FROM blog_posts ORDER BY postID DESC');
            while($row = $stmt->fetch()){

                echo '<div>';
                    echo '<h1><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h1>';
                    echo '<p>Posted on '.date('jS M Y H:i:s', strtotime($row['postDate'])).'</p>';
                    echo '<p>'.$row['postDesc'].'</p>';             
                    echo '<p><a href="viewpost.php?id='.$row['postID'].'">Read More</a></p>';               
                echo '</div>';

            }

预期结果应该是网站博客页面 实际结果是页面显示无效数据源名称。

2 个答案:

答案 0 :(得分:0)

如果您要使用PDO,下面是一些示例代码,这些代码可以帮助您朝正确的方向前进。

db_connection.php

<?php
$host = 'localhost'; $db = 'db_name'; $user = 'user'; $pw = 'password';
$conn = new PDO('mysql:host='.$host.';dbname='.$db.';charset=utf8', $user, $pw);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>

index.php

<head>

上方
<?php
require("path/to/file/1_ErrorReporting.php"); // this is the file that will allow you to see errors during production
// don't forget to remove the line or remark it out with double forward slashes when going live
require('path/to/folder/db_connection.php');

$sql = "SELECT `postID`, `postTitle`, `postDesc`, `postDate` FROM `blog_posts` ORDER BY `postID` DESC";
$query = $conn->prepare($sql);
$query->execute();
$results = $query->fetchAll();
$totalRows = $query->rowCount();
?>

注意表名和列名周围的“反引号”。万一您碰巧将保留字用作表名或列名,这总是一个好主意。

1_ErrorReporting.php

<?php
ini_set('display_errors',1); 
error_reporting(E_ALL);
?>

在index.php页面的正文中

<?php
foreach($results as $row) {
    echo '<div>'.
    '<h1><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h1>'.
   '<p>Posted on '.date('jS M Y H:i:s', strtotime($row['postDate'])).'</p>'.
   '<p>'.$row['postDesc'].'</p>'.
   '<p><a href="viewpost.php?id='.$row['postID'].'">Read More</a></p>'.
   '</div>';
}
?>

无需在每行上写回显。只需以句点结束该行,直到要回显的最后一行,然后以分号结束。

在index.php页面底部

</body>
</html>
<?php
$query->closeCursor();
$conn = null;
?>

我希望这能使您朝正确的方向前进。

快乐编码!!!

答案 1 :(得分:0)

您没有执行选择语句。你可以试试这个

$conn = new PDO("mysql:host=YOURSERVERNAME;dbname=YOUR DB NAME", YOUR USERNAME, YOUR PASSWORD);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT postID, postTitle, postDesc, postDate FROM blog_posts ORDER BY postID DESC"); 
$stmt->execute();

while($row = $stmt->fetch()){
   echo '<div>';
       echo '<h1><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h1>';
       echo '<p>Posted on '.date('jS M Y H:i:s', strtotime($row['postDate'])).'</p>';
       echo '<p>'.$row['postDesc'].'</p>';             
       echo '<p><a href="viewpost.php?id='.$row['postID'].'">Read More</a></p>';               
   echo '</div>';