Solved

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

Posted on 2007-11-21
15
762 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now