don11 Posted February 23, 2011 Share Posted February 23, 2011 Hello, I have a problem. I run this query: $query = "SELECT column1 FROM table WHERE column2='$kk'"; $which = $link1; mysql_query($query,$which); some codes.... It is displaying all data correct but only when $kk exists in column2 and when $kk does not exist, it display blank page. So, i want to do a check before running above query to make sure that $kk exists in column2. How to check it before running query? Quote Link to comment https://forums.phpfreaks.com/topic/228580-help-checking-before-doing-query-in-php-with-mysql/ Share on other sites More sharing options...
trq Posted February 23, 2011 Share Posted February 23, 2011 You can't check without executing the query. The problem is likely that your not checking the results. $query = "SELECT column1 FROM table WHERE column2='$kk'"; $which = $link1; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { // it is safe to use $result as it contains data } else { // no result where found } } else { // query failed. } This is the basic syntax for ALL select queries. Quote Link to comment https://forums.phpfreaks.com/topic/228580-help-checking-before-doing-query-in-php-with-mysql/#findComment-1178561 Share on other sites More sharing options...
Muddy_Funster Posted February 23, 2011 Share Posted February 23, 2011 ... This is the basic syntax for ALL select queries. That's a bit of a strong statement, perticularly with the capitalisation of ALL. I dissagree, partly: The first if is not, in my opinion, the best way of handeling an execution error - use of a try would, I think, be better. Personal prefference on my part sees that I never nest variables within an SQL string, but break the string and add the variable with dot connectors. Just because I have come across a fair few occasions whre people (myself included at the start) have tried to nest a variable within a single quoted string. oh yeah....and I personaly have never build a query with "$which = $link1;" in it . Good template though - could be well worth starting a sticky with simple templates like this in it for a quick refference (though you may have to restrict posting in it to admins/mods so tubes like me don't ruin it ) Quote Link to comment https://forums.phpfreaks.com/topic/228580-help-checking-before-doing-query-in-php-with-mysql/#findComment-1178586 Share on other sites More sharing options...
don11 Posted February 23, 2011 Author Share Posted February 23, 2011 Checking the results after executing the query worked. Thanks alot phpfreaks staff helped me again. Thanks once again. Quote Link to comment https://forums.phpfreaks.com/topic/228580-help-checking-before-doing-query-in-php-with-mysql/#findComment-1178588 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.