uwannadonkey Posted February 5, 2008 Share Posted February 5, 2008 how can i check if a row EXISTS in my mysql table, through php. for example, i wanna check if ID 6 exists in table, how would i do that. Quote Link to comment https://forums.phpfreaks.com/topic/89491-phpmysql-help/ Share on other sites More sharing options...
pocobueno1388 Posted February 5, 2008 Share Posted February 5, 2008 <?php $query = mysql_query("SELECT col FROM table WHERE ID=6")or die(mysql_error()); if (mysql_num_rows($query) > 0){ //the row exists } else { //doesn't exist } Quote Link to comment https://forums.phpfreaks.com/topic/89491-phpmysql-help/#findComment-458363 Share on other sites More sharing options...
awpti Posted February 5, 2008 Share Posted February 5, 2008 <?php function row_exists($id) { $res = mysql_query("SELECT * FROM my_table WHERE my_id_field = {$id}"); if(mysql_num_rows($res) === 1) { return TRUE; } else { return FALSE; } } example; <?php if(row_exists($id)) { // it exists } else { // it doesn't exist } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89491-phpmysql-help/#findComment-458364 Share on other sites More sharing options...
pocobueno1388 Posted February 5, 2008 Share Posted February 5, 2008 <?php function row_exists($id) { $res = mysql_query("SELECT * FROM my_table WHERE my_id_field = {$id}"); if(mysql_num_rows($res) === 1) { return TRUE; } else { return FALSE; } } example; <?php if(row_exists($id)) { // it exists } else { // it doesn't exist } ?> Thats a bit overkill, don't you think? Quote Link to comment https://forums.phpfreaks.com/topic/89491-phpmysql-help/#findComment-458365 Share on other sites More sharing options...
uwannadonkey Posted February 5, 2008 Author Share Posted February 5, 2008 wow, thanks for your responses! ill test it out and get back to you all. i prefer awpti's snippet, actually. Quote Link to comment https://forums.phpfreaks.com/topic/89491-phpmysql-help/#findComment-458367 Share on other sites More sharing options...
awpti Posted February 5, 2008 Share Posted February 5, 2008 Thats a bit overkill, don't you think? How many times is he going to use it? If more than once; create a function. Could even extend that function to allow variable table and field names.. oh, the magic of such functions goes on and on. Code repetition is bad. Quote Link to comment https://forums.phpfreaks.com/topic/89491-phpmysql-help/#findComment-458372 Share on other sites More sharing options...
Aureole Posted February 5, 2008 Share Posted February 5, 2008 Why are you selecting *... that is overkill... you only need to select one thing, e.g. the id... EDIT: I don't think he was saying the function was overkill, he was saying SELECT * was overkill when a simple SELECT `id` or SELECT `column` would do. Quote Link to comment https://forums.phpfreaks.com/topic/89491-phpmysql-help/#findComment-458375 Share on other sites More sharing options...
awpti Posted February 5, 2008 Share Posted February 5, 2008 Why are you selecting *... that is overkill... you only need to select one thing, e.g. the id... Likely he's using MySQL and the MyISAM engine. "SELECT *" has the same performance as "SELECT field". If it's innodb, that's different. Doubt it's innodb. Quote Link to comment https://forums.phpfreaks.com/topic/89491-phpmysql-help/#findComment-458378 Share on other sites More sharing options...
Aureole Posted February 5, 2008 Share Posted February 5, 2008 How can selecting EVERYTHING from a table be quicker than/the same as selecting only one thing? Quote Link to comment https://forums.phpfreaks.com/topic/89491-phpmysql-help/#findComment-458383 Share on other sites More sharing options...
awpti Posted February 5, 2008 Share Posted February 5, 2008 How can selecting EVERYTHING from a table be quicker than/the same as selecting only one thing? It's just how the ISAM engine works. Do some performance benchmarks. The only thing "SELECT *" does is give you a smidgen more data overhead. Quote Link to comment https://forums.phpfreaks.com/topic/89491-phpmysql-help/#findComment-458390 Share on other sites More sharing options...
Aureole Posted February 5, 2008 Share Posted February 5, 2008 Well you probably know more than me, so I'll just take your word for it. Quote Link to comment https://forums.phpfreaks.com/topic/89491-phpmysql-help/#findComment-458392 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.