Solved

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

Posted on 2009-05-13
5
828 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 143

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

735 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