Jump to content

[SOLVED] MYSQL Query Help


pavanpuligandla

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.