Solved

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

Posted on 2007-11-21
15
774 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
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 125 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

729 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