Home > Archive > PHP Language > February 2007 > mySQL loop and string operation
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
mySQL loop and string operation
|
|
| Simon Harris 2007-02-04, 6:58 pm |
| Hi All,
I have a page which loops through many database records, running a string
function to strip HTML (Using RegEx/preg_replace). I am concerned that over
time, this is going to become very slow.
Few ideas I had:
a) Cache the HTML free version of the text in the DB, updating/creating each
records text when the page is loaded, based on the updated date of the
record.
b) Is there a faster way to strip the HTML? Could I do this on-the-fly at
mySQL level?
c) In ASP ( My native language! :-) ) I can load the results into an array,
then loop the array which is quicker. Can this be done in PHP?
Any ideas much appreciated.
Simon.
--
--
* Please reply to group for the benefit of all
* Found the answer to your own question? Post it!
* Get a useful reply to one of your posts?...post an answer to another one
* Search first, post later : http://www.google.co.uk/groups
* Want my email address? Ask me in a post...Cos2MuchSpamMakesUFat!
--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 3476 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!
| |
| Norman Peelman 2007-02-04, 6:58 pm |
| Simon Harris wrote:
> Hi All,
>
> I have a page which loops through many database records, running a string
> function to strip HTML (Using RegEx/preg_replace). I am concerned that over
> time, this is going to become very slow.
>
> Few ideas I had:
>
> a) Cache the HTML free version of the text in the DB, updating/creating each
> records text when the page is loaded, based on the updated date of the
> record.
> b) Is there a faster way to strip the HTML? Could I do this on-the-fly at
> mySQL level?
> c) In ASP ( My native language! :-) ) I can load the results into an array,
> then loop the array which is quicker. Can this be done in PHP?
>
> Any ideas much appreciated.
>
> Simon.
>
>
Yes, you can do the same in PHP. It can be done like so:
$dbc = mysql_connect('localhost','user','passww
ord');
$db = mysql_select_db('database',$dbc);
$query = "SELECT * FROM tablename WHERE ????"; // you get the idea. :)
$result = mysql_query($query,$dbc);
// grab rows into array $data with associative keys
while($data[] = mysql_fetch_array($result,MYSQL_ASSOC)
{
}
// subtract 1 as an extra row still gets created when the final
mysql_fetch_array fails.
$data_size = count($data)-1;
// loop through data
for($loop = 0;$loop <= $data_size; $loop++)
{
echo "{$data[$loop]}<br>";
$stripped = strip_tags($data[$loop]);
echo "$stripped<br>";
}
---
Norm
| |
| Simon Harris 2007-02-04, 6:58 pm |
| Norman,
Thank you very much. I will give this a go.
Simon.
"Norman Peelman" <npeelman@cfl.rr.com> wrote in message
news:45c5e5a6$0$18883$4c368faf@roadrunne
r.com...
> Simon Harris wrote:
>
> Yes, you can do the same in PHP. It can be done like so:
>
> $dbc = mysql_connect('localhost','user','passww
ord');
> $db = mysql_select_db('database',$dbc);
>
> $query = "SELECT * FROM tablename WHERE ????"; // you get the idea. :)
> $result = mysql_query($query,$dbc);
>
> // grab rows into array $data with associative keys
> while($data[] = mysql_fetch_array($result,MYSQL_ASSOC)
> {
> }
>
> // subtract 1 as an extra row still gets created when the final
> mysql_fetch_array fails.
> $data_size = count($data)-1;
>
> // loop through data
> for($loop = 0;$loop <= $data_size; $loop++)
> {
> echo "{$data[$loop]}<br>";
> $stripped = strip_tags($data[$loop]);
> echo "$stripped<br>";
> }
>
> ---
>
> Norm
--------------------------------------------------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 3496 spam emails to date.
Paying users do not have this message in their emails.
Try SPAMfighter for free now!
| |
| Norman Peelman 2007-02-05, 3:58 am |
| Norman Peelman wrote:
> Simon Harris wrote:
>
> Yes, you can do the same in PHP. It can be done like so:
>
> $dbc = mysql_connect('localhost','user','passww
ord');
> $db = mysql_select_db('database',$dbc);
>
> $query = "SELECT * FROM tablename WHERE ????"; // you get the idea. :)
> $result = mysql_query($query,$dbc);
>
> // grab rows into array $data with associative keys
> while($data[] = mysql_fetch_array($result,MYSQL_ASSOC)
> {
> }
>
> // subtract 1 as an extra row still gets created when the final
> mysql_fetch_array fails.
> $data_size = count($data)-1;
>
> // loop through data
> for($loop = 0;$loop <= $data_size; $loop++)
> {
echo "{$data[$loop]['field_name']}<br>";
> $stripped = strip_tags($data[$loop]['field_name']);
> echo "$stripped<br>";
> }
>
Simon,
I made a simple mistake... take note of corrections within the for..loop.
Norm
|
|
|
|
|