[MySQL] How to stop creating more then 1 of the same row?
#1

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.
Reply
#2

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
Reply
#3

Well...I don't think selecting from rows with specific values is gonna help...it would also be a big pain in the ass.
Reply
#4

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.
Reply
#5

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.
Reply
#6

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
Reply
#7

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?
Reply
#8

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.
Reply
#9

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.
Reply
#10

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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)