db查询没有结果,但在phpmyadmin上的相同查询有效吗?

qcbq4gxm  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(220)

所以我有一个简单的查询,它检查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);
?>

db连接应该可以正常工作。我添加了以下代码:

if ($connection == false) {
   echo 'whoops';
}

当它被放置在不同的检查站时,这个条件是永远不会满足的。有人能帮我一下吗?在这上面花了好几个小时,却没有掷骰子。提前谢谢。

qvk1mo1f

qvk1mo1f1#

请改变你的连接功能如下

$connection = mysql_connect ("localhost", "root", "redhat") or die(mysql_error());
    mysql_select_db('test');

另外,请使用mysqli,因为mysql在新版本的php中已被弃用

rdlzhqv9

rdlzhqv92#

不推荐使用mysqli或pdo
警告mysql\u query、mysql\u fetch\u array、mysql\u connect等。。在php5.5.0中不推荐使用扩展,在php7.0.0中删除了扩展。相反,应该使用mysqli或pdo\umysql扩展。

<?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();

    ?>

相关问题