Jump to content

php mysql fetch array problem


synking

Recommended Posts

i am attempting to create a php cms using a tutorial and the tutorial used php4 and this line

 

 
  //***function: FetchArray, Purpose: Get array of query results***
     function fetchArray($result){

              return mysql_fetch_array($result);

   }

 

but i get this error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/jking/public_html/includes/DbConnector.php on line 44

 

 

when attempting to call the function with this code.

<b>what's New: </b><br>
<?php
// Require the database class
require_once('includes/DbConnector.php');

// Create an object (instance) of the Dbconnector
$connector = new DbConnector();

//Execure the query to retrive the articles
$result = $connector->query('SELCET ID, title FROM cmsarticles ORDER BY ID DESC LIMIT 0,5');

// Get an array containing the results.
// Loop for each item in that array
while ($row = $connector->fetchArray($result)){

    echo '<p><a href="viewArticle.php?id='.$row['ID'].'">';
    echo $row['title'];
    echo '</a> </p>';

}
?>

can anyone help with this.

Link to comment
Share on other sites

The error means that your query failed and returned a FALSE value instead of a result resource and your code has no error checking in it so it blindly attempted to execute a mysql_fetch_array() on a FALSE value instead of a result resource.

 

You need to test if a query works before attempting to access any data from the query.

Link to comment
Share on other sites

i added the die call after the fetch array and i get this warning message now.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/jking/public_html/includes/DbConnector.php on line 44
could Not fetch data: 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 'SELCET ID, title FROM cmsarticles ORDER BY ID DESC LIMIT 0,5' at line 1

 

anyone able to help.

 

Link to comment
Share on other sites

is anyone able to help on this i have been working on it and now i am back to the original error

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/jking/public_html/includes/DbConnector.php on line 46

 

and the line in question

 

  function fetchArray($result){

             return mysql_fetch_array($result)
                    or die ("could Not fetch data: ". mysql_error());

 

i have read over the way to use that but i am not sure what i am doing wrong....anyone got anything i could read to find out.

Link to comment
Share on other sites

mysql_fetch_array() does not set mysql_error(), so your code is not really doing what you think.

 

The or die(...) needs to be on the mysql_query() statement. The reason your code previously output the correct mysql error was - the mysql_query() failed and set up mysql_error() with the reason why. You then called the mysql_fetch_array() and it returned a false value. This caused the or die() statement to output the query error that was already set.

 

For your present problem, either the result resources has been overwritten or the query was a type that does not return a result resource, such as an UPDATE (in which case you should not be performing a mysql_fetch_array() as that only applies to a SELECT query.) You would need to show the query that is being executed and what your current code is that is causing the error.

Link to comment
Share on other sites

ok so here is the DbConnector.php

 

<?php
//////////////////////////////////////////////////
//Class: DbConnector
//Purpose; Connect to database, Mysql version
//////////////////////////////////////////////////
require_once 'SystemComponent.php';

class Dbconnector extends SystemComponent {

   var $theQuery;
   var $link;

   //***Function: DbConnector, Purpose: Connect to the database***
   function DbConnector(){

       //Load settings from parent class
        $settings = SystemComponent::getSettings();

       //Get the main settings from the array we just loaded
        $host = $settings['dbhost'];
        $db = $settings['dbname'];
        $user = $settings['dbusername'];
        $pass = $settings['dbpassword'];


       //Connect to the database
       $this->link=mysql_connect($host, $user, $pass) 
                          or die("Could not connect: " . mysql_error());
       mysql_select_db($db);
       register_shutdown_function(array(&$this, 'close'));

   }

      //***Function:query, purpose: execute a database query***
      function query($query){

              $this->theQuery = $query;
              return mysql_query($query, $this->link)
                     or die ("unable to run query: ". mysql_error());

   }

     //***function: FetchArray, Purpose: Get array of query results***
     function fetchArray($result){

             return mysql_fetch_array($result) 
                    or die ("could Not fetch data: ". mysql_error());


   }


    //***Function: close, Purpose: Close the connection ***
    function close(){

          mysql_close($this->link);

   }


}
?>

 

and the index.php i am having issue with.

<html><head><title></title></head>
<body>

<b>what's New: </b><br>


<?php
// Require the database class
require_once('includes/DbConnector.php');

// Create an object (instance) of the Dbconnector
$connector = new DbConnector();

//Execure the query to retrive the articles
$result = $connector->query('SELECT ID, title FROM cmsarticles ORDER BY ID DESC LIMIT 0,5')
                             or die ("Could not fetch: " . mysql_error());

// Get an array containing the results.
// Loop for each item in that array
while ($row = $connector->fetchArray($result)){

    echo '<p><a href="viewArticle.php?id='.$row['ID'].'">';
    echo $row['title'];
    echo '</a> </p>';

}
?>
</body>
</html>

Link to comment
Share on other sites

The current problem is because the return statement in the function code is causing the "or" in the or die() to be evaluated, returning a true value instead of the result resource (I think the "return" section of the php manual might actually state something about this.)

 

Either assign the result to a variable and return the variable or remove the or die() part in the function code. You should just do error checking on one place anyway. Either in the function or in the main code, but not both.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.