pavanpuligandla Posted October 16, 2008 Share Posted October 16, 2008 hii.. i'm deleting records from mysql table.,i want to show an error message like "no records to delete" if there are 0 records in that table, else "records deleted". below is my query for deleting records, <?PHP include "authn.php"; //Connect to mysql server $link=mysql_connect("localhost","root",""); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db=mysql_select_db("cge"); if(!$db) { die("Unable to select database"); } $type=$_POST['type']; $batch=$_POST['batch']; $query = mysql_query("DELETE FROM `1in` WHERE CONVERT(`1in`.`Type` USING utf8) = '$type' AND CONVERT(`1in`.`Batch` USING utf8) = '$batch'") or die(mysql_error()); if(mysql_num_rows($query)>0) { include("in1delup.php"); } else { echo 'No records to delete'; } ?> but i'm getting warning message as : Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\Project\in1delete.php on line 18 No records to delete can anyone help me out in using if else condition properly.. many thanks, pavan.p Quote Link to comment https://forums.phpfreaks.com/topic/128671-solved-mysql-query-help/ Share on other sites More sharing options...
beansandsausages Posted October 16, 2008 Share Posted October 16, 2008 I WOULD DO SOME THING LIKE THIS: <?php $conn = mysql_connect("localhost", "mysql_user", "mysql_password"); if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("mydbname")) { echo "Unable to select mydbname: " . mysql_error(); exit; } $sql = "SELECT id as userid, fullname, userstatus FROM sometable WHERE userstatus = 1"; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { // NOTHING TO DELETE exit; } while ($row = mysql_fetch_assoc($result)) { // DELETE } mysql_free_result($result); ?> Took of the php.net manual : http://uk.php.net/manual/en/function.mysql-fetch-assoc.php was to lazy to type it out. Quote Link to comment https://forums.phpfreaks.com/topic/128671-solved-mysql-query-help/#findComment-666905 Share on other sites More sharing options...
R4nk3d Posted October 16, 2008 Share Posted October 16, 2008 I think that the lines: $query = mysql_query("DELETE FROM `1in` WHERE CONVERT(`1in`.`Type` USING utf8) = '$type' AND CONVERT(`1in`.`Batch` USING utf8) = '$batch'") or die(mysql_error()); if(mysql_num_rows($query)>0) { include("in1delup.php"); } The $query line shouldnt have the actual mysql_query command on it, just the "DELETE FROM....". Cause right now, ur deleting all the rows, then asking if there are any left, so of course there wont be, u just deleted them. I dont know if im right, someone else needs to confirm this. Quote Link to comment https://forums.phpfreaks.com/topic/128671-solved-mysql-query-help/#findComment-666919 Share on other sites More sharing options...
pavanpuligandla Posted October 16, 2008 Author Share Posted October 16, 2008 hii... the table contains many records based on "Type" and "Batch".. so if i'm deleting based on those variables.. ex: if type="UG" and Batch="08", then the records will be deleted are the rows having "UG" and "08" only, not other records like "PG" and "09".. rite. so now my table has no "ug" type and "08" batch rows.. so if i try to delete them, i should get an error message like "no records to delete"... hope u understand my problem.. even after writing if else condition also, i'm getting the same error as shown in the above post.. pls can any one tel me idea?? ??? many thnx, pavan.p Quote Link to comment https://forums.phpfreaks.com/topic/128671-solved-mysql-query-help/#findComment-667132 Share on other sites More sharing options...
fenway Posted October 16, 2008 Share Posted October 16, 2008 Please echo that query. Quote Link to comment https://forums.phpfreaks.com/topic/128671-solved-mysql-query-help/#findComment-667145 Share on other sites More sharing options...
pavanpuligandla Posted October 16, 2008 Author Share Posted October 16, 2008 helo, i printed the query, i'm getting the below warning; Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\MyDreamProject\csin1delete.php on line 38 No records to delete1 Quote Link to comment https://forums.phpfreaks.com/topic/128671-solved-mysql-query-help/#findComment-667384 Share on other sites More sharing options...
fenway Posted October 16, 2008 Share Posted October 16, 2008 No you didn't... you ran the query... please echo the query string that you're passing to mysql_query(). Quote Link to comment https://forums.phpfreaks.com/topic/128671-solved-mysql-query-help/#findComment-667453 Share on other sites More sharing options...
Barand Posted October 16, 2008 Share Posted October 16, 2008 mysql_num_rows() is for queries that return a result set. DELETE queries do not. Use if (mysql_affected_rows() > 0 ) Quote Link to comment https://forums.phpfreaks.com/topic/128671-solved-mysql-query-help/#findComment-667534 Share on other sites More sharing options...
pavanpuligandla Posted October 17, 2008 Author Share Posted October 17, 2008 @ barand,. if (mysql_affected_rows() > 0 ) yes got it. mysql_num_rows is for gettin resultset..u r rite.. many thnx boss. Quote Link to comment https://forums.phpfreaks.com/topic/128671-solved-mysql-query-help/#findComment-667807 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.