msaz87 Posted December 3, 2009 Share Posted December 3, 2009 Hey All, Just a quick question... I'm looking to complete an if/then statement in PHP by checking to see if there's an entry in a DB table. Basically, I'm wondering how to go about making a query that says: $query = " SELECT * FROM table WHERE col = '$whatimlookingfor' "; So if there is no column in that table matching the variable, it would make $truefalse = '0', which could then be used later in the PHP if/then... Hopefully that makes sense... any help or guidance is appreciated. Thanks! Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 3, 2009 Share Posted December 3, 2009 Use mysql_nym_rows to see if this query returns 0 or more rows. Quote Link to comment Share on other sites More sharing options...
Kryptix Posted December 3, 2009 Share Posted December 3, 2009 Use mysql_nym_rows to see if this query returns 0 or more rows. He means mysql_num_rows Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 3, 2009 Share Posted December 3, 2009 I do. You now may deduce I have a civilised QWERTY keyboard as opposed to barbaric QWERTZ or esoteric Dvorak layout. Fortunately PHP manual's search yields quite good results in this case. Quote Link to comment Share on other sites More sharing options...
msaz87 Posted December 3, 2009 Author Share Posted December 3, 2009 I do. You now may deduce I have a civilised QWERTY keyboard as opposed to barbaric QWERTZ or esoteric Dvorak layout. Fortunately PHP manual's search yields quite good results in this case. I was able to deduce as much... this looks like it'll work perfectly -- thanks very much for all your help! Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 3, 2009 Share Posted December 3, 2009 Don't use that query to see if something exists and don't use the mysql_num_rows() method. The query you've presented will return the data in all of the rows. Let's say your average record is 500 bytes. Now let's say your database contains 1,000 records that meet the criteria. You'll be returning all 1,000 records at 500 bytes per record. 1) This is making the database do way more work than it has to. 2) Your sending excessive data through the connection between your application and the database (500,000 bytes in my example). Use something like this: <?php $escaped_variable = mysql_real_escape_string( $_POST['some_input'] ); $count_stmt = " SELECT COUNT(*) AS `n` FROM `yourtable` WHERE `thecol`={$escaped_variable} "; $q_handle = mysql_query( $count_stmt ); $count = 0; if( $q_handle ) { $count_row = mysql_fetch_object( $q_handle ); if( $count_row ) { $count = $count_row->n; } } echo "There are {$count} records matching the criteria."; ?> Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 3, 2009 Share Posted December 3, 2009 Or just use LIMIT 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.