所以我有一个简单的查询,检查mySQL数据库中的文章,并回显HTML代码以动态生成我的博客。当我直接用phpMyAdmin运行它时,查询有效,但是当它从.php文件中调用时,它会显示" No Results"。这是查询:
SELECT * FROM article ORDER BY article_timestamp DESC LIMIT 4;
在phpMyAdmin上测试时运行正常。但是当它在我的PHP代码中时它并没有。 PHP代码如下:
<?php
// establish connection
function connect() {
$connection = mysql_connect ("localhost", "user", "password", "dbname") or die(mysql_error);
return $connection;
}
$connection = connect();
// define article variables
$article_id;
$article_title;
$article_headline;
$article_image_path;
$article_body;
$article_author;
$article_tags;
$article_timestamp;
$myquery = "SELECT * FROM article ORDER BY article_timestamp DESC LIMIT 4";
$result = mysql_query($connection, $myquery);
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_array($result)) {
$article_id = $row['article_id'];
$article_title = $row['article_title'];
$article_headline = $row['article_headline'];
$article_image_path = $row['article_image_path'];
$article_body = $row['article_body'];
$article_author = $row['article_author'];
$article_tags = $row['article_tags'];
$article_timestamp = $row['article_timestamp'];
echo "<!-- Blog Post -->
<h2>
<a href=\"post.php?id=$article_id\">$article_title</a>
</h2>
<p class=\"lead\">
by $article_author
</p>
<p>Posted on $article_timestamp</p>
<hr>
<img class=\"img-responsive\" src=\"$article_image_path\" alt=\"\">
<hr>
<p class=\"lead\">$article_headline</p>
<a class=\"btn\" style=\"border-color: #4c044d;\" href=\"post.php?id=$article_id\">Read More</a>
<hr>";
}
}
else {
echo 'No Results found';
}
mysql_close ($connection);
?>
数据库连接应该正常工作。我添加了这段代码:
if ($connection == false) {
echo 'whoops';
}
当它被放置在各个检查站时,情况永远不会得到满足。有人可以帮我一把吗?花了几个小时,没有骰子。 提前谢谢。
答案 0 :(得分:1)
请更改您的连接功能,如下所示
$connection = mysql_connect ("localhost", "root", "redhat") or die(mysql_error());
mysql_select_db('test');
另外请使用mysqli,因为在新版本的php中不推荐使用mysql
答案 1 :(得分:0)
不推荐使用MYSQL_ *尝试使用mysqli_ *或PDO
警告mysql_query,mysql_fetch_array,mysql_connect等..扩展在PHP 5.5.0中已弃用,并且已在PHP 7.0.0中删除。 相反,应该使用MySQLi或PDO_MySQL扩展。
<?php
// establish connection
function connect() {
$servername = "localhost"; //host name
$username = "username"; //username
$password = "password"; //password
$mysql_database = "dbname"; //database name
$connection = mysqli_connect($servername, $username, $password,$mysql_database) or die("Connection failed: " . mysqli_connect_error());
return $connection;
}
$connection = connect();
$myquery = "SELECT * FROM article ORDER BY article_timestamp DESC LIMIT 4";
$stmt = $connection->prepare($myquery );
//The argument may be one of four types:
//i - integer
//d - double
//s - string
//b - BLOB
//change it by respectively
$stmt->execute();
$get_result =$stmt->get_result();
$row_count= $get_result->num_rows;
if ($row_count > 0) {
while($row = mysqli_fetch_array($get_result)) {
$article_id = $row['article_id'];
$article_title = $row['article_title'];
$article_headline = $row['article_headline'];
$article_image_path = $row['article_image_path'];
$article_body = $row['article_body'];
$article_author = $row['article_author'];
$article_tags = $row['article_tags'];
$article_timestamp = $row['article_timestamp'];
echo "<!-- Blog Post -->
<h2>
<a href=\"post.php?id=$article_id\">$article_title</a>
</h2>
<p class=\"lead\">
by $article_author
</p>
<p>Posted on $article_timestamp</p>
<hr>
<img class=\"img-responsive\" src=\"$article_image_path\" alt=\"\">
<hr>
<p class=\"lead\">$article_headline</p>
<a class=\"btn\" style=\"border-color: #4c044d;\" href=\"post.php?id=$article_id\">Read More</a>
<hr>";
}
}
else {
echo 'No Results found';
}
$stmt->close();
$connection->close();
?>