Page 1 of 1

Double query or workaround needed.

Posted: Fri Feb 23, 2007 1:03 pm
by UrButtFullOfArr0ws
I have a registration page which verifies first if there is a duplicate email, using

Code: Select all

$query = "select * from accounts where email='" . mysql_real_escape_string($_POST[email]) . "'";
and if there is no duplicate, it will then go on and create an index based on the rows found in the database like this:

Code: Select all

$query1 = mysql_query("select * from accounts",$con);
$dummy = mysql_num_rows($query1);
If ($dummy == 0 OR $dummy == null) { //not sure about the null thing, any enlightment?
$dummy1 = $dummy;
}
Else {
$dummy1 = $dummy + 1; // would "++" work? ?
}
Now i already know that 2 same queries don't work, but, is there a work around?
The first query is successfull and of course the second fails.

Code: Select all

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:\inetpub\wwwroot\verifyreg.php on line 45

Duplicate entry '1' for key 1
is the result when i run the code.

Queries and results echoed with a new email give this:

Code: Select all

select * from accounts where email='d@d.com'
select * from accounts
Array ( )
Same for duplicate email:

Code: Select all

select * from accounts where email='a@a.com'
select * from accounts
Array ( [0] => Array ( [MyIndex] => 1 [email] => a@a.com [pass] => 123456789 [admin] => 0 ) )
Admin field is not yet used but it's good to be there :D
Wide open to anything :)
Thx beforehand.

EDIT: Forgot to state the problem, even though it's written in the title xD.
If there is any way to do both queries with my way or any other way to get that particular job done.

Posted: Fri Feb 23, 2007 1:18 pm
by kaszu
$query = "select * from accounts where email='" . mysql_real_escape_string($_POST[email]) . "'";
Has an mistake in $_POST[email], it should be $_POST['email'] (but probably its just typo here not in real code).

You don't need to create index, because you can set "auto increment" for field MyIndex, then in insert just skip that field and mysql will create it for you (yes it will be unique):

Code: Select all

INSERT INTO accounts (email, pass, admin) VALUES (MY_EMAIL, MY_PASS, MY_ADMIN_VALUE)

Posted: Fri Feb 23, 2007 1:37 pm
by UrButtFullOfArr0ws
1)Yes that code i wrote it i didn't copy-paste it.
2)$_POST works without quotes (at least for me????) with same results, and that's not the problem anyway.
3)I'm well aware of the auto-increment function of mysql. I prefer to use my own indexing system for reasons not even i know :D... It goes back a long time so i can't remember. I will eventually come back to that if i find no other way for this.

Posted: Fri Feb 23, 2007 1:54 pm
by kingconnections
So you can run two queries, but I believe that you have to use a different connection string.

For example:

Code: Select all

//1st query 

$dbConn=connectToEpo(); /// connection string goes here
$query="select * from blah";
$result = mssql_query($query, $dbConn);
while ($row = mssql_fetch_assoc($result)){

do stuff
}

//2nd query

$dbConn2=connectToEpo(); /// connection string goes here
$query2="select * from blah";
$result2 = mssql_query($query2, $dbConn2);
while ($row = mssql_fetch_assoc($result2)){

do stuff
}

// You may be able to actually close the database connection then reopen it again using same resource 
ie 

mysql_free_result($result);

// Closing connection
mysql_close($link);



// then proceed to do it again
I hope this helps!

Posted: Fri Feb 23, 2007 1:59 pm
by UrButtFullOfArr0ws
Ehm... care to explain abit more about how exactly that would make it different ?
Thx for your answer.

Posted: Fri Feb 23, 2007 2:06 pm
by kingconnections
It makes it different because, instead of trying to use a db connection that is already open, it creates a new one.
You can have 2 seperate db connections open, connecting to the same db. But it has a hard time using an open resource that is still in use from the 1st query.

You wanted a way to do both querys, this will allow you to do it. You can't use the same resource for 2 queries, but you can create an entire new connection.


Oh you also have to rename the $results to $results2 or whatever.