Jump to content

Recommended Posts

I am trying to check whether a given value exists in the database or not before inserting/deleting/updating it.

So i will be doing it using 4 functions, one is to check if the record exists and other 3 to add, update & delete.

 

Here is a sample code

 


function check($k)
{
$chk="select * from tablename where columnname = '$k'";
$res=mysql_query($chk);
$count = mysql_num_rows($res);
if ($count>0)
{
return false;
}
}

function add($a)
{
$sql="insert into tablename values('$a')";
$exec = mysql_query($sql);
if($exec)
{
echo "row added successfully";
}
else
{
echo "could not update table";
}
}

 

How do i control the add function on the basis of the output of the check function?

Link to comment
https://forums.phpfreaks.com/topic/232160-problems-with-function/
Share on other sites

@Nightslyr

 

Thanks for replying,

I should had mentioned this earlier, sorry about that.

I want to call the check function within the add function.

I'm using classes and hence all these functions will be on a different page and i'll be calling only add/delete/update function and hence want to include the check function within these three functions.

function add($value)
{
   if (!check($value)) // when you translate this to a class, you'll have to use !$this->check($value)
   {
      // add the value
   }
   else
   {
      // abort, because the value already exists
   }
}

 

You may want to give check() a better name, like valueExists(), just so your code is more readable.

Its almost the same as above,

here

function check($k)
{
$chk="select * from testing where username = '$k'";
$res=mysql_query($chk);
$count = mysql_num_rows($res);
if ($count>0)
{
return false;
}
}

function add($a)
{
if (!$this->check($a))
{
$sql="insert into testing values('$a')";
$exec = mysql_query($sql);
if($exec)
{
echo "row added successfully";
}
else
{
echo "could not update table";
}
}
else
{
echo "Value exists, enter a different string";
}
}

Nope, that is just the function part.

This is the whole class page,

lemme know if u want the other page too.

<?php
class db_handling
{
function connection()
{
$con = mysql_connect("localhost","root","");
if(!$con)
{
die ("Connection Failed");
}
echo "Connected <br/>";
}

function selection()
{
$recordset = mysql_select_db ("sample");
if(!$recordset)
{
die ("Database not found");
}
echo "Database selected <br/>";
}

function check($k)
{
$chk="select * from testing where username = '$k'";
$res=mysql_query($chk);
$count = mysql_num_rows($res);
if ($count>0)
{
return false;
}
}

function add($a)
{
if (!$this->check($a))
{
$sql="insert into testing values('$a')";
$exec = mysql_query($sql);
if($exec)
{
echo "row added successfully";
}
else
{
echo "could not update table";
}
}
else
{
echo "Value exists, enter a different string";
}
}
?>

Try:

 

class DB_Handling
{
   private $dbconn;

   public function __construct($dbname)
   {
      $conn = mysql_connect("localhost", "root", "");

      if ($conn)
      {
         $this->dbconn = $conn;

         $recordset = mysql_select_db($dbname);

         if (!$recordset) { /* abort, error */ }
      }
      else { /* abort, error */ }
   }

   public function valueExists($value, $tablename)
   {
      $query = "SELECT * FROM $tablename WHERE username = $value";
      $result = mysql_query($query, $this->dbconn);
      $count = mysql_num_rows($result);

      if ($count > 0) { return false; }
      else { return true; }
   }

   public function add($value, $tablename)
   {
      if (!$this->valueExists($value, $tablename))
      {
         $query = "INSERT INTO $tablename VALUES ($value)";
         $result = mysql_query($query, $this->dbconn);

         if ($result) { echo "Success!"; }
         else { /* abort, query error */ }
      }
      else { /* abort, value exists */ }
   }
}

$myDB = new DB_Handling("sample");
$myDB->add("Bubba", "testing");

 

NOT tested, so there may be some syntax errors, and you'll need to fill in the error handling.

@Nightslyr

 

Thanks for the code,

But i still cant get it to work right,

I modified my own version of coding and there seem to be a problem while calling the function.

 

function check($k)
{
$chk="select * from testing where username = '$k'";
$res=mysql_query($chk);
$count = mysql_num_rows($res);
}

function add($a)
{
$this ->check($a);
echo $this ->count; // It doesnt not echo this, my guess is there is some problem at function call.
if ($this ->count<=0)
{
// Insert in to table
}
else
{
// Show error
}
}

$this->count doesn't exist.  $count is local to the check() method.  The 'this' keyword denotes a class member, which is either a pre-declared class field (like $dbconn was in my example), or a class method.  Local variables (variables created within a method) don't count, and unless they're returned and stored in a variable outside of that method's scope, essentially disappear, which is what $count does.

 

Before you go much further, you should brush up on the basics: http://php.net/manual/en/language.oop5.php

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.