[MySQL] How to stop creating more then 1 of the same row? -
Toni - 13.09.2010
Hi there,
I can't seem to find the function, or how to create a row, and checking if it exists. If it does, then it doesn't create the row, but if it doesn't it does create the row.
I tried with mysql_num_rows...but shortly after that I released that wasn't going to work.
Re: [MySQL] How to stop creating more then 1 of the same row? -
Mauzen - 13.09.2010
You would have to try to select that row first (SELECT * FROM table WHERE field1=value, field2=value,...) and then use the mysql_num_rows. This should work i think, sorry if you already did it that ways
Re: [MySQL] How to stop creating more then 1 of the same row? -
Toni - 13.09.2010
Well...I don't think selecting from rows with specific values is gonna help...it would also be a big pain in the ass.
Re: [MySQL] How to stop creating more then 1 of the same row? -
Calgon - 14.09.2010
Run a select query with the conditions of your query and then use mysql_num_rows, if a result equal to 0 is returned, allow the query to process.
Re: [MySQL] How to stop creating more then 1 of the same row? -
Toni - 14.09.2010
Ah okay. Thanks Calgon.
I'll edit, and post a Code i'll make rl fast.
EDIT:
pawn Код:
function CreateBusiness(Bizzname[], price, Float:EnterX, Float:EnterY, Float:EnterZ)
{
format(str, sizeof(str), "SELECT * FROM `bizzinfo`");
mysql_query(str);
mysql_store_result();
if(mysql_num_rows() != 0)
format(str, sizeof(str), "INSERT INTO `bizzinfo` (BizzName, Owner, Price, PayAmount, EntX, EntY, EntZ) VALUES ('%s', 'For Sale', %d, 5000, %f, %f, %f)", Bizzname, price, EnterX, EnterY, EnterZ);
mysql_query(str);
mysql_free_result();
return 1;
}
This is all I had. This still lets me create more then 1 of the same business.
Re: [MySQL] How to stop creating more then 1 of the same row? -
Calgon - 14.09.2010
Quote:
Originally Posted by The Toni
Ah okay. Thanks Calgon.
I'll edit, and post a Code i'll make rl fast.
EDIT:
pawn Код:
function CreateBusiness(Bizzname[], price, Float:EnterX, Float:EnterY, Float:EnterZ) { format(str, sizeof(str), "SELECT * FROM `bizzinfo`"); mysql_query(str); mysql_store_result(); if(mysql_num_rows() != 0) format(str, sizeof(str), "INSERT INTO `bizzinfo` (BizzName, Owner, Price, PayAmount, EntX, EntY, EntZ) VALUES ('%s', 'For Sale', %d, 5000, %f, %f, %f)", Bizzname, price, EnterX, EnterY, EnterZ); mysql_query(str); mysql_free_result(); return 1; }
This is all I had. This still lets me create more then 1 of the same business.
|
I said with the conditions, you need to execute a query like this:
SELECT * FROM `bizzinfo` WHERE x, y, z, blah, blah blah
Re: [MySQL] How to stop creating more then 1 of the same row? -
Toni - 14.09.2010
Quote:
Originally Posted by Calgon
I said with the conditions, you need to execute a query like this:
SELECT * FROM `bizzinfo` WHERE x, y, z, blah, blah blah
|
Well yeah... I know what you mean. But wouldn't I need to do different coordinates for
every business I add?
Re: [MySQL] How to stop creating more then 1 of the same row? -
Calgon - 14.09.2010
Quote:
Originally Posted by The Toni
Well yeah... I know what you mean. But wouldn't I need to do different coordinates for every business I add?
|
I don't understand what you're trying to do. If you're trying to ensure certain values aren't the same, then use the select w/ the where conditionals in your query.
Re: [MySQL] How to stop creating more then 1 of the same row? -
Toni - 14.09.2010
Quote:
Originally Posted by Calgon
I don't understand what you're trying to do. If you're trying to ensure certain values aren't the same, then use the select w/ the where conditionals in your query.
|
Well, this is what I meant.
That code I posted up there (CreateBusiness function), will add a business to the database even if it already exists.
Say I did this:
pawn Код:
CreateBusiness("A Test"....);
CreateBusiness("AnotherTest"...);
Every time I start my server, it will add those 2 business's again, and again, and again, every time I reset my server.
EDIT:
Never mind...its all good. I figured it out.
Re: [MySQL] How to stop creating more then 1 of the same row? -
Jaymzanator - 14.09.2010
guys - even faster:
Код:
SELECT HIGH_PRIORITY COUNT(`idbusiness`) FROM `business` WHERE x=? AND y=?
Код:
if (!bcCount) {
//row doesnt exist with x=? and y=?
//insert row
}
ps. high_priority can increase accuracy of count on very busy mysql servers. otherwise you probably wont need it