DB query from PHP gives no result but same query on phpMyAdmin works?

Omar Rida

So I have a simple query that checks a mySQL database for articles and echos the HTML code to generate my blog dynamically. The query works when I run it directly with phpMyAdmin, but when it's called from the .php file, it show "No Results". Here is the query:

SELECT * FROM article ORDER BY article_timestamp DESC LIMIT 4;

Runs fine when tested on phpMyAdmin. But when it's in my php code it doesn't. PHP code below:

<?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 connection should be working fine. I added this code:

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

And the condition is never met when it's placed at various checkpoints. Can someone give me a hand? Spent hours on this and no dice. Thanks in advance.

Mukesh Swami

Please change your connect function like below

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

Also please use mysqli as mysql is deprecated in new versions of php

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Query works in phpmyadmin but not in PHP

From Dev

MySQL query works in PHPMyAdmin but not PHP

From Dev

Mysql query works in Phpmyadmin but not works in PHP

From Dev

Getting same wrong result from query in PHP

From Dev

MySQL SELECT query works in PHPmyadmin, not in PHP

From Dev

A query that uses variables works in PHPMyAdmin, but not in a PHP script

From Dev

MySQL INSERT query works in Phpmyadmin but not in PHP

From Dev

SQL Query works in phpMyAdmin but not in php page

From Dev

A query that uses variables works in PHPMyAdmin, but not in a PHP script

From Dev

mysql query works in phpmyadmin but errors in php

From Dev

Calling a PHP function only gives one row from the query result

From Dev

Calling a PHP function only gives one row from the query result

From Dev

Query gives same result multiple times

From Dev

PHP SQL query not returning same rows as in phpMyAdmin

From Dev

php mysql update query gives an empty result

From Dev

Query from php and in Mysql Workbench not same result for special char

From Dev

PHP MYSQL query won't work in PHP but works in PHPMyAdmin

From Dev

mysql and php %like% query. Works in phpmyadmin but different results php

From Dev

Query inside Laravel gives different results than from phpMyAdmin

From Dev

Insert query works on phpMyAdmin but not with Java

From Dev

PDO query works from CLI, not from PHP

From Dev

MySQL query gives wrong result

From Dev

Jena sparql (Dbpedia) query OPTIONAL filter gives no results but (http://dbpedia.org/snorql/) same query works

From Dev

different query for the same result?

From Dev

Difference query but same result

From Dev

DIfferent MySQL behaviour from MySQL query from PHP and directly at PHPMyAdmin

From Dev

mysql query works in phpmyadmin but not in node.js

From Dev

MySQL Query Not Working Live But Works In PHPMyAdmin

From Dev

Query returns empty in script, works perfect in phpMyAdmin

Related Related

  1. 1

    Query works in phpmyadmin but not in PHP

  2. 2

    MySQL query works in PHPMyAdmin but not PHP

  3. 3

    Mysql query works in Phpmyadmin but not works in PHP

  4. 4

    Getting same wrong result from query in PHP

  5. 5

    MySQL SELECT query works in PHPmyadmin, not in PHP

  6. 6

    A query that uses variables works in PHPMyAdmin, but not in a PHP script

  7. 7

    MySQL INSERT query works in Phpmyadmin but not in PHP

  8. 8

    SQL Query works in phpMyAdmin but not in php page

  9. 9

    A query that uses variables works in PHPMyAdmin, but not in a PHP script

  10. 10

    mysql query works in phpmyadmin but errors in php

  11. 11

    Calling a PHP function only gives one row from the query result

  12. 12

    Calling a PHP function only gives one row from the query result

  13. 13

    Query gives same result multiple times

  14. 14

    PHP SQL query not returning same rows as in phpMyAdmin

  15. 15

    php mysql update query gives an empty result

  16. 16

    Query from php and in Mysql Workbench not same result for special char

  17. 17

    PHP MYSQL query won't work in PHP but works in PHPMyAdmin

  18. 18

    mysql and php %like% query. Works in phpmyadmin but different results php

  19. 19

    Query inside Laravel gives different results than from phpMyAdmin

  20. 20

    Insert query works on phpMyAdmin but not with Java

  21. 21

    PDO query works from CLI, not from PHP

  22. 22

    MySQL query gives wrong result

  23. 23

    Jena sparql (Dbpedia) query OPTIONAL filter gives no results but (http://dbpedia.org/snorql/) same query works

  24. 24

    different query for the same result?

  25. 25

    Difference query but same result

  26. 26

    DIfferent MySQL behaviour from MySQL query from PHP and directly at PHPMyAdmin

  27. 27

    mysql query works in phpmyadmin but not in node.js

  28. 28

    MySQL Query Not Working Live But Works In PHPMyAdmin

  29. 29

    Query returns empty in script, works perfect in phpMyAdmin

HotTag

Archive