rocky_88 Posted March 30, 2011 Share Posted March 30, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/232160-problems-with-function/ Share on other sites More sharing options...
KevinM1 Posted March 30, 2011 Share Posted March 30, 2011 Try: if (!check($value)) { add($value); } Quote Link to comment https://forums.phpfreaks.com/topic/232160-problems-with-function/#findComment-1194246 Share on other sites More sharing options...
rocky_88 Posted March 30, 2011 Author Share Posted March 30, 2011 @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. Quote Link to comment https://forums.phpfreaks.com/topic/232160-problems-with-function/#findComment-1194270 Share on other sites More sharing options...
KevinM1 Posted March 30, 2011 Share Posted March 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232160-problems-with-function/#findComment-1194274 Share on other sites More sharing options...
rocky_88 Posted March 30, 2011 Author Share Posted March 30, 2011 Thanks for the reply & suggestion, tried the code (!$this->check($value)), but it did not work, it is adding duplicate values. Quote Link to comment https://forums.phpfreaks.com/topic/232160-problems-with-function/#findComment-1194294 Share on other sites More sharing options...
KevinM1 Posted March 30, 2011 Share Posted March 30, 2011 Show me your actual code. Quote Link to comment https://forums.phpfreaks.com/topic/232160-problems-with-function/#findComment-1194313 Share on other sites More sharing options...
rocky_88 Posted March 30, 2011 Author Share Posted March 30, 2011 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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/232160-problems-with-function/#findComment-1194315 Share on other sites More sharing options...
KevinM1 Posted March 30, 2011 Share Posted March 30, 2011 Is this all of it? As in, you don't have any code that defines a class? Quote Link to comment https://forums.phpfreaks.com/topic/232160-problems-with-function/#findComment-1194316 Share on other sites More sharing options...
rocky_88 Posted March 30, 2011 Author Share Posted March 30, 2011 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"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232160-problems-with-function/#findComment-1194320 Share on other sites More sharing options...
KevinM1 Posted March 30, 2011 Share Posted March 30, 2011 In check, put: if ($count > 0) { return false; } else { return true; } Quote Link to comment https://forums.phpfreaks.com/topic/232160-problems-with-function/#findComment-1194328 Share on other sites More sharing options...
rocky_88 Posted March 30, 2011 Author Share Posted March 30, 2011 @Nightslyr Now its not at all taking any values, for all the values it says Value exists. It was working fine when i put the statements of the check function within add function, but if i call it as a function, its not working at all. Quote Link to comment https://forums.phpfreaks.com/topic/232160-problems-with-function/#findComment-1194366 Share on other sites More sharing options...
KevinM1 Posted March 30, 2011 Share Posted March 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232160-problems-with-function/#findComment-1194414 Share on other sites More sharing options...
rocky_88 Posted March 31, 2011 Author Share Posted March 31, 2011 @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 } } Quote Link to comment https://forums.phpfreaks.com/topic/232160-problems-with-function/#findComment-1194775 Share on other sites More sharing options...
KevinM1 Posted March 31, 2011 Share Posted March 31, 2011 $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 Quote Link to comment https://forums.phpfreaks.com/topic/232160-problems-with-function/#findComment-1194890 Share on other sites More sharing options...
rocky_88 Posted March 31, 2011 Author Share Posted March 31, 2011 @Nightslyr Thanks for the explanation & link. Quote Link to comment https://forums.phpfreaks.com/topic/232160-problems-with-function/#findComment-1194941 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.