Consider switching to PDO too; you won't have to deal with the `mysql_real_escape_string` nonsense and improve security.
Jevon On Thu, Jul 1, 2010 at 11:00 AM, Simon J Welsh <[email protected]> wrote: > > On 30/06/2010, at 5:26 PM, Skunk wrote: > > > Hi, first post and it's off topic... sorry. > > > > I'm trying to find a better way of comparing two $_POST vars and > > checking if they are both in the same MYSQL table column. > > > > Currently it's done like this: > > > > $query ="SELECT * FROM groups"; > > for($i=1; $i <= $fields; $i++){ > > $result=mysql_query($query,$dbconn); > > while($row=mysql_fetch_row($result)) { > > if($_POST['item1'] != $row[$i]){ > > // continue search > > }else{ > > $column1 = $i; > > } > > } > > } > > $query ="SELECT * FROM groups"; > > for($i=1; $i <= $fields; $i++){ > > $result=mysql_query($query,$dbconn); > > while($row=mysql_fetch_row($result)) { > > if ($_POST['item2'] != $row[$i]){ > > // continue search > > }else{ > > $column2 = $i; > > } > > } > > } > > if ($column1 == $column2){ > > $message='Same grid'; > > } > > > > There must be a better way but I don't know it. > > Any help appreciate - just point me in the right direction. > > > > Cheers > > Andrew > > Well, you can optimise this rather a lot. Firstly, you can do it with only > one loop and without the nesting, and use inbuilt functions for most of it. > Following code typed straight into Mail, may not work. General disclaimer > here. > > $query = 'SELECT * FROM groups'; > $res = mysql_query($query, $dbconn); > $col = false; > while ($row = mysql_fetch_assoc($res)) { > $col = array_search($_POST['item1'], $row); > if ($col !== false) break; > } > if ($col !== false) { > $query = 'SELECT ' . $col ' . FROM groups WHERE ' . $col . ' = \'' . > mysql_real_escape_string($_POST['item2']) . '\''; > if (mysql_num_rows(mysql_query($query, $dbconn))) { > $message = 'Same grid'; > } > } > > --- > Simon Welsh > Admin of http://simon.geek.nz/ > > Who said Microsoft never created a bug-free program? The blue screen never, > ever crashes! > > http://www.thinkgeek.com/brain/gimme.cgi?wid=81d520e5e > > > > > -- > NZ PHP Users Group: http://groups.google.com/group/nzphpug > To post, send email to [email protected] > To unsubscribe, send email to > [email protected]<nzphpug%[email protected]> > -- NZ PHP Users Group: http://groups.google.com/group/nzphpug To post, send email to [email protected] To unsubscribe, send email to [email protected]
