Some SQL Help needed

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Alright. I know that this is not the best way to do this. I know that this is not how you do this as well, because giving even one result exceeds 30 seconds :roll: :lol:

PHP Code: [ Select ]
        $p = 1;
        // Getting the result form the last SQL
        while($result = $this->fetch_row($sql))
        {
            $page .= "            <p>Result #{$p}</p>\n";
            $i = 1;
            // Creating the resulting page
            foreach($result as $column_name => $column_value)
            {
                $column_value = nl2br($column_value);
                $page .= "            <div class=\"sql_sdata{$i}\">\n               <strong>{$column_name}</strong>\n            </div>\n            <div class=\"sql_data{$i}\">\n                 <strong>{$column_value}</strong>\n            </div>\n";
                $i = (($i == 1) ? ++$i : --$i);
            }
            ++$p;
        }
  1.         $p = 1;
  2.         // Getting the result form the last SQL
  3.         while($result = $this->fetch_row($sql))
  4.         {
  5.             $page .= "            <p>Result #{$p}</p>\n";
  6.             $i = 1;
  7.             // Creating the resulting page
  8.             foreach($result as $column_name => $column_value)
  9.             {
  10.                 $column_value = nl2br($column_value);
  11.                 $page .= "            <div class=\"sql_sdata{$i}\">\n               <strong>{$column_name}</strong>\n            </div>\n            <div class=\"sql_data{$i}\">\n                 <strong>{$column_value}</strong>\n            </div>\n";
  12.                 $i = (($i == 1) ? ++$i : --$i);
  13.             }
  14.             ++$p;
  15.         }

What I need to do though, is to print a CSS page following what the above code would produce hypothetically... if everything works, and it does, if you remove the while.

And if I try do{}while() it doesn't work either... same result as above code.

I can't just use plain while(){} and then put it all in HTML, because the SQL would always dynamically change and it would not be the same, so I would have no idea what column name would be used, and I can't have an numeric key... I need the column name to be as a name key... like...
PHP Code: [ Select ]
 
$row = mysql_fetch_assoc($sql);
 
// Like this
$column_name = $row['column_name'];
 
// NOT like this
$column_name = $row[1];
 
  1.  
  2. $row = mysql_fetch_assoc($sql);
  3.  
  4. // Like this
  5. $column_name = $row['column_name'];
  6.  
  7. // NOT like this
  8. $column_name = $row[1];
  9.  

I know that there is a function (mysql_field_name) but I don't know how to implement that in my case... I have tried the following
PHP Code: [ Select ]
        $num = $this->num_rows($this->last_sql, 2);
        $offset = 1;
        // Getting the result form the last SQL
        while($result = $this->fetch_row($sql, 2))
        {
            $column_name = mysql_field_name($results, $offset);
            $result[$column_name] = $result[$column_name];
            ++$offset;
            if($offset > $num)
            {
                die('How the hell did that happen?');
            }
        }
  1.         $num = $this->num_rows($this->last_sql, 2);
  2.         $offset = 1;
  3.         // Getting the result form the last SQL
  4.         while($result = $this->fetch_row($sql, 2))
  5.         {
  6.             $column_name = mysql_field_name($results, $offset);
  7.             $result[$column_name] = $result[$column_name];
  8.             ++$offset;
  9.             if($offset > $num)
  10.             {
  11.                 die('How the hell did that happen?');
  12.             }
  13.         }

But I get "How the hell did that happen?" And I don't know how that happened :scratchhead: I have 10 fields in the table but for some reason the $offset starts at 10 and on... if I put a number in there like 5 instead of $offset for mysql_field_name(); it takes a long time and exceeds 30 seconds. I know that there is a better more efficient way to do this, but I just don't know how to do this :(

I hope I have explained myself well enough... if not, please ask for clarification.

Thanks in advance for help that I hope to get :D
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6250
  • Loc: Seattle, WA

Post 3+ Months Ago

I haven't looked at all of your code and so I won't comment on other issues yet (or the fact that you're using 30 seconds for something like that), but to answer your last question:

You keep incrementing 'offset' but never check it's value on each iteration of your loop. Eventually it's going to keep looping until it's one value larger than 'num', and the conditional block will be executed, hence your error message.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I thought that
PHP Code: [ Select ]
            if($offset > $num)
            {
                die('How the hell did that happen?');
            }
  1.             if($offset > $num)
  2.             {
  3.                 die('How the hell did that happen?');
  4.             }
was checking the value on each iteration of my loop :| Now I'm confused :scratchhead:

BTW: The reason I'm using 30 seconds for that is because I don't know any other way to do this the correct way.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6250
  • Loc: Seattle, WA

Post 3+ Months Ago

It is checking it, but what is preventing 'offset' from incrementing past 'num' in the first place?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

spork wrote:
It is checking it, but what is preventing 'offset' from incrementing past 'num' in the first place?

lol... Alright, I get you... now the following code
PHP Code: [ Select ]
        $num = $this->num_rows($this->last_sql, 2);
        // echo $num;
        $offset = 0;
        // Getting the result form the last SQL
        while($result = $this->fetch_row($sql, 2))
        {
            $column_name = mysql_field_name($results, $offset);
            $result[$column_name] = $result[$column_name];
            if($offset != $num)
            {
                ++$offset;
            }
            if($offset > $num)
            {
                die('How the hell did that happen?');
            }
        }
  1.         $num = $this->num_rows($this->last_sql, 2);
  2.         // echo $num;
  3.         $offset = 0;
  4.         // Getting the result form the last SQL
  5.         while($result = $this->fetch_row($sql, 2))
  6.         {
  7.             $column_name = mysql_field_name($results, $offset);
  8.             $result[$column_name] = $result[$column_name];
  9.             if($offset != $num)
  10.             {
  11.                 ++$offset;
  12.             }
  13.             if($offset > $num)
  14.             {
  15.                 die('How the hell did that happen?');
  16.             }
  17.         }

Gives me the following error
Quote:
Warning: mysql_field_name() [function.mysql-field-name]: Field 10 is invalid for MySQL result index 8 in C:\wamp\www\censite ttest\classes\db.php on line 745
like 60 bajilion times :x



Even though by idea the following should work
PHP Code: [ Select ]
while($result = fetch_row($sql))
{
   $i = 0
   foreach($result as $column_name => $column_value)
   {
      $page .= "      <div class=\"sql_sdata{$i}\">\n           <strong>{$column_name}</strong>\n      </div>\n       <div class=\"sql_data{$i}\">\n           <strong>{$column_value}</strong>\n      </div>\n";
      $i = (($i == 1) ? ++$i : --$i);
   }
}
  1. while($result = fetch_row($sql))
  2. {
  3.    $i = 0
  4.    foreach($result as $column_name => $column_value)
  5.    {
  6.       $page .= "      <div class=\"sql_sdata{$i}\">\n           <strong>{$column_name}</strong>\n      </div>\n       <div class=\"sql_data{$i}\">\n           <strong>{$column_value}</strong>\n      </div>\n";
  7.       $i = (($i == 1) ? ++$i : --$i);
  8.    }
  9. }

But it doesn't... exceeds 30 seconds for some reason :x
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6250
  • Loc: Seattle, WA

Post 3+ Months Ago

Hmm.. before I go any further here, may I ask what this accomplishes?

Code: [ Select ]
$result[$column_name] = $result[$column_name];
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I don't even know... I'm testing here, get into a lot of dead ends and get out of it.

I was hoping that that would set the array $result[$column_name] to the value of $result[$column_name] so in reality it would be $result[$column_name] = 'Column_value';

It's just that I need to create that array from the SQL Result Set.

If you want to see the whole function for this, I'll paste it here... it's about 100 lines long.

Just generating a page showing the results of a SELECT query of the last query used... for administrator debug thing but whatever.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6250
  • Loc: Seattle, WA

Post 3+ Months Ago

You can go ahead and post what you have, I'll tear through it tomorrow when I have some time.

As for now, it's 2 a.m. and time for some much needed sleep :D
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I fixed it with the following code
PHP Code: [ Select ]
    $results = mysql_query($this->last_sql);
      $p = 1;
      // Getting the result form the last SQL
      while($result = mysql_fetch_assoc($results))
      {
         $page .= "      <p style=\"clear: both;\">Result #{$p}</p>\n";
         $i = 1;
         // Creating the resulting page
         foreach($result as $column_name => $column_value)
         {
            $column_value = nl2br(htmlspecialchars($column_value));
            if(is_null($column_value))
            {
               $column_value = '&nbsp;';
            }
            $page .= "      <div class=\"sql_sdata{$i}\">\n           <strong>{$column_name}</strong>\n      </div>\n       <div class=\"sql_data{$i}\">\n           <strong>{$column_value}</strong>\n      </div>\n";
 
         }
         ++$p;
      }
  1.     $results = mysql_query($this->last_sql);
  2.       $p = 1;
  3.       // Getting the result form the last SQL
  4.       while($result = mysql_fetch_assoc($results))
  5.       {
  6.          $page .= "      <p style=\"clear: both;\">Result #{$p}</p>\n";
  7.          $i = 1;
  8.          // Creating the resulting page
  9.          foreach($result as $column_name => $column_value)
  10.          {
  11.             $column_value = nl2br(htmlspecialchars($column_value));
  12.             if(is_null($column_value))
  13.             {
  14.                $column_value = '&nbsp;';
  15.             }
  16.             $page .= "      <div class=\"sql_sdata{$i}\">\n           <strong>{$column_name}</strong>\n      </div>\n       <div class=\"sql_data{$i}\">\n           <strong>{$column_value}</strong>\n      </div>\n";
  17.  
  18.          }
  19.          ++$p;
  20.       }
Looks like I can't have my own defined fetch_row function in there... but it looks like
PHP Code: [ Select ]
  function fetch_row($sql, $type = 1)
   {
      // Setting the variable to be the last SQL
      $this->last_sql = $sql;
     
      // Creating a result resource
      $results = mysql_query($sql);
     
      // Checking what type of result we want
      if($type == 1)
      {
         $result = mysql_fetch_assoc($results);
      }
      elseif($type == 2)
      {
         $result = mysql_fetch_array($results);
      }
      elseif($type == 3)
      {
         $result = mysql_fetch_object($results);
      }
      elseif($type == 4)
      {
         $result = mysql_fetch_row($results);
      }
      else
      {
         $result = mysql_fetch_assoc($results);
      }
     
      // Setting the variable to be the last result
      $this->last_result = $result;
     
      // Checking if the result should be cached
      /*if($this->cached)
      {
         // Result should be cached... cache it
         return $this->cache_result($result);
      }*/
      return $result;
   }
  1.   function fetch_row($sql, $type = 1)
  2.    {
  3.       // Setting the variable to be the last SQL
  4.       $this->last_sql = $sql;
  5.      
  6.       // Creating a result resource
  7.       $results = mysql_query($sql);
  8.      
  9.       // Checking what type of result we want
  10.       if($type == 1)
  11.       {
  12.          $result = mysql_fetch_assoc($results);
  13.       }
  14.       elseif($type == 2)
  15.       {
  16.          $result = mysql_fetch_array($results);
  17.       }
  18.       elseif($type == 3)
  19.       {
  20.          $result = mysql_fetch_object($results);
  21.       }
  22.       elseif($type == 4)
  23.       {
  24.          $result = mysql_fetch_row($results);
  25.       }
  26.       else
  27.       {
  28.          $result = mysql_fetch_assoc($results);
  29.       }
  30.      
  31.       // Setting the variable to be the last result
  32.       $this->last_result = $result;
  33.      
  34.       // Checking if the result should be cached
  35.       /*if($this->cached)
  36.       {
  37.          // Result should be cached... cache it
  38.          return $this->cache_result($result);
  39.       }*/
  40.       return $result;
  41.    }
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Oh, and the function that I was talking about is...
PHP Code: [ Select ]
<?php
   /*
   * function last_sql_data([$linked [, $title [, $return]]])
   *  @boolean $linked - Determines if the generated HTML should be a whole page
   *  @string $title - The title of the SQL Result set
   *  @boolean $return - Determines if the page should be returned or echoed
   *
   * Creates a TABLE like result using CSS div's using
   * the last SQL used in the class
   */
   
   function last_sql_data($linked = false, $title = 'SQL Results Data', $return = true)
   {
      // Setting the starting time
      $time1 = microtime(true);
     
      // Retrieving the last sql
      $sql = (($this->valid_select($this->get_last_sql())) ? $this->get_last_sql() : null);
     
      // Checking if $sql is null
      if(is_null($sql))
      {
         trigger_error('The SQL for function last_sql_data() is invalid. Should be a valid SELECT query ', E_USER_ERROR);
      }
     
      // Creating a result resource
      $this->set_result_resource($sql);
 
      // Storing the CSS used for the class
      $this->data_css = " .sql_data {
   width: 100%%;
   border: 1px solid #000000;
   background-image: url('images/dseg.png');
}
.sql_data1 {
   float: left;
   width: 80%;
   height: auto;
   border: 1px solid #000000;
   background-image: url('images/dseg.png');
}
.sql_data2 {
   float: left;
   width: 80%;
   height: auto;
   border: 1px solid #000000;
}
.sql_sdata1 {
   float: left;
   width: 15%;
   height: auto;
   border: 1px solid #000000;
   background-image: url('images/dseg.png');
}
.sql_sdata2 {
   float: left;
   width: 15%;
   height: auto;
   border: 1px solid #000000;
}
p.sql_d {
   margin-bottom: 0px;
}";
     
      // Generating the first half of the page
      if($linked)
      {
         $page = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">
<head>
<title>{$title}</title>
<style type=\"text/css\">
<!--
{$this->data_css}
//-->
</style>
<link href=\"./templates/default/style/main.css\" rel=\"stylesheet\" type=\"text/css\" />
</head>
<body>
<div id=\"top\">
   <p>Century Sites</p>
   <div id=\"menu\">
      <h2 class=\"top\">SQL Results</h2>
      <p>Page generated in {TIME} seconds.</p>
   </div>
</div>
<div id=\"divider\"></div>
<div id=\"mainb\">
   <p class=\"top\"> &loz; <em>Century Sites</em> &rarr; <em><a href=\"index.php\">Index</a></em> &rarr; <em><a href=\"#\">{$title}</a></em></p>
   <div id=\"text\">
      <div id=\"sql_data\">
";
      }
      else
      {
         $page = "<div id=\"sql_data\">\n";
      }
 
      // Pointing the MySQL seek pointer to the first result
      mysql_data_seek($this->last_result, 0);
     
      // The result number
      $p = 1;
     
      // Getting the result form the last SQL
      while($result = mysql_fetch_assoc($this->last_result))
      {
         // The result number page HTML
         $page .= "      <p style=\"clear: both;\">Result #{$p}</p>\n";
         
         // The style configuration number
         $i = 1;
         
         // Creating the resulting page
         foreach($result as $column_name => $column_value)
         {
            // Formatting the column value to what we need
            $column_value = nl2br($this->spchars($column_value));
           
            // Making sure that empty results are still generated
            if($column_value == false)
            {
               $column_value = '&nbsp;';
            }
           
            // Generating the main portion of the data table
            $page .= "      <div class=\"sql_sdata{$i}\">\n           <strong>{$column_name}</strong>\n      </div>\n       <div class=\"sql_data{$i}\">\n           <strong>{$column_value}</strong>\n      </div>\n";
 
            // Incrementing the style configuration number
            $i = (($i == 1) ? ++$i : --$i);
         }
         
         // Incrementing the result number
         ++$p;
      }
     
      // Finishing off the generation of the page
      if($linked)
      {
         $page .= "  <p style=\"clear: both;\"></p>
      </div>
   </div>
</div>
<div id=\"footer\">
   <p>Copyright &copy; 2008 <a href=\"http://centurysites.ismywebsite.com\">Century Sites</a><br />
   <em>Designed and coded by <strong><a href=\"http://www.wedevoy.com\">Wedevoy</a></strong></em></p>
</div>
<div id=\"left\"></div>
<div id=\"right\"></div>
</body>
</html>";
      }
      else
      {
         $page .= "<p style=\"clear: both;\">Page generated in {TIME} seconds.</p>
</div>\n";
      }
      // Setting the ending time
      $time2 = microtime(true);
     
      // Determining how long it took to generate the page
      $time = $time2 - $time1;
     
      // Replacing the {TIME} to be the time it took the page to generate
      $page = str_replace('{TIME}', substr($time, 0, -10), $page);
     
      // Checking if the page should be returned or echoed
      if($return)
      {
         // Returning the generated page
         return $page;
      }
      else
      {
         echo $page;
      }
   }
?>
  1. <?php
  2.    /*
  3.    * function last_sql_data([$linked [, $title [, $return]]])
  4.    *  @boolean $linked - Determines if the generated HTML should be a whole page
  5.    *  @string $title - The title of the SQL Result set
  6.    *  @boolean $return - Determines if the page should be returned or echoed
  7.    *
  8.    * Creates a TABLE like result using CSS div's using
  9.    * the last SQL used in the class
  10.    */
  11.    
  12.    function last_sql_data($linked = false, $title = 'SQL Results Data', $return = true)
  13.    {
  14.       // Setting the starting time
  15.       $time1 = microtime(true);
  16.      
  17.       // Retrieving the last sql
  18.       $sql = (($this->valid_select($this->get_last_sql())) ? $this->get_last_sql() : null);
  19.      
  20.       // Checking if $sql is null
  21.       if(is_null($sql))
  22.       {
  23.          trigger_error('The SQL for function last_sql_data() is invalid. Should be a valid SELECT query ', E_USER_ERROR);
  24.       }
  25.      
  26.       // Creating a result resource
  27.       $this->set_result_resource($sql);
  28.  
  29.       // Storing the CSS used for the class
  30.       $this->data_css = " .sql_data {
  31.    width: 100%%;
  32.    border: 1px solid #000000;
  33.    background-image: url('images/dseg.png');
  34. }
  35. .sql_data1 {
  36.    float: left;
  37.    width: 80%;
  38.    height: auto;
  39.    border: 1px solid #000000;
  40.    background-image: url('images/dseg.png');
  41. }
  42. .sql_data2 {
  43.    float: left;
  44.    width: 80%;
  45.    height: auto;
  46.    border: 1px solid #000000;
  47. }
  48. .sql_sdata1 {
  49.    float: left;
  50.    width: 15%;
  51.    height: auto;
  52.    border: 1px solid #000000;
  53.    background-image: url('images/dseg.png');
  54. }
  55. .sql_sdata2 {
  56.    float: left;
  57.    width: 15%;
  58.    height: auto;
  59.    border: 1px solid #000000;
  60. }
  61. p.sql_d {
  62.    margin-bottom: 0px;
  63. }";
  64.      
  65.       // Generating the first half of the page
  66.       if($linked)
  67.       {
  68.          $page = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  69. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  70. <html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">
  71. <head>
  72. <title>{$title}</title>
  73. <style type=\"text/css\">
  74. <!--
  75. {$this->data_css}
  76. //-->
  77. </style>
  78. <link href=\"./templates/default/style/main.css\" rel=\"stylesheet\" type=\"text/css\" />
  79. </head>
  80. <body>
  81. <div id=\"top\">
  82.    <p>Century Sites</p>
  83.    <div id=\"menu\">
  84.       <h2 class=\"top\">SQL Results</h2>
  85.       <p>Page generated in {TIME} seconds.</p>
  86.    </div>
  87. </div>
  88. <div id=\"divider\"></div>
  89. <div id=\"mainb\">
  90.    <p class=\"top\"> &loz; <em>Century Sites</em> &rarr; <em><a href=\"index.php\">Index</a></em> &rarr; <em><a href=\"#\">{$title}</a></em></p>
  91.    <div id=\"text\">
  92.       <div id=\"sql_data\">
  93. ";
  94.       }
  95.       else
  96.       {
  97.          $page = "<div id=\"sql_data\">\n";
  98.       }
  99.  
  100.       // Pointing the MySQL seek pointer to the first result
  101.       mysql_data_seek($this->last_result, 0);
  102.      
  103.       // The result number
  104.       $p = 1;
  105.      
  106.       // Getting the result form the last SQL
  107.       while($result = mysql_fetch_assoc($this->last_result))
  108.       {
  109.          // The result number page HTML
  110.          $page .= "      <p style=\"clear: both;\">Result #{$p}</p>\n";
  111.          
  112.          // The style configuration number
  113.          $i = 1;
  114.          
  115.          // Creating the resulting page
  116.          foreach($result as $column_name => $column_value)
  117.          {
  118.             // Formatting the column value to what we need
  119.             $column_value = nl2br($this->spchars($column_value));
  120.            
  121.             // Making sure that empty results are still generated
  122.             if($column_value == false)
  123.             {
  124.                $column_value = '&nbsp;';
  125.             }
  126.            
  127.             // Generating the main portion of the data table
  128.             $page .= "      <div class=\"sql_sdata{$i}\">\n           <strong>{$column_name}</strong>\n      </div>\n       <div class=\"sql_data{$i}\">\n           <strong>{$column_value}</strong>\n      </div>\n";
  129.  
  130.             // Incrementing the style configuration number
  131.             $i = (($i == 1) ? ++$i : --$i);
  132.          }
  133.          
  134.          // Incrementing the result number
  135.          ++$p;
  136.       }
  137.      
  138.       // Finishing off the generation of the page
  139.       if($linked)
  140.       {
  141.          $page .= "  <p style=\"clear: both;\"></p>
  142.       </div>
  143.    </div>
  144. </div>
  145. <div id=\"footer\">
  146.    <p>Copyright &copy; 2008 <a href=\"http://centurysites.ismywebsite.com\">Century Sites</a><br />
  147.    <em>Designed and coded by <strong><a href=\"http://www.wedevoy.com\">Wedevoy</a></strong></em></p>
  148. </div>
  149. <div id=\"left\"></div>
  150. <div id=\"right\"></div>
  151. </body>
  152. </html>";
  153.       }
  154.       else
  155.       {
  156.          $page .= "<p style=\"clear: both;\">Page generated in {TIME} seconds.</p>
  157. </div>\n";
  158.       }
  159.       // Setting the ending time
  160.       $time2 = microtime(true);
  161.      
  162.       // Determining how long it took to generate the page
  163.       $time = $time2 - $time1;
  164.      
  165.       // Replacing the {TIME} to be the time it took the page to generate
  166.       $page = str_replace('{TIME}', substr($time, 0, -10), $page);
  167.      
  168.       // Checking if the page should be returned or echoed
  169.       if($return)
  170.       {
  171.          // Returning the generated page
  172.          return $page;
  173.       }
  174.       else
  175.       {
  176.          echo $page;
  177.       }
  178.    }
  179. ?>
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Alright... I got why it didn't work at first and I worked it out... had to create a second function that does this and creates the array and then returns that array... this time it works if I use that function... except that using the following sql

SELECT * FROM forum_a

gives me the following error

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 'Resource id #7' at line 1

And if I remove that or die(mysql_error()); I get the following error


Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\wamp\www\censite ttest\classes\db.php on line 200


The function that I'm using for that is

Code: [ Select ]
<?php
    function fetch_rowset($sql, $type = 1)
    {
        // Setting the variable to be the last SQL
        $this->last_sql = $sql;
        
        // Creating a result resource
        $results = mysql_query($sql);
        
        // Saving the result to be as the one used last
        $this->last_result = $results;
        
        // Checking what type of result we want
        if($type == 1)
        {
            while($row = mysql_fetch_assoc($results))
            {
                $result[] = $row;
            }
        }
        elseif($type == 2)
        {
            while($row = mysql_fetch_array($results))
            {
                $result[] = $row;
            }
        }
        elseif($type == 3)
        {
            while($row = mysql_fetch_object($results))
            {
                $result[] = $row;
            }
        }
        elseif($type == 4)
        {
            while($row = mysql_fetch_row($results))
            {
                $result[] = $row;
            }
        }
        else
        {
            while($result = mysql_fetch_assoc($results))
            {
                $result[] = $row;
            }
        }
        return $result;
    }
?>
  1. <?php
  2.     function fetch_rowset($sql, $type = 1)
  3.     {
  4.         // Setting the variable to be the last SQL
  5.         $this->last_sql = $sql;
  6.         
  7.         // Creating a result resource
  8.         $results = mysql_query($sql);
  9.         
  10.         // Saving the result to be as the one used last
  11.         $this->last_result = $results;
  12.         
  13.         // Checking what type of result we want
  14.         if($type == 1)
  15.         {
  16.             while($row = mysql_fetch_assoc($results))
  17.             {
  18.                 $result[] = $row;
  19.             }
  20.         }
  21.         elseif($type == 2)
  22.         {
  23.             while($row = mysql_fetch_array($results))
  24.             {
  25.                 $result[] = $row;
  26.             }
  27.         }
  28.         elseif($type == 3)
  29.         {
  30.             while($row = mysql_fetch_object($results))
  31.             {
  32.                 $result[] = $row;
  33.             }
  34.         }
  35.         elseif($type == 4)
  36.         {
  37.             while($row = mysql_fetch_row($results))
  38.             {
  39.                 $result[] = $row;
  40.             }
  41.         }
  42.         else
  43.         {
  44.             while($result = mysql_fetch_assoc($results))
  45.             {
  46.                 $result[] = $row;
  47.             }
  48.         }
  49.         return $result;
  50.     }
  51. ?>

Other than that, it works and it doesn't take 30 seconds to do it :lol: :roll:

Any help on this would be great :D
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Anyone? Any help here?

$db->last_sql = 'SELECT * FROM forum_a';

$db->last_sql_data(true, 'SQL Results Data', false);

That is how I'm calling it in the database test file...

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 64 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.