jay7981 Posted February 19, 2010 Share Posted February 19, 2010 i have a MySQL query and i want to display 1 thing only if the number of affected rows is >=1 and if not then display the error, here is what i have so far and nothing is being displayed ... if (mysql_num_rows() >= "1") { echo "Update OK!<br />"; } else { die("Update failed: " . mysql_error()); } Link to comment https://forums.phpfreaks.com/topic/192590-php-using-if-to-display-error/ Share on other sites More sharing options...
tecmeister Posted February 19, 2010 Share Posted February 19, 2010 You need to put the database info inside the mysql_num_row($query) example: $query = SELECT * FROM users; if(mysql_num_rows($query) > 1) echo "alert"; else quit(); Link to comment https://forums.phpfreaks.com/topic/192590-php-using-if-to-display-error/#findComment-1014648 Share on other sites More sharing options...
jay7981 Posted February 19, 2010 Author Share Posted February 19, 2010 Please forgive me as i am still learning, this is my query, what i am trying to accomplish is if the number of rows affected is >1 then echo Update OK! and include ('ths_form.php') if not then echo Update failed: mysql_error() and include ('that_form.php'); <?php $authid = "'" . mysql_real_escape_string($_POST['authid]') . "'"; $name = "'" . mysql_real_escape_string($_POST['name']) . "'"; $email = "'" . mysql_real_escape_string($_POST['email']) . "'"; $fid = "'" . mysql_real_escape_string($_POST['fid']) . "'"; $rank = "'" . mysql_real_escape_string($_POST['rank']) . "'"; $access = "'" . mysql_real_escape_string($_POST['access']) . "'"; $admin_id = "'" . mysql_real_escape_string($_POST['admin_id']) . "'"; $group_id = "'" . mysql_real_escape_string($_POST['group_id']) . "'"; $query = "UPDATE bioclan.clan_members a INNER JOIN bioclan.admins b ON a.authid = b.auth INNER JOIN bioclan.sm_admins c ON a.authid = c.identity INNER JOIN bioclan.sm_groups d ON d.id = e.group_id INNER JOIN bioclan.sm_admins_groups e ON c.id = e.admin_id SET a.authid = $authid, a.name = $name, a.email = $email, a.fid = $fid, a.rank = $rank, b.auth = $authid, b.name = $name, b.access = $access, c.name = $name, c.identity = $authid, e.admin_id = $admin_id, e.group_id = $group_id WHERE a.authid = '" . mysql_real_escape_string($_POST['authid']) . "'"; mysql_query($query) or die("Query failed: " . mysql_error()); mysql_close($db); ?> Link to comment https://forums.phpfreaks.com/topic/192590-php-using-if-to-display-error/#findComment-1014654 Share on other sites More sharing options...
trq Posted February 19, 2010 Share Posted February 19, 2010 You sure your query should affect more than one row? if ($result = mysql_query($query)) { if (mysql_affected_rows()) { echo "Update OK!"; } else { echo "No rows updated"; } } else { echo "Query failed " . mysql_error(); } Link to comment https://forums.phpfreaks.com/topic/192590-php-using-if-to-display-error/#findComment-1014656 Share on other sites More sharing options...
jay7981 Posted February 19, 2010 Author Share Posted February 19, 2010 no it should only affect 1 row, does that make a difference? if it affects more than 1 row i would like it to stop and not do anything except echo more than 1 row match your query also would i replace this line with your code? mysql_query($query) or die("Query failed: " . mysql_error()); Link to comment https://forums.phpfreaks.com/topic/192590-php-using-if-to-display-error/#findComment-1014658 Share on other sites More sharing options...
trq Posted February 19, 2010 Share Posted February 19, 2010 It will have already executed the query though. if ($result = mysql_query($query)) { if (mysql_affected_rows() > 1) { echo "More than one row updated"; } else if (mysql_affected_rows() == 1) { echo "Update OK!"; } else { echo "No rows updated"; } } else { echo "Query failed " . mysql_error(); } Link to comment https://forums.phpfreaks.com/topic/192590-php-using-if-to-display-error/#findComment-1014660 Share on other sites More sharing options...
jay7981 Posted February 19, 2010 Author Share Posted February 19, 2010 thank you that worked beautifully Link to comment https://forums.phpfreaks.com/topic/192590-php-using-if-to-display-error/#findComment-1014665 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.