Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/274023-hello/
Share on other sites

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 by Nodral
Link to comment
https://forums.phpfreaks.com/topic/274023-hello/#findComment-1410252
Share on other sites

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. :-)

Link to comment
https://forums.phpfreaks.com/topic/274023-hello/#findComment-1410723
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/274023-hello/#findComment-1410724
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.