Some SQL Help needed
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8212
- Loc: USA
- Status: Offline
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

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...
$row = mysql_fetch_assoc($sql);
// Like this
$column_name = $row['column_name'];
// NOT like this
$column_name = $row[1];
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
But I get "How the hell did that happen?" And I don't know how that happened
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
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;
}
// 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;
}
- $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;
- }
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];
- $row = mysql_fetch_assoc($sql);
- // Like this
- $column_name = $row['column_name'];
- // NOT like this
- $column_name = $row[1];
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?');
}
}
$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?');
}
}
- $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?');
- }
- }
But I get "How the hell did that happen?" And I don't know how that happened
I hope I have explained myself well enough... if not, please ask for clarification.
Thanks in advance for help that I hope to get
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
January 19th, 2009, 11:11 pm
- spork
- Brewmaster


- Joined: Sep 22, 2003
- Posts: 6134
- Loc: Seattle, WA
- Status: Offline
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.
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.
The Beer Monocle. Classy.
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8212
- Loc: USA
- Status: Offline
I thought that
was checking the value on each iteration of my loop
Now I'm confused 
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.
PHP Code: [ Select ]
if($offset > $num)
{
die('How the hell did that happen?');
}
{
die('How the hell did that happen?');
}
- if($offset > $num)
- {
- die('How the hell did that happen?');
- }
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.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- spork
- Brewmaster


- Joined: Sep 22, 2003
- Posts: 6134
- Loc: Seattle, WA
- Status: Offline
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8212
- Loc: USA
- Status: Offline
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?');
}
}
// 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?');
}
}
- $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?');
- }
- }
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 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);
}
}
{
$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);
}
}
- 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);
- }
- }
But it doesn't... exceeds 30 seconds for some reason
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- spork
- Brewmaster


- Joined: Sep 22, 2003
- Posts: 6134
- Loc: Seattle, WA
- Status: Offline
Hmm.. before I go any further here, may I ask what this accomplishes?
Code: [ Select ]
$result[$column_name] = $result[$column_name];
The Beer Monocle. Classy.
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8212
- Loc: USA
- Status: Offline
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.
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.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- spork
- Brewmaster


- Joined: Sep 22, 2003
- Posts: 6134
- Loc: Seattle, WA
- Status: Offline
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8212
- Loc: USA
- Status: Offline
I fixed it with the following code
Looks like I can't have my own defined fetch_row function in there... but it looks like
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 = ' ';
}
$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;
}
$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 = ' ';
}
$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;
}
- $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 = ' ';
- }
- $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;
- }
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;
}
{
// 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;
}
- 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;
- }
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8212
- Loc: USA
- Status: Offline
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\"> ◊ <em>Century Sites</em> → <em><a href=\"index.php\">Index</a></em> → <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 = ' ';
}
// 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 © 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;
}
}
?>
/*
* 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\"> ◊ <em>Century Sites</em> → <em><a href=\"index.php\">Index</a></em> → <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 = ' ';
}
// 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 © 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;
}
}
?>
- <?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\"> ◊ <em>Century Sites</em> → <em><a href=\"index.php\">Index</a></em> → <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 = ' ';
- }
- // 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 © 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;
- }
- }
- ?>
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8212
- Loc: USA
- Status: Offline
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
Other than that, it works and it doesn't take 30 seconds to do it

Any help on this would be great
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;
}
?>
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;
}
?>
- <?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;
- }
- ?>
Other than that, it works and it doesn't take 30 seconds to do it
Any help on this would be great
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8212
- Loc: USA
- Status: Offline
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 12 posts
- Users browsing this forum: No registered users and 105 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
