Solved

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

Posted on 2007-11-21
15
776 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
 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

624 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