?
Solved

the mssql_affected_rows($qry) doesn't seem to work in php to sql server

Posted on 2007-11-21
15
Medium Priority
?
788 Views
Last Modified: 2013-12-12
could someone look at this code and tell me what is wrong: The data is submitted into the db successfully, but I have an if($qry) mssql_affected_rows($qry) check in my code, that will not run.

go down the section that starts:
if ($_REQUEST["txt_submitValue"]  == 'addChar')
<?php
 
echo "hello addchar world";
trim($txt_AddChar);
 
$userID = $txt_AddChar; //this is the userID
echo "<br>userid is:".$userID;
 
//$txt_submitValue ='search';
if (!isset($_REQUEST["txt_submitValue"]))
	{
				if (!$txt_AddChar)
		{
			echo "nothing entered";
			exit;
		}
	echo "<FORM method = 'post' action=".basename($_SERVER['PHP_SELF'])." NAME=frmAddChar>";
	//echo "<input type=text name=txt_submitValue size=20>";       //hidden submit value
	 
	echo "<p><input type=\"text\" name=\"txt_submitValue\" size=\"20\" value=\"addChar\"></p>"; //hidden submit value
	echo "<p><input type=text name=txt_search size=20 value=$userID></p>";                       //hold userID
	echo "<p>Character First Name:  <input type=text name=txt_charFName size=20 value=></p>";    //hold char fname
	echo "<p>Character Last Name:  <input type=text name=txt_charLName size=20 value=></p>";    //hold char lname
	 
	echo "<input type=image src=modifyUser.jpg style=color: #0000FF; font-weight: bold width=153 height=24 onclick=document.frmAddChar.txt_submitValue.value = 'addChar';document.formname.submit();>";
	                
	echo "</form>";
	} 
	else
	{
		// Debug
echo '<pre>';
var_dump($_POST);
echo '</pre>';
 
if ($_REQUEST["txt_submitValue"]  == 'addChar')
	{
                    
		$userID = trim($txt_search);
		echo "<br><br>userID 2 =:".$userID;
		$charFName = trim($txt_charFName);
		$charLName = trim($txt_charLName);
	                    
		$dbname="northwinds";
		$host="localhost";
		$port="1433";
		$user="admin";
		$password="testing";
	            
	    $connection=mssql_connect($host,$user,$password) or Die("Unable to connect to " . $host);
	    $qry = mssql_query("INSERT INTO TBL_PC (UserUID, NameIndex, NameFlag, FirstName, LastName, Gender, FaceStyle, HairStyle, HairColor, SkinColor, EyeColor, Height, FNCode, Body, MNCode, LNCode, RegDate) VALUES ($userID, 0, 0, '$charFName', '$charLName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, getDate())",$connection);
	    mssql_select_db($dbname, $connection);
	    $num_results = mssql_affected_rows($qry);
	    
	    if ($qry)
		    {
		    	echo $num_results."new character inserted in to database";
		    }
		    else 
		    {
		    	echo "<br><br>userID 2 =:".$userID;
		    	echo "Nothing entered into db";
		    	exit;
		    }	                        
	}       
}              
?>

Open in new window

0
Comment
Question by:justmelat
  • 8
  • 3
  • 2
  • +2
15 Comments
 
LVL 17

Expert Comment

by:nplib
ID: 20329080
mssql_affected_rows()
returns the number of affected rows from the last query,

you don't put anything inside it, just
mssql_affected_rows()
0
 
LVL 17

Expert Comment

by:nplib
ID: 20329090
change all your mssql_affected_rows($qry) to mssql_affected_rows()
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20329103
The correct function is  mssql_rows_affected NOT mssql_affected_rows
http://www.php.net/manual/en/function.mssql-rows-affected.php
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:nplib
ID: 20329109
oh and your mssql_select_db should be above your query
$qry = mssql_query("INSERT INTO TBL_PC (UserUID, NameIndex, NameFlag, FirstName, LastName, Gender, FaceStyle, HairStyle, HairColor, SkinColor, EyeColor, Height, FNCode, Body, MNCode, LNCode, RegDate) VALUES ($userID, 0, 0, '$charFName', '$charLName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, getDate())",$connection);
            mssql_select_db($dbname, $connection);


should be
mssql_select_db($dbname, $connection);
mssql_query("INSERT INTO TBL_PC (UserUID, NameIndex, NameFlag, FirstName, LastName, Gender, FaceStyle, HairStyle, HairColor, SkinColor, EyeColor, Height, FNCode, Body, MNCode, LNCode, RegDate) VALUES ($userID, 0, 0, '$charFName', '$charLName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, getDate())",$connection);
           
and you can't put an insert statement into a variable, placing queries in a variable is for returning a result, when performing an update or insert you just run the query.
0
 
LVL 17

Expert Comment

by:nplib
ID: 20329127
ah yes, the function is mssql_rows_affected, my bad.

how ever, to accomplish what you want change your code to this.
            $num_results = mssql_affected_rows();
           
            if ($num_results >= 1)
                    {
                        echo $num_results."new character inserted in to database";
                    }
                    else
                    {
                        echo "<br><br>userID 2 =:".$userID;
                        echo "Nothing entered into db";
                        exit;
                    }
0
 
LVL 1

Author Comment

by:justmelat
ID: 20329277
ok, here is what i have now:  see attached.

it still dies here:$num_results = mssql_affected_rows();
data is intered into db though.
$connection=mssql_connect($host,$user,$password) or Die("Unable to connect to " . $host);
	    mssql_select_db($dbname, $connection);
	    $qry = mssql_query("INSERT INTO TBL_PC (UserUID, NameIndex, NameFlag, FirstName, LastName, Gender, FaceStyle, HairStyle, HairColor, SkinColor, EyeColor, Height, FNCode, Body, MNCode, LNCode, RegDate) VALUES ($userID, 0, 0, '$charFName', '$charLName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, getDate())",$connection);
	    $num_results = mssql_affected_rows();
 
	    
	    if ($num_results >=1)
		    {
		    	echo $num_results."new character inserted in to database";
		    }
		    else 
		    {
		    	echo "<br><br>userID 2 =:".$userID;
		    	echo "Nothing entered into db";
		    	exit;
		    }

Open in new window

0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20329300
$num_results = mssql_rows_affected();

NOT

$num_results = mssql_affected_rows();

http://www.php.net/manual/en/function.mssql-rows-affected.php
0
 
LVL 17

Expert Comment

by:nplib
ID: 20329321
mssql_query("INSERT INTO TBL_PC (UserUID, NameIndex, NameFlag, FirstName, LastName, Gender, FaceStyle, HairStyle, HairColor, SkinColor, EyeColor, Height, FNCode, Body, MNCode, LNCode, RegDate) VALUES ($userID, 0, 0, '$charFName', '$charLName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, getDate())",$connection);

you can't place an INSERT or UPDATE statement in a variable

do it like above.
0
 
LVL 17

Expert Comment

by:nplib
ID: 20329346
yes, and change that too.
0
 
LVL 1

Author Comment

by:justmelat
ID: 20329373
ok, now here is hwat i have:

$num_results returns nothing.  I placed an echo statement after it.  Data still goes to db though.
  $connection=mssql_connect($host,$user,$password) or Die("Unable to connect to " . $host);
	    mssql_select_db($dbname, $connection);
	    mssql_query("INSERT INTO TBL_PC (UserUID, NameIndex, NameFlag, FirstName, LastName, Gender, FaceStyle, HairStyle, HairColor, SkinColor, EyeColor, Height, FNCode, Body, MNCode, LNCode, RegDate) VALUES ($userID, 0, 0, '$charFName', '$charLName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, getDate())",$connection);
	    //$num_results = mssql_affected_rows();
	    $num_results = mssql_rows_affected();
	echo "this is".$num_results;
	    
	    if ($num_results >=1)
		    {
		    	echo $num_results."new character inserted in to database";
		    }
		    else 
		    {
		    	echo "<br><br>userID 2 =:".$userID;
		    	echo "Nothing entered into db";
		    	exit;
		    }
	                        
	}       

Open in new window

0
 
LVL 17

Accepted Solution

by:
nplib earned 375 total points
ID: 20329388
mssql_query("INSERT INTO TBL_PC (UserUID, NameIndex, NameFlag, FirstName, LastName, Gender, FaceStyle, HairStyle, HairColor, SkinColor, EyeColor, Height, FNCode, Body, MNCode, LNCode, RegDate) VALUES ($userID, 0, 0, '$charFName', '$charLName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, getDate())",$connection) or die(mssql_error());

try that and see if it returns an error.
0
 
LVL 1

Author Comment

by:justmelat
ID: 20329432
nope, no error.  this is so weird.  It's not a big deal, except i really need to catch an error should it occur
0
 
LVL 17

Expert Comment

by:nplib
ID: 20329460
try this

$num_results = mssql_rows_affected($connection);
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20331089
justmelat:

if this still doesn't work for you, there's a simple alternative which will do it in the actual database, a neat function here.

<?php
function update($query){
    mssql_query($query,$connection);
 
    $rsRows = mssql_query("select @@rowcount as rows", $connection);
    $rows = mssql_fetch_assoc($rsRows);
    return $rows['rows'];
}
 
$connection=mssql_connect($host,$user,$password) or Die("Unable to connect to " . $host);
mssql_select_db($dbname, $connection);
$query = "INSERT INTO TBL_PC (UserUID, NameIndex, NameFlag, FirstName, LastName, Gender, FaceStyle, HairStyle, HairColor, SkinColor, EyeColor, Height, FNCode, Body, MNCode, LNCode, RegDate) VALUES ($userID, 0, 0, '$charFName', '$charLName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, getDate())";
 
$num_results = update($query);
 
if ($num_results >=1)
{
    echo $num_results."new character inserted in to database";
}
else 
{
    echo "<br><br>userID 2 =:".$userID;
    echo "Nothing entered into db";
    exit;
}
?>

Open in new window

0
 
LVL 20

Expert Comment

by:Muhammad Wasif
ID: 20333837
You must have to pass connection link to the function as nplib suggested.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question