Assistance Needed For A Mysql Script
#1
Toungue 
Greetings,
I am working on a mod, and need to know how to write a script where if a user navigates to a page, it inserts sql into an already existing table. I also need one that clears all data from an existing table. Thanks in advance!
Reply
#2
These will delete all data in the tables. The difference is that truncate will reset all id settings (eg auto_increment), delete from will keep all ids at the same values
TRUNCATE [i]table_name[/i]
or
DELETE FROM [i]table_name[/i]

As for inserting data, lots of ways
Inserting one record

INSERT INTO [i]table_name[/i] VALUES([i]column_data1[/i], [i]column_data2[/i])
- This is the very basic sql. However, all of the columns in the table must be accounted for in the VALUES() part, with columns seperated by commas. If you add or delete a column then your sql will have to change because the number of columns has changed.

INSERT INTO [i]table_name[/i]([i]column_name1[/i], [i]column_name2[/i]) VALUES([i]column_data1[/i], [i]column_data2[/i])
- Much better way. You specify the column names and then the corresponding values in the VALUES() part. The columns can be in any order. This is much safer, if you add a column, the script will not crash, because you've specified which columns will have data.



As for using Mybb to do it.
$db->query("INSERT INTO ....");
Will do the trick. If you want it to continue if there are any mysql errors then use
$db->query("INSERT INTO ....", TRUE);
(If you are selecting data, you will need to capture the query_result by using "$result = $db->query("SELECT * FROM ....");")


You can also use the new function (PR2)
$data_array = new array();
$data_array['[i]column_name1[/i]'] = [i]column_data1[/i];
$data_array['[i]column_name1[/i]'] = [i]column_data1[/i];
$db->insert_query([i]table_name[/i], $column_data);
It takes a table name and an array of data. It uses the keys as the columns and the values as the values. Very useful.
Reply
#3
Thanks a lot!
Reply
#4
What would the exact code be for adding an insert sql script into a plugin?

I would like to insert a bunch of badwords in the "badwords" table and the the three variables would be "bid", "badword" and "replacement".

I would really appreciate if you could show me an example on how to do this. Thank you very much for your time!

--------------------
bump! Smile
Reply
#5
$sql = "INSERT INTO ".TABLE_PREFIX."badwords (badword,replacement) VALUES('$badword', '$replacement')";
Reply
#6
Excellent, thanks!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)