![]() |
![]() |
![]() |
||||
Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact us. |
![]() ![]() |
|
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed. |
|
Thread Tools |
![]() |
#1 | |
Confirmed User
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,111
|
![]() So I know that I can call information from the SQL database and output it and replace certain words using the REPLACE string but, how would I go about replacing everything in a specific column with a word, or set of words, without actually removing that columns data?
Quote:
Is there something like REPLACE_ALL I can use across the column or can I only replace certain substring occurrences? ![]() [quickedit] Also, does it matter if the string is numeric or alphanumeric too as far as the command goes? |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#2 |
Confirmed User
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,228
|
Your description is kind of odd but I think you are looking for CONCAT.
SELECT CONCAT(`animal`,'zombie') AS animal FROM pets WHERE `item` = 'dog' If you mean you want to replace dog, cat, and fish with 'zombie' then REPLACE(`animal`,'dog','zombie') If you want to replace multiple, then something like REPLACE(REPLACE(REPLACE(`animal`,'fish','zombie'), 'cat','zombie'),'dog','zombie')
__________________
Mechanical Bunny Media Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#3 | |
Confirmed User
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,111
|
Quote:
What if there are thousands of rows with different items, say 100 different items plus, is the only way to replace them by using each items value? |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#4 |
Confirmed User
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,111
|
Is there a command such as REPLACE_COLUMN which will just change the data in that specific 'pets' column irrespective of the rows value?
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#5 |
Confirmed User
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,228
|
Your description is still incredibly confusing, but there is no other replace query in mysql.
You can do this with php Code:
$replace = array("cat","dog","fish"); $with = "zombie"; $result = mysqli_query($dblink,"SELECT animal,id FROM pets"); while($row = mysqli_fetch_assoc($result)) { $row['animal'] = mysqli_real_escape_string($dblink,stri_replace($replace,$with,$row['animal'])); mysqli_query($dblink,"UPDATE pets SET animal = '".$row['animal']."' WHERE record_num = '".$row['id']."'"); }
__________________
Mechanical Bunny Media Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#6 | |
Confirmed User
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,111
|
Quote:
![]() Thank you for the help, I appreciate it ![]() |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#7 |
Confirmed User
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,228
|
Well, you're gonna have to input them one way or the other. You can pull them into the array from another mysql table if that helps.
__________________
Mechanical Bunny Media Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#8 |
Confirmed User
Industry Role:
Join Date: Apr 2019
Posts: 657
|
UPDATE table SET pets = 'zombie';
__________________
__________________ |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#9 |
Confirmed User
Industry Role:
Join Date: Jan 2003
Location: Nomad Land
Posts: 1,601
|
This, and you can mimic replace behavior with a where statement.
Code:
UPDATE table SET pets = 'zombie' WHERE pets = 'cat'; Code:
UPDATE table SET pets = 'zombie' WHERE pets IN ('cat','dog','mouse');
__________________
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#10 | |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,052
|
![]() Quote:
You are overthinking it, I think. You do not need to do a replace. Just select the string you want. Select 'zombie' as petname, other field, otherfield, etc. from pets where whatever. That way you get the data from the record but instead of the name you get zombie. But you can also do that when you print out the data. Select * from pets. While($row=...) { echo 'Pet name: Zombie' . ' ' . $row['pet_price'] . ' ' . whetever other data you want } I think that is what it sound like you are trying to do. .
__________________
All cookies cleared! |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#11 | |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,052
|
Quote:
.
__________________
All cookies cleared! |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#12 | |
Confirmed User
Industry Role:
Join Date: Jan 2003
Location: Nomad Land
Posts: 1,601
|
Quote:
In that case I would explicitly state the columns I'm selecting where one would simply be a string. ie: Code:
SELECT column1,column2,'zombie',column4 FROM table; OOPS, second on the ball.
__________________
![]() Last edited by natkejs; 09-16-2021 at 08:39 PM.. Reason: I see you already posted this in between our replies :) |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#13 |
Confirmed User
Industry Role:
Join Date: Jan 2003
Location: Nomad Land
Posts: 1,601
|
Too early morning here. So to clarify, sarettah gave the correct answer, and with an eye for detail with the "as petname" part so you can still access the column by its original name.
Apologies for the confusion, am in need of more coffee.
__________________
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#14 | |
Confirmed User
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,228
|
Quote:
So the row has a record with a piece of text that says 'I have a dog and a cat' and he wants to replace it with "I have a zombie and a zombie".
__________________
Mechanical Bunny Media Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#15 | |
Confirmed User
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,111
|
Quote:
So whether the row says dog, cat, bird, fish, etc. I want the output in the page to say Zombie. Going to play around with the suggestions above, thank you all for your help ![]() |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#16 | |
Confirmed User
Industry Role:
Join Date: Apr 2019
Posts: 657
|
If you don't need the data from the database and don't need to change it there's no reason to even access the database. Are you trolling?
$pet = 'zombie'; Quote:
"How do I replace whatever is in that 'pets' column with the word 'zombie' no matter if its dog, fish, cat, porcupine, etc?" "the replace is what I am trying to do but being lazy about it and not having to type out a few hundred individual words to replace with 'zombie'" "Is there a command such as REPLACE_COLUMN which will just change the data in that specific 'pets' column irrespective of the rows value?"
__________________
__________________ |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#17 | |
Confirmed User
Industry Role:
Join Date: Sep 2013
Location: The Netherlands
Posts: 805
|
Quote:
SET pets = zombie ? |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#18 | ||
Confirmed User
Industry Role:
Join Date: Sep 2013
Location: The Netherlands
Posts: 805
|
Quote:
Quote:
![]() ![]() |
||
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#19 |
Confirmed User
Industry Role:
Join Date: Apr 2019
Posts: 657
|
Yeah, that's the answer to what he was repeatedly asking for, as posted here.
But apparently his actual requirement is simply "How do I make the word 'zombie' appear on a web page". You don't get this on Stackoverflow.
__________________
__________________ |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#20 |
Too lazy to set a custom title
Industry Role:
Join Date: Oct 2006
Location: Vancouver
Posts: 30,986
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#21 | |
Confirmed User
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,228
|
Quote:
![]() "SELECT 'zombie' AS animal, id, some, other, columns FROM pets" If this is the solution to your problem, you need to take a step back and look at where you fucked up because you're fixing an issue you shouldn't have to begin with.
__________________
Mechanical Bunny Media Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#22 | |
(>^_^)b
Industry Role:
Join Date: Dec 2011
Posts: 7,224
|
Quote:
![]()
__________________
![]() I've referred over $1.7mil in spending this past year, you should join in. ![]() ![]() I make a lot more money in the medical field in a lab now, fuck you guys. Don't ask me to come back, but do join Chaturbate in my sig, it still makes bank without me touching shit for years.. ![]() |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#23 | |
Confirmed User
Industry Role:
Join Date: Sep 2013
Location: The Netherlands
Posts: 805
|
Quote:
![]() I suspect OP is just looking for something like string replace and just wants to change the output... $stringFromDB = "my cat can't swim..."; $newString = str_replace("cat", "zombie", $stringFromDB); //replace echo $newString; //print No clue how that animal name from the table column/row got inside that string in the first place... I mean, why the string isn't "my zombie can't swim...", in the first place... But hey, if it helps. ![]() |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#24 | ||
Confirmed User
Industry Role:
Join Date: Apr 2019
Posts: 657
|
Quote:
![]() Quote:
Would be a lot easier if they had posted some code though, rather than everyone else having to guess.
__________________
__________________ |
||
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#25 |
Confirmed User
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,111
|
Just to clarify, the reason I need to change the output in a specific column is because the data from that column is used in several blocks of text across a site.
Example: My best friend has a 'cat' for a pet that he feeds 'cat'food to on a daily basis at 9am. My friends 'cat' only likes a certain brand of 'cat'food. On another page its, a section with specifics that I do not want the word 'cat' to appear. instead something like 'pet' as placeholder text while I edit the row data to actually have a value instead of currently being empty so that once the column has data in it, I do not have to go in and edit the HTML or code on the page: Animal Type: 'cat' Brand: BrandName Cost: $12 Some rows do not have a value in them for 'cat' so until I get a value in there, I just want to use a generic word so that the page still 'reads' correctly, even if there is no specific data being displayed. So whether the word is dog, cat, fish, etc I want to use 'zombie' (or 'pet') as the word being displayed hence changing the output data without actually changing the entire data set, because what is in there, is correct for the other pages. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#26 |
Confirmed User
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,228
|
<?php if(!$row['animal']) { $row['animal'] = 'pet'; } ?>
__________________
Mechanical Bunny Media Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#27 |
Confirmed User
Industry Role:
Join Date: Sep 2013
Location: The Netherlands
Posts: 805
|
Assuming you are somewhere within the <?php { //your code } ?>, where you are getting data from the selected row. Basically, the code where you get the database data, you can use str_replace.
So, something like this: { //your code $newString = str_replace("cat", "pet", $row['pets']); // replaces cat with the word pet. echo $newString; //print output if you want } Replace $row['pets'], with the actual variable you are using $row[pet_name] or whatever. Edit: Nevermind, usek0nr4d's comment instead. It replaces empty values and prints pet as placeholder. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#28 |
Confirmed User
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,111
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |