Use PHP to set column description in MSSQL - php

I'm working on a web app that is using PHP and MSSQL. One of my requirements is to use the field descriptions in MSSQL in part of the web app.
I know the Stored Proc. for adding descriptions in MSSQL is:
EXEC sp_addextendedproperty
#name = N'Description', #value = 'Description goes here',
#level0type = N'Schema', #level0name = 'schemaname',
#level1type = N'Table', #level1name = 'tablename',
#level2type = N'Column', #level2name = 'columname'
GO
I am having problems converting this into php, however.
I'm trying to use the mssql_init/bind/execute commands, but I keep getting an error that I am not sure how to troubleshoot.
PHP Code:
$query = mssql_init("sp_addextendedproperty",$dblink);
mssql_bind($query,"#name","N'Description'",SQLVARCHAR);
mssql_bind($query,"#value",$_POST['description'],SQLVARCHAR);
mssql_bind($query,"#level0type","N'Schema'",SQLVARCHAR);
mssql_bind($query,"#level0name","dbo",SQLVARCHAR);
mssql_bind($query,"#level1type","N'Table'",SQLVARCHAR);
mssql_bind($query,"#level1name",$_POST['tableselect'],SQLVARCHAR);
mssql_bind($query,"#level2type","N'Column'",SQLVARCHAR);
mssql_bind($query,"#level1name",$_POST['column_name'],SQLVARCHAR);
mssql_execute($query)
My error is:
An invalid parameter or option was specified for procedure 'sp_addextendedproperty'. (severity 16)
I don't know enough about stored procedures to fully troubleshoot this. Can somebody help me out?

Unless you've simply made a typo posting your sample code, you're using #level1name twice. The second occurrence should be #level2name instead.

Related

How to chain RethinkDB filters in PHP

I'm needing to create more than 1 filter to run a query to cut down the number of results found.
I am trying to do these 3 queries at once using RethinkDB for php found here
The code I am running is:
$query = \r\table('payments')->filter(
\r\row('forwarded')->eq('1'),
\r\row('bad_callbacks_sent')->lt("1"),
\r\row('confirmations')->le('7')
)->run($this->conn);
I've just also tried doing the following and it doesn't work (it's just showing all where the first argument - where forwarded = 1. It's only doing the first filter:
$query = \r\table('payments')
->filter(\r\row('forwarded')->eq('1'))
->filter(\r\row('bad_callbacks_sent')->lt("6"))
->filter(\r\row('confirmations')->le("7"))
->run($this->conn);
But it doesn't seem to be doing what I ask.
I need to make it get:
Where forwarded == 1
Where bad_callbacks_sent < 1
Where confirmations <= 7
I found this the following code from here, which shows that you can chain them in js but I'm wondering about PHP:
r.db('items').table('tokens')
.filter(r.row('valid_to').gt(r.now()))
.filter(r.row["processed"] == False)
Any ideas?
Okay so it turns out, the issue was not with my new code:
$query = \r\table('payments')
->filter(\r\row('forwarded')->eq('1'))
->filter(\r\row('bad_callbacks_sent')->lt(6))
->filter(\r\row('confirmations')->le(7))
->run($this->conn);
But with the variable type.
In my database, it was stored as an int but I was trying to read it via (le("7")) a string.
The above code works fine :)
It allows you to filter more than 1 query in retihnkdb for php.
Make sure you check your variable types in your database!
RethinkDB is very sensitive to what types they're.
Hope I save you some time.

Using PHP & ODBC_RESULT, Why am I losing the 4th decimal place

I'm hoping this is an easy question.
If I run an ODBC connection via Excel I get exactly what I would expect to see from the database. When I port the query over to Xampp for testing I can not get the query to display the 4th decimal place in the results.
Here is my SQL query defined in Excel:
SELECT MC_BOM_DETAIL.finished_item, MC_BOM_DETAIL.item_num, MC_BOM_DETAIL.quantity, MC_BOM_DETAIL.line_num, IC_INVENTRY_MAST.um_stocking
FROM IC_INVENTRY_MAST IC_INVENTRY_MAST, MC_BOM_DETAIL MC_BOM_DETAIL
WHERE IC_INVENTRY_MAST.company = MC_BOM_DETAIL.company AND IC_INVENTRY_MAST.item_num = MC_BOM_DETAIL.item_num
ORDER BY MC_BOM_DETAIL.finished_item, MC_BOM_DETAIL.line_num
The php page executing the query is as follows
//Define ODBC Connection
$mas_conn = odbc_connect("odbc_connection", "user_name", "password");
//Define Query
$query = "SELECT mbd.finished_item, mbd.item_num, mbd.line_num, mbd.quantity, icm.um_stocking
FROM IC_INVENTRY_MAST icm, MC_BOM_DETAIL mbd
WHERE icm.company = mbd.company AND icm.item_num = mbd.item_num
ORDER BY mbd.finished_item, mbd.line_num";
if($result=odbc_exec($mas_conn, $query)) {
while(odbc_fetch_row($result)){
echo odbc_result($result,'quantity') .'<br>';
}
}
odbc_free_result($result);
odbc_close($mas_conn);
The results are always truncated (not rounded) to the 3rd decimal place, however I need precision to the 4th.
I looked through the php.ini file to see if that maybe the cause but came up short.
Does anybody have an suggestions?
Thank you for your time.
Solution:
The issue in my case was with the Data Definitions inside the source data. This is a bit tricky as the database is read only, and the ODBC driver is provided by 3rd party (ProvideX). After speaking with a tech at the software manufacture, I was able to change the data definitions which was set to decimal(8,3). Why Excel could read the 4th decimal and the php odbc connection could not is still unanswered. My only thought would be since Excel is running locally.
Check You Windows Localization Settings:
Panel Control->Region and Language -> Formats -> Additional Settings -> No of digits after decimal...

How to display 'text' data correctly with Microsoft ODBC driver for Linux? Currently displaying as random characters

I am trying to display the contents of a field in a MS SQL database, the data type of which is 'text'. When I connect to the database with MS Excel on a PC the value looks something like:
Shipped on the: 18/10/12 Toll IPEC Connote Number: XXX XXX XXXX
When I connect to the database with PHP using the Microsoft ODBC driver for Linux, the output of the text field will display random characters, which are slightly different each time I run the exact same script. Here is what it output the last four times:
Xisep)!ØwXment.class.php))
5isep)!ment.class.php))
µ}isep)!Ø}ment.class.php))
t)!!Owner_IDt)Ø©Ø8
Not sure where it's getting the 'ment.class.php' bit from. That looks like the end of the name of one of my classes, but not one that is included in this particular script. Is the driver broken or what? All other data types (int, varchar, datetime etc) seem to display correctly, the problem only seems to happen with this one text field.
Here is the code:
<?php
require('ConnectWise.inc.php');
$config = new CW_Config;
// Connect to database
$dbcnx = odbc_connect("ConnectWise", $config->db_username, $config->db_password);
// Query database
$query = "select * from Billing_Log where Invoice_Number = '24011'";
$result = odbc_exec($dbcnx, $query);
while ($row = odbc_fetch_array($result)) {
echo $row['Top_Comment'] . "\n";
}
odbc_close($dbcnx);
Here is the output of my last few attempts:
\3ȶä!!¶äY!
öÈö§!!ö§Y!
&Èö×!!ö×Y!
Looks like you're getting an overflow. For some reason SQL is passing the length of your text field as 0, which to SQL means "long" but to PHP means "I dont know" and so PHP is showing whatever is in its memory at the location its expecting data (or something like that - any PHP experts care to explain?).
I've seen this with varchar(max) before but not text. (Converting to) Text is normally the way to fix it. So maybe I'm wrong.
Hope this is of some help. Im not a PHP developer, I just have to use it occasionally, and this sounds much like a painful experience I've gone through before :)

Could you please assist me with PHP 5.3 and MySQL 5.5 stored procedures and mysqli library + persistent connection

Helo,
I have a stored procedure that has 7 IN parameters and 3 OUT parameters.
I need to pass 7 parameters IN from PHP, execute the query with procedure, and retrieve the 3 OUT parameters.
I am using mysqli with persistent connection mode enabled. (For load considerations)
What is the most efficient way to execute the query and get results?
I need something that doesn't affect other concurrently running PHP scripts, and that cleans the result itself, and that is straightforward.
This is what my application is (simplified) (not a working example, just how i wish it was)
$inParam1 = 'wer';
$inParam2 = 'fewf';
$inParam3 = 'dsf';
$inParam4 = 'vccv';
$inParam5 = '34t3';
$inParam6 = 'ter';
$inParam7 = 'ert';
$query = "CALL my_procedure('$inParam1', '$inParam2', '$inParam3', '$inParam4', '$inParam5', '$inParam6', '$inParam7', #outParam8, #outParam9, #outParam10); SELECT #outParam8, #outParam9, #outParam10;";
$result = $mysql_query($query);
list($outParam1, $outParam2, $outParam3) = mysql_fetch_array($result);
echo $outParam1; // String param #1 that my procedure returned as an OUT variable
echo $outParam2; // String param #2 that my procedure returned as an OUT variable
echo $outParam3; // String param #3 that my procedure returned as an OUT variable
If somebody could show how this code could look in reality, please please would be great!
I am obviously using mysqli with proper connection, and such, but the examples I have found in internet are really confusing and seem to be inefficient, I am also worried if it will conflict with other clients, because it works like "nextResult" and other some strange functions.
Many thanks!
PHP 5.3, MySQL 5.5
Try looking here. As im not overly familiar with this. :)
http://www.mysqltutorial.org/stored-procedures-parameters.aspx
You need to create a query first. This query will then be stored in the database as a callable procedure. Then later using whatever language you can call the procedure.
DELIMITER //
CREATE PROCEDURE GetUserByCountry(IN countryName VARCHAR(255))
BEGIN
SELECT name, email
FROM users
WHERE country = countryName;
END //
DELIMITER ;
Then calling it.
CALL GetUserByCountry('mexico')
Returns all users names and emails who live in mexico.
I believe if you want to create a dynamic query string such as yours, you need to put {} around your variables in the string.
$query = "CALL my_procedure('{$inParam1}', '{$inParam2'}, '{$inParam3}', '{$inParam4}', '{$inParam5}', '{$inParam6}', '{$inParam7}', #outParam8, #outParam9, #outParam10); SELECT #outParam8, #outParam9, #outParam10;";

PHP/json My field names are being truncated to 30 characters. Can I stop this?

Ok so I got this piece of vendor software that they said should be run on an apache php server and MySql database.
I didn't have either of those so I put it on a PHP IIS server and I converted the code to work on SQL server.
ex.
mysql_select_db -> mssql_select_db
(among other things)
So I have the following code in a php file
$query = "SELECT * FROM TableName WHERE KEY_FIELD = '".$keyField."';";
$result = mssql_query($query);
$arr = array();
while ( $obj = mssql_fetch_object($result) )
{
$arr[] = $obj;
}
echo '{"results":'.json_encode($arr).'}';
and my results look something like this (captured with fiddler 2)
{"results":[{"KEY_FIELD":"57", "My30characterlongfieldthatiscu":"GoodValue"}]}
"My30characterlongfieldthatiscu" should be "My30characterlongfieldthatiscutoff"
Kinda weird, no?
The vendor claims that the app works perfectly on their end.
I'm thinking this is some sort of IIS PHP limit, is there a way around it or can I expand it?
I found this solution
http://www.php.net/manual/en/ref.mssql.php#74834
but I don't understand it.
Thanks!
See http://bugs.php.net/bug.php?id=33060 - this is what's causing your issue.
You might also want to consider changing the column names - more than 30 characters sounds excessively long.
http://docs.php.net/manual/en/mssql.requirements says:
Note: On Windows, the DBLIB from Microsoft is used. Functions that return a column name are based on the dbcolname() function in DBLIB. DBLIB was developed for SQL Server 6.x where the max identifier length is 30. For this reason, the maximum column length is 30 characters. On platforms where FreeTDS is used (Linux), this is not a problem.
Is using the sqlsrv extension instead of mssql an option?

Categories