Page 1 of 1

a function to check if a mysql_query has happened properly

Posted: Fri Apr 04, 2003 5:32 am
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

Posted: Fri Apr 04, 2003 5:33 am
by twigletmac
You can use mysql_affected_rows()

Mac

Posted: Fri Apr 04, 2003 6:11 am
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

Posted: Fri Apr 04, 2003 6:24 am
by pootergeist
mysql_affected_rows() would return -1 if the prior query had failed

so

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

should be fine

Posted: Fri Apr 04, 2003 6:44 am
by VisionsOfCody
ok i'll try that
thanks a lot

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

Posted: Fri Apr 04, 2003 9:57 am
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)) {

oooh - i like that!

Posted: Mon Apr 07, 2003 8:41 am
by VisionsOfCody
that way i can put up a message to contact the administrator if the efforts to upload a file fail.
Thanks :D