Solved

Warning: mysql-fetch-assoc():supplied argument is not a valid MySQL result resource in

Posted on 2009-05-13
5
812 Views
Last Modified: 2013-12-12
Warning: mysql-fetch-assoc():supplied argument is not a valid MySQL result resource in  /include/functions.inc.php on line 142

line 142 is :  while ($row = mysql_fetch_assoc($result) )

this and other errors appear after server and php change

How do i fix this?  Help.
/*

* Get an array of the questions

*/

function getQuestions($qid='all')

{

    $sql  = "SELECT * FROM QUESTION ORDER BY Q_TEXT";
 

    if ($qid != 'all' && !empty($qid))

    {

        $sql .= "AND Q_ID='$qid'";

    }
 

    $result=getSQLResult($sql);
 

    while ($row = mysql_fetch_assoc($result) )

    {
 

        $Q[$row['Q_ID']]=$row['Q_TEXT'];

    }
 

    return $Q;

}

Open in new window

0
Comment
Question by:justmelat
5 Comments
 
LVL 8

Expert Comment

by:mostart
ID: 24376596
again a warning, saying $result is not a valid mysql result. You need to make sure $result is a valid result before fetching data.

All those warnings probably are  now showing up because your "error_reporting" setting in the php.ini config file changed. It just did not show the warnings before allthough they where always there so you could also just turn off warnings in php.ini again (which is not a goog idea though in my opinion)
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24376710
Alter your code to look more that that below

function getQuestions($qid='all')
{
    $sql  = "SELECT * FROM QUESTION ORDER BY Q_TEXT";
 
    if ($qid != 'all' && !empty($qid))
    {
        $sql .= "AND Q_ID='$qid'";
    }
 
    $result=getSQLResult($sql);

    // Assuming that getSQLResult passes back a result set or false on failure ...
    //
    if ( !$result )
        $Q = array();
    else
        while ($row = mysql_fetch_assoc($result) )
        {

             $Q[$row['Q_ID']]=$row['Q_TEXT'];
        }
 
    return $Q;
}

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24376747
To turn off error messages on a live server, go to /etc/php5/apache2/php.ini (the most likely location for php.ini) and edit it. Look for a line saying

display_errors = on

and change it to

display_errors = off

then save the file and restart Apache with

apache2ctl restart on a command line. You will need root access to do this.
0
 
LVL 12

Expert Comment

by:jessegivy
ID: 24381181
I think bportlock is on the right track.  The real question here is what the heck does getSQLResult($sql) return?  Is it really appropriate to return false?  More importantly though, where is this function being called from?  If the function you posted is inside a class you'll need to use the $this->getSQLResult($sql) keyword!?  It seems more likely that this is a function that's being included from another file, which is something i can strongly advise against.  If you have custom functionality you'd like to use each and every time you want to connect to the database, create a connection class.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24563873
the problem is here:

   $sql  = "SELECT * FROM QUESTION ORDER BY Q_TEXT";
 
    if ($qid != 'all' && !empty($qid))
    {
        $sql .= "AND Q_ID='$qid'";

the resulting sql would be:

SELECT * FROM QUESTION ORDER BY Q_TEXTAND Q_ID='<something>'

as you see, there are actually PLENTY of problems:
§ no WHERE clause,
§ AND after the ORDER instead of before
§ possible sql injection
§ if the Q_ID is numerical data types, don't use quotes
§ avoid SELECT * whenever you can in your queries, just wasting resources...

fix for all 3 issues:
function getQuestions($qid='all')

{   // query all columns, all rows by default

    $sql  = "SELECT * FROM QUESTION WHERE 1 = 1 " ;#

    // better specify the columns you really need

    // $sql  = "SELECT Q_ID, Q_TEXT FROM QUESTION WHERE 1 = 1 " ;

 

    if ($qid != 'all' && !empty($qid))

    {   // query for a given Q_ID

        $sql .= " AND Q_ID='" . mysql_real_escape_string($qid) . "'";

        // if q_id is numerical, don't use quotes:

        //$sql .= " AND Q_ID= " . mysql_real_escape_string($qid) . " ";

    }

    else

    {  // if we get all the records, do the order by

       // for 1 single record (above), no need to request a order by 

       $sql .= " ORDER BY Q_TEXT ";

    }
 

    $result=getSQLResult($sql);

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now