Forum Moderators: coopster

Message Too Old, No Replies

Selecting only part of MYSQL result

Like l *word*

         

Knowles

1:05 am on Feb 16, 2004 (gmt 0)

10+ Year Member



Ok I am writting a script where the first time you look at something it inserts a word, on that shouldnt be mistakenly typed in. The script is pulling from a MySQL DB. I need to have a way to check for the word and if present not readd the word so it doesnt replicate over and over again.

echo "<TEXTAREA name=\"comments\" rows=\"8\" cols=\"20\">$displaycall[9]

TheWord:</TEXTAREA>";

What is happening right now every time this updated it adds TheWord: over and over again. Any ideas how to check inside of the $displaycall[9] for TheWord:?

jatar_k

1:36 am on Feb 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



can you do a select and a mysql_num_rows to see if it returned anything before inserting?

Knowles

1:45 am on Feb 16, 2004 (gmt 0)

10+ Year Member



Well when its is updated again I want the orginal text (what is echoed before TheWord:) and what is entered after TheWord; to be placed in the DB. So unless I am miss understanding the num_rows would return something but it would be to late at that point because the submit button has been pressed and $comments is set.

vincevincevince

1:51 am on Feb 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how about first checking if the word is there?

$sql="SELECT ......... WHERE `field` LIKE %word%";
if (!$r=mysql_fetch_row($mysql_query($sql)) .... go on and update itl

Knowles

1:54 am on Feb 16, 2004 (gmt 0)

10+ Year Member



I didnt explain this good. Even though the word is there its going to need to be updated. What I am trying to figure out is on the display side, due to the fact that I add TheWord: in the echo so its added every time. I need something simular to this:

if ($displaycall['9'] contains TheWord:){
Echo this;
}else{
Echo TheWord;
}

Does that make sense? I am just not sure what 'contains' could mean.

coopster

1:17 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



strpos() [php.net] may work for you:

if (!strpos($displaycall['9'], 'TheWord') === false) {
Echo this;
} else {
Echo TheWord;
}

Knowles

1:55 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



Now that is a needle in a haystack! That did work thank you coopster, I was just starting to search PHP.net but I dont think I would have found this!

coopster

2:24 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sure.

The cool part is the PHP developers have come out with a case-insensitive version as well (stripos [php.net]) but it is available in PHP 5 CVS only right now. I didn't realize this until I received errors when I tried to use it the other day on a PHP 4.3.4 installation...bummer. I had to resort to the following workaround for case-insensitive:


$found = (stristr($haystack, 'needle') === false)? false : true;

I only wanted to know if case-insensitive 'needle' was found, so I wanted to use the stripos [php.net] function which is faster and less memory intensive than the stristr [php.net] function. Oh well, the workaround will have to do for now.

ergophobe

4:49 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sure enough...


Note: If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead.

I never noticed that and have been using strstr(). I think two minutes with grep will speed up a certain script that calls strstr() a lot.

Thanks coopster

Tom