SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result...

    Hi,

    I have the following function to display posts on my site. It works fine on my localhost but when I uploaded the files to the server I got the following warning:

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

    I can't see what I am doing wrong because it works just fine on localhost. Any ideas?

    PHP Code:
    function get_posts() {
        
    $query mysql_query("SELECT
                    `post_title`,
                    `post_name`,
                    `post_body`,
                    `post_date`,
                    `author_title`,
                    `author_name`
                    FROM `posts`
                    WHERE `post_type` = 'post'
                    LEFT JOIN `authors` ON `author_id` = `post_author`
                    ORDER BY `post_id` DESC LIMIT 0,8"
    );
        while (
    $row mysql_fetch_assoc($query)) { $posts[] = $row;}
        return 
    $posts;


  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Instead of jumping straight in the array you need some error catching to see whats going wrong, see the below example.

    PHP Code:
    $query mysql_query("QUERY GOES HERE") or die(mysql_error()); 
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris.upjohn View Post
    Instead of jumping straight in the array you need some error catching to see whats going wrong, see the below example.

    PHP Code:
    $query mysql_query("QUERY GOES HERE") or die(mysql_error()); 
    I tried that, it says there is some kind of syntax problem in the query that I can't spot. I can't spot because it works just fine on my localhost.

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Could you please post the error that you receive?
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,016
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    the error is invalid syntax

    you can't put the WHERE clause there

    it goes after the FROM clause, and the FROM clause includes all joins

    pro tip: test your queries outside of php first
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is solved now, thanks for your help Chris and Rudy.

    Quote Originally Posted by chris.upjohn View Post
    Could you please post the error that you receive?
    Following was the error I got:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN `authors` ON `author_id` = `post_author` ORDER BY `post_id` ...

    Quote Originally Posted by r937 View Post
    the error is invalid syntax

    you can't put the WHERE clause there

    it goes after the FROM clause, and the FROM clause includes all joins

    pro tip: test your queries outside of php first
    Thanks for the tip, I will definitely test my queries when I get such errors the next time. I now have the following and it works perfectly.

    PHP Code:
    $query mysql_query("SELECT 
                    `post_title`, 
                    `post_name`, 
                    `post_body`, 
                    `post_date`, 
                    `author_title`, 
                    `author_name`
                    FROM `posts` 
                    LEFT JOIN `authors` ON `author_id` = `post_author` 
                    WHERE `post_type` = 'post'
                    ORDER BY `post_id` DESC LIMIT 0,8"
    ); 
    I guess that my first query worked on the localhost and not on the server because they have different MySQL versions.

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,016
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Nail Yener View Post
    I guess that my first query worked on the localhost and not on the server because they have different MySQL versions.
    had to have been some other query

    WHERE in front of LEFT JOIN has always been an error, in any mysql version, ever
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •