$conn = mysql_connect("localhost", "some_user", "password"); if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("some_database")) { echo "Unable to select mydbname: " . mysql_error(); exit; } $sql = "SELECT id FROM some_table"; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } while ($row = mysql_fetch_assoc($result)) { echo $row["id"]; echo "\r\n"; } mysql_free_result($result); Code (markup): So it prints the result like this: 112234 142134 145534 etc. I also have a $row["adid"] that is 112234 but when I try to do this, nothing happens: <?php if($row["adid"] == $row["id"]){ ?> DO SOMETHING <?php } else { ?> DO NOTHING <?php } ?> Code (markup): Not sure how to make this work. PS Yes, I know the function is deprecated.
If you're expecting to have row['adid'] from a query where you're asking to only fetch 'id' from the database... then you'll be out of luck. Either you need to specify both columns in the SELECT-query, or you need to rethink how you do things. And yes, you should redo the whole mysql_ thing.
Yeah, I am trying to add something new to something that I've had for a while. It's tough since I am not flexible in PHP / MySQL.
Eh... I'm confused? The code you pasted above has one SELECT-statement, which looks for "id". If you're doing another SELECT statement from another table to get the 'adid' one, then you'll have to do two loops / or something else - or join queries. Also, if you're gonna intersperse one or more loops with results from two queries, a wise move will be to name the results something different, so as to avoid messing up the content of "$row". So - are the 'adid' and 'id' in two different tables? You also seem to want to do nothing if there is no match, so why not just check to see if there is a match in the query? First, you do a query for 'adid', and loop through that result. WITHIN that loop, you check to see if the 'id' column matches the result from the 'adid' query. Something like this (pseudocode): $adid_query = mysql_query("SELECT adid FROM some_table"); while ($adid = mysql_fetch($adid_query)) { $check = $adid['adid']; $id_query = mysql_query("SELECT id FROM some_other_table WHERE id = $check"); // do something } PHP:
Although this does work, I wouldn't put the query in the while loop. Get the data needed, store it in 1 (or 2) array(s) and loop the array(s) (or use array_search) to make the comparison. or use inner join to retrieve only the records who have a match in both tables. select A.*,B.* from adid A inner join othertable B on A.ADID = B.SOMETHINGMATCHING Code (markup): maybe with where clause if value comes from form or so (?) where A.ADID=$_POST['someIntegerParam'] limit 1 Code (markup): ps : just to illustrate , I know it's not secure etc...
I wouldn't put the query in the loop either, but using mysql it is the only way. I would use prepared queries and just iterate through the prepared values.
there are other ways... in below (dirty example) you only query the db twice instead of << select count(adid) from some_table >> times $adid_query = mysql_query("SELECT adid FROM some_table"); while ($row1 = mysql_fetch($adid_query)) { $check = $row1['adid']; } $other_query = mysql_query("SELECT x FROM some_other_table"); while ($row2 = mysql_fetch($other_query)) { $x[] = $row2['x']; } $match = false; foreach($check as $val) { if(array_search($val, $x){ $match = true; break; } } if($match){ } Code (markup):