dmcglone Posted February 4, 2013 Share Posted February 4, 2013 Hi everyone, I'm new to the forum, but not to php. I consider myslelf mediocre at PHP, but I'm getting better. :-) I also have a quick simple question that I'm sure plenty of people here will know the answer to. Anyway, all I'm trying to do is check to see if a certain value in the db exsist and if not, stop execution or if it doesn't then insert it into the db. You can see where I attempted to use mysql_num_rows(), but I'm not very good with mysql. here's my code: class insertRecord extends createConnection { function insertRecord(){ $this->query = "SELECT hexnum FROM $this->tablename"; $this->result = mysql_query($this->query); if (mysql_affected_rows() > 0 ) { echo "<p>Hex Number Number is already in the listings</p> <a href='admin.php'>Please check your MLS Number</a>"; }else { $sql="INSERT INTO properties (hexnum) VALUES ('$_POST[hexnum]')"; if (!mysql_query($sql)){ die('Error: ' . mysql_error()); } echo "<p>1 record added</p>"; echo "<a href='admin.php'>Add another record</a>"; }//endif }//end else }//end class Blessings, David M. Quote Link to comment https://forums.phpfreaks.com/topic/274023-hello/ Share on other sites More sharing options...
Nodral Posted February 5, 2013 Share Posted February 5, 2013 (edited) mysql_affected_rows returns nothing on a select as nothing is 'affected' / changed. You want mysql_num_rows as this is the amount of rows returned by the previous sql statement. Try this $this->query = "SELECT hexnum FROM $this->tablename"; $this->result = mysql_query($this->query); if (mysql_num_rows]() > 0 ) Edited February 5, 2013 by Nodral Quote Link to comment https://forums.phpfreaks.com/topic/274023-hello/#findComment-1410252 Share on other sites More sharing options...
soycharliente Posted February 6, 2013 Share Posted February 6, 2013 Obligatory: Users are advised to use MySQL Improved mysqli_ functions rather than the older [replaced] mysql_ functions where applicable and subject the appropriate latest stable versions of Apache, php and MySQL, etc. Quote Link to comment https://forums.phpfreaks.com/topic/274023-hello/#findComment-1410509 Share on other sites More sharing options...
dmcglone Posted February 7, 2013 Author Share Posted February 7, 2013 mysql_affected_rows returns nothing on a select as nothing is 'affected' / changed. You want mysql_num_rows as this is the amount of rows returned by the previous sql statement. Try this $this->query = "SELECT hexnum FROM $this->tablename"; $this->result = mysql_query($this->query); if (mysql_num_rows]() > 0 ) Thanks. I'll try this. :-) Quote Link to comment https://forums.phpfreaks.com/topic/274023-hello/#findComment-1410723 Share on other sites More sharing options...
dmcglone Posted February 7, 2013 Author Share Posted February 7, 2013 Obligatory: Users are advised to use MySQL Improved mysqli_ functions rather than the older [replaced] mysql_ functions where applicable and subject the appropriate latest stable versions of Apache, php and MySQL, etc. Thanks Charlie. I'm glad you mentioned this. I've seen mysqli on occasion, but never had a need to fully looked into it. I think now is the best time to do so. Quote Link to comment https://forums.phpfreaks.com/topic/274023-hello/#findComment-1410724 Share on other sites More sharing options...
soycharliente Posted February 7, 2013 Share Posted February 7, 2013 No worries. PDO is also an option as well. You could probably mark this solved if you plan on trying to rewrite the code and post issues (if any) on a new thread. Quote Link to comment https://forums.phpfreaks.com/topic/274023-hello/#findComment-1410729 Share on other sites More sharing options...
dmcglone Posted February 7, 2013 Author Share Posted February 7, 2013 Thanks, I'll probably stick with mysqli because it's looks a lot closer to plain MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/274023-hello/#findComment-1410852 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.