a function to check if a mysql_query has happened properly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
VisionsOfCody
Forum Commoner
Posts: 30
Joined: Sat Mar 01, 2003 1:19 pm
Location: France

a function to check if a mysql_query has happened properly

Post by VisionsOfCody »

Hi
a quickie :

If i insert some information into a database table with mysql_query(INSERT) is there a simple function that enables me to check that the entry has been made correctly without running a mysql_query(SELECT) function ?

If not, is it possible to put a mysql_query(INSERT) followed directly by a mysql_query(SELECT) within the same mysql_connect()?
I've been trying to do this but it doesn't work and i'm not sure if it is actually possible to do this.....

thanks

Cody d*l*b
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can use mysql_affected_rows()

Mac
VisionsOfCody
Forum Commoner
Posts: 30
Joined: Sat Mar 01, 2003 1:19 pm
Location: France

Post by VisionsOfCody »

thanks - i had a look at the manual entry

sorry to appear a bit dim but i need to check if i've got this - can i do this to turn the result into a variable?:

mysql_query("INSERT into password (login,password,type,name,trad,email) values ('$login','$password','$client','$name','$trad','$email')");
$rows=mysql_affected_rows();

then check if $rows is 1 or 0 to be sure that the data has been entered into the table??

thanks for your patience

Cody d*l*b
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

mysql_affected_rows() would return -1 if the prior query had failed

so

if(mysql_affected_rows() < 0) { // failed }

should be fine
VisionsOfCody
Forum Commoner
Posts: 30
Joined: Sat Mar 01, 2003 1:19 pm
Location: France

Post by VisionsOfCody »

ok i'll try that
thanks a lot
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Re: a function to check if a mysql_query has happened proper

Post by Kriek »

VisionsOfCody wrote:If i insert some information into a database table with mysql_query(INSERT) is there a simple function that enables me to check that the entry has been made correctly without running a mysql_query(SELECT) function
Unless I'm mistaken MySQL should throw an error if the information is not entered correctly ..

Code: Select all

$query = "Place your query here";
$result = mysql_query($query) or die("Error executing $query <br /> MySQL reported: " . mysql_error());
while ($row = mysql_fetch_array($query)) {
VisionsOfCody
Forum Commoner
Posts: 30
Joined: Sat Mar 01, 2003 1:19 pm
Location: France

oooh - i like that!

Post by VisionsOfCody »

that way i can put up a message to contact the administrator if the efforts to upload a file fail.
Thanks :D
Post Reply