uchacker11 Posted October 26, 2009 Share Posted October 26, 2009 I am trying to run a Select statement that will return an int to then be fed into an insert statement but I keep getting the following error: Warning: mssql_query() [function.mssql-query]: message: Conversion failed when converting the varchar value 'Resource id #3' to data type int. (severity 16) in C:\www\Rentals\Add\InsertRenter.php on line 33 $server = 'HomeServer'; $link = mssql_connect($server, 'username', 'password'); if(!$link) { die('Failure to connect to Server'); } $db = mssql_select_db('Rentals', $link) or die('Database Connection Failed'); $Last=$_POST['RenterLast']; $First=$_POST['RenterFirst']; $Temp=$_POST['RenterHouse']; $Sec8ID=$_POST['RenterSec8ID']; $MoveInDate=$_POST['RenterMoveInDate']; $MoveOutDate=$_POST['RenterMoveOutDate']; $Phone=$_POST['RenterPhone']; $Deposit=$_POST['RenterDeposit']; $Sec8Payment=$_POST['RenterSec8Payment']; $RenterPayment=$_POST['RenterRenterPayment']; $Comments=$_POST['RenterComments']; $HouseIDLookup = "SELECT HouseID from Houses WHERE Alias LIKE '$Temp'"; $HouseID = mssql_query($HouseIDLookup, $link, 0); $query= "INSERT INTO Renters ([LastName], [FirstName], [HouseID], [section8ID], [MoveInDate], [MoveOutDate], [Phone], [Deposit], [section8PaymentAmt], [RenterPaymentAmt], [Comments]) VALUES ('$Last', '$First', '$HouseID', '$Sec8ID', '$MoveInDate', '$MoveOutDate', '$Phone', '$Deposit', '$Sec8Payment', '$RenterPayment', '$Comments')"; $insert = mssql_query($query, $link, 0); ?> Any help would be greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/179012-ms-sql-query-failure/ Share on other sites More sharing options...
trq Posted October 26, 2009 Share Posted October 26, 2009 $HouseID is a result resource, you seem to be trying to insert it back into your database. Link to comment https://forums.phpfreaks.com/topic/179012-ms-sql-query-failure/#findComment-944461 Share on other sites More sharing options...
uchacker11 Posted October 26, 2009 Author Share Posted October 26, 2009 Yes i am trying to insert it into a different table within my database as a foreign key constraint. I need to select the HouseID from Houses table using the Alias field in the Houses table, then insert the HouseID field into the Renters table. Link to comment https://forums.phpfreaks.com/topic/179012-ms-sql-query-failure/#findComment-944462 Share on other sites More sharing options...
trq Posted October 26, 2009 Share Posted October 26, 2009 You need to pass your result resource ($HouseID) to mssql_fetch_assoc to actually get the data you want. Link to comment https://forums.phpfreaks.com/topic/179012-ms-sql-query-failure/#findComment-944464 Share on other sites More sharing options...
uchacker11 Posted October 26, 2009 Author Share Posted October 26, 2009 Thanks for the help but now i get a similar error: Warning: mssql_query() [function.mssql-query]: message: Conversion failed when converting the varchar value 'Array' to data type int. (severity 16) in C:\www\Rentals\Add\InsertRenter.php on line 34 I added the line: $House = mssql_fetch_assoc($HouseID); Did i mess up syntax or something Link to comment https://forums.phpfreaks.com/topic/179012-ms-sql-query-failure/#findComment-944468 Share on other sites More sharing options...
trq Posted October 26, 2009 Share Posted October 26, 2009 So now your trying to insert $House? $House is an array. Did you actually look at the examples in the manual page I pointed you too? Do so, your missing the absolute basics here. Link to comment https://forums.phpfreaks.com/topic/179012-ms-sql-query-failure/#findComment-944485 Share on other sites More sharing options...
uchacker11 Posted October 26, 2009 Author Share Posted October 26, 2009 Sorry I am very new to PHP but i figured it out using mssql_result(). Link to comment https://forums.phpfreaks.com/topic/179012-ms-sql-query-failure/#findComment-944488 Share on other sites More sharing options...
trq Posted October 26, 2009 Share Posted October 26, 2009 You could have used... $House = mssql_fetch_assoc($HouseID); then insert.... $House['HouseID'] into your query. Link to comment https://forums.phpfreaks.com/topic/179012-ms-sql-query-failure/#findComment-944501 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.