Web Hosting Talk







View Full Version : Weird MYSQL/PHP issue


iamdave
11-24-2004, 02:02 AM
I have been on this for a half hour, but I cannot figure out what the problem is...


<?
//DATABASE ACCESS
$db_username = "user";
$db_password = "pass";
$database = "db";
//CONNECT TO DATABASE
$link = mysql_connect("localhost",$db_username,$db_password);
if (!$link) {
die ("Could not connect to mySQL server.");
}
mysql_select_db($database);
if (!mysql_select_db ("$database", $link)) {
die ("Could not open database $database. " . mysql_error() );
}

if($_REQUEST['unsubscribe'] == "true"){

$sql = "UPDATE `newsletter` SET status = 'Inactive' WHERE id = '$_REQUEST[id]' AND email = '$_REQUEST[email]'";
$result = mysql_query($sql, $link);

if (!mysql_num_rows($result)) {
echo("<strong>There was an error deleting your email address form the database.</strong>");
}
else {
echo("<strong>Your email address has been deleted. You will no longer receive any mailings from us.</strong>");
}
}
?>


The actual query works, it does change the status to "Inactive," but then I get an error saying the following:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/httpd/vhosts/domaing.com/httpsdocs/test.php on line 21

ub3r
11-24-2004, 03:09 AM
try changing


$sql = "UPDATE `newsletter` SET status = 'Inactive' WHERE id = '$_REQUEST[id]' AND email = '$_REQUEST[email]'";

to..


$sql = "UPDATE `newsletter` SET status = 'Inactive' WHERE id = '$_REQUEST['id']' AND email = '$_REQUEST['email']'";

datutorials
11-24-2004, 03:49 AM
I might be wrong, but when you execute an UPDATE query, there is no result returned, so the $result variable is not initiated and that should cause the error IMO.

Regards,
Ya

hiryuu
11-24-2004, 04:01 AM
Correct. UPDATE (among others) does not return rows, so you actually want mysql_affected_rows($link), not num_rows.

terahost
11-24-2004, 08:26 AM
you could have an IF for if the query executes e.g.

if (!mysql_query($sql, $link)){}

i think that should work. It should output if the query doesn't execute correctly. - however, i don't think it will outpur if the query executes but there are no affected rows.

If you want to know if it actually changed anything,stick with hiryuu's code.

azizny
11-24-2004, 11:32 PM
<?
//Connect to database
define ('DB_USER', 'user');
define ('DB_PASSWORD', 'pass');
define ('DB_HOST', 'localhost');
$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db('db');

if($_REQUEST['unsubscribe'] == "true"){
$result = @mysql_query("UPDATE `newsletter` SET `status` = 'Inactive' WHERE `id` = '$_REQUEST[id]'
AND `email` = '$_REQUEST[email]'");
if (!$result) {
echo("<strong>There was an error deleting your email address form the database. Please contact support team.</strong>");
}
else {
echo("<strong>Your email address has been deleted. You will no longer receive any mailings from us.</strong>");
}
}
@mysql_close();
?>


Peace,

tabarry
11-25-2004, 05:40 PM
You can use an @ sign before mysql_query() - @mysql_query(). The @ sign is very useful sometimes and is used to suppress warnings.