Jump to content

Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link reso


BradD

Recommended Posts

Hey Guys;

 

I can't figure out this error;

 

Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in /home/database/public_html/db_insert.php on line 20

 

The Table is being Updated when the form is submitted but this error message is also...

using the mysql_affected_rows()

 

if( mysql_affected_rows($query) == 1 )  {
$id = mysql_insert_id();
$new_record = "SELECT `firstname`, `username`, `email`, `emailactivated` FROM `members` WHERE `id` = $id";
$result = mysql_query($new_record);
$array = mysql_fetch_assoc($result);

 

Here's how I see it, blurry and probably wrong but...

 

We are using the mysql_affected_rows function to discover whether data in the $query Variable has been recorded, then we are assigning a variable $new_record to the SELECT function, we then tell $new_record mysql_query that it is now $result and then we fetch this $result from the $array. That's probably the shortest possible explanation and probably wrong as well.

 

Anyway I can't figure out whats causing this error? Because I'm pretty sure  :confused: the link resource is okay.

 

<?php


require_once('connectDB.php'); 

$password = md5($_POST['password']); // hash the password before the array_map()

array_map('mysql_real_escape_string', $_POST); // Since only one element won't need to be sanitized, run the entire $_POST array through mysql_real_escape_string.

$conf_code = md5(uniqid(rand(), true)); // generates confirmation code to use wth the confirmation email that gets sent //

$query = "INSERT INTO `members` (
`firstname`, `lastname`, `username`, `password`, `accounttype`, `country`, `state`, `city`, `phone`, `mobile`, `business`, `email`, `website`, `signupdate`, `emailactivated`, `confirmed`
) VALUES (
'" . $_POST['firstname'] . "', '" . $_POST['lastname'] . "', '" . $_POST['username'] . "', '" . $password . "', '" . $_POST['accounttype'] . "', '" . $_POST['country'] . "', '" . $_POST['state'] . "', '" . $_POST['city'] . "', '" . $_POST['phone'] . "', '" . $_POST['mobile'] . "', '" . $_POST['business'] . "', '" . $_POST['email'] . "',  '" . $_POST['website'] . "', now(), '" . $conf_code . "', 0
)";
mysql_query($query) or die("There has been a system problem. Failed to add record to database.");

if( mysql_affected_rows($query) == 1 )  {
$id = mysql_insert_id();
$new_record = "SELECT `firstname`, `username`, `email`, `emailactivated` FROM `members` WHERE `id` = $id";
$result = mysql_query($new_record);
$array = mysql_fetch_assoc($result);

$email = $array['email'];
$subject = "Your confirmation code from the website";
$message = "Dear " . $array['firstname'] . ",\r\n";
$message .= "Thank you for registering at my website. To confirm your registration, please visit the link provided and enter the following confirmation code,\r\n";
$message .= $array['emailactivated'];
$from = "From: brad@mlmnetworkingsystem.com\n";

if( mail($email, $subject, $message, $from) ) {
header('Location: nuttin.php'); // redirects to a "landing page" if mail was sent successfully.
exit(); // stops further script execution after redirect.
}
}
?>

 

 

 

 

Link to comment
Share on other sites

You need to use the result of the mysql_query() function call in the mysql_affected_rows() function call, you're using the $query itself.

 

<?php
$rs = mysql_query($query) or die("There has been a system problem. Failed to add record to database.");

if( mysql_affected_rows($rs) == 1 )  {
?>

 

Ken

Link to comment
Share on other sites

The key to effectively using any function in programming is to read the documentation for that function, especially if you get an error when you use that function -

Description

int mysql_affected_rows ([ resource $link_identifier ] )

Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query associated with link_identifier .

 

Parameters

 

link_identifier

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

 

mysql_affected_rows() (optionally) accepts the connection link identifier as a parameter.

 

Link to comment
Share on other sites

Thanks man, I'll have a crack at it...

 

You need to use the result of the mysql_query() function call in the mysql_affected_rows() function call, you're using the $query itself.

 

<?php
$rs = mysql_query($query) or die("There has been a system problem. Failed to add record to database.");

if( mysql_affected_rows($rs) == 1 )  {
?>

 

Ken

Link to comment
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.