views:

228

answers:

4

I have a PHP script which takes a value from a row in my MySQL database, runs it through a function, and if it determines it's true returns one value, and if it's false, it needs to go to the next value in the database and check that one until eventually one returns true. I think I need to use mysql_fetch_assoc, but I'm not really sure in what way to use it... I wish I could post my code to be more specific, but it's a lot of code and most of it has no bearing on this issue...

+2  A: 

Is the "function" something you could do in the database instead? It's really inefficient to process every row in the table to check for some type of condition. That's exactly what databases are good at, namely, processing queries efficiently and getting answers to you quickly.

So I'd recommend looking at how to do it all on the database side so that your PHP code is just fetching the end result (i.e. rows filtered by the function). Maybe if you provide more details of what your "function" is doing, a more specific answer can be provided.

dcp
Oh, no, I need to check it against a PHP session variable. It won't have to handle much data, it won't go through more than two or three or so before it would come up true.
ropbhardgood
You could always use the PHP session variable as a parameter in your query: SELECT * from mytable where col1 = <PHP session variable>
dcp
I would do that, dcp, but I'm actually trying to find one where one of the values does NOT contain the session variable. Do you know if there is a way to do this with a MySQL command?
ropbhardgood
Sure, SQL has that feature as well. You'd use the <> operator, which is "NOT EQUAL"SELECT * from mytable where col1 <> <PHP session variable>
dcp
Wow, I wish I'd known about that operator before... Maybe I should have looked this up before I started.Um, the thing is the field I'm checking has a bunch of values separated by value1:value2|value1:value2|value1:value2 and so on... It's usernames and their submitted values, which I needed to squeeze into one column for various reasons, and I need to check if just the username, or value1, is not in the value. I've been using explode() to separate and check... I'm afraid this MySQL operator would check the value2's as well, right?..
ropbhardgood
Wait, I've got it, I'd just tell it to look for the sessionvariable followed by a colon. Would it be able to search within the value and not just try to match the entire value?
ropbhardgood
A: 

You can use mysql_fetch_array, and just jump on the values fetched using $row[id] and not $row['name']. Say your function returns true, you'd just use $row[lastid+1].

If the ID isn`t incremental, this could work :

$qry_result = mysql_query($qry) or die(mysql_error());
while ($row = mysql_fetch_array($qry_result)) {
    $result = yourfunction($row['whatever');
    if ($result != false)
         break;
}

Also there is a php function next() which advances a pointer to the next array element. In your function you could implement an array builder, and then cycle between the elements with this. Depends on what your function actually does or script purpose is. It could lead to some load if there are alot of results.

Adrian A.
Interesting. Would this work when the primary key is not incremental?
ropbhardgood
Updated the solution for your case with non-incremental.
Adrian A.
A: 

Try something like this:

SELECT * FROM table WHERE field NOT LIKE '%$sessionvar:%';

I think that is what you are after?

Coop182
I think this is going to be helpful. Thank you.
ropbhardgood
A: 
  1. You should not check database this way, as mentioned above. Database has a little difference from the plain text file.
  2. You should not have a field in your database that has a bunch of values separated by value1:value2|value1:value2|value1:value2. It must be separate fields. Database has a little difference from the plain text file and you better learn it.
Col. Shrapnel
Thanks for that valuable input.
ropbhardgood