PHP - Mssql Query Truncating Strings
strings from a field(type: VARCHAR(4000)) are being truncated at 255 characters. Apparently this is how varchars work(how is that useful?). I've found these two solutions on the internet but neither of them seemed to work.
first, some people thought it might be a php.ini thing, so I put this at the top of my php file: Code: [Select] ini_set ( 'mssql.textlimit' , '65536' ); ini_set ( 'mssql.textsize' , '65536' ); that didn't work, so other people suggested using the text field type instead but these two queries return nothing, so I must be doing something wrong or this just isn't the answer: Code: [Select] $bquery="SELECT CONVERT(TEXT,description) FROM table WHERE userID='$row[userID]'"; $bquery="SELECT CAST(description AS TEXT) FROM table WHERE userID='$row[userID]'"; Similar TutorialsI'm restarting this under a new subject b/c I learned some things after I initially posted and the subject heading is no longer accurate. What would cause this behavior - when I populate session vars from a MYSQL query, they stick, if I populate them from an MSSQL query, they drop. It doesn't matter if I get to the next page using a header redirect or a form submit. I have two session vars I'm loading from a MYSQL query and they remain, the two loaded from MSSQL disappear. I have confirmed that all four session vars are loading ok initially and I can echo them out to the page, but when the application moves to next page via redirect or form submit, the two vars loaded from MSSQL are empty. Any ideas? Hello,
I am trying to run a report as per the below mssql query:
$query = "SELECT tblWJCItem.AddedDescription, tblWJC.WJCPrefix, tblWJC.WJCNo, tblWJCItem.MaterialName, tblStockFamily.StockFamily, tblWJCItem.WeightToSend, tblWJC.DateCreated, tblWJC.WJCStatusID FROM tblWJC INNER JOIN tblCustomer ON tblWJC.CustomerID = tblCustomer.CustomerID INNER JOIN tblWJCStockStatus ON tblWJC.WJCStockStatusID = tblWJCStockStatus.WJCStockStatusID INNER JOIN tblStockStatus ON tblWJC.WJCID = tblStockStatus.WJCID LEFT OUTER JOIN tblWJCProductLine ON tblWJC.WJCID = tblWJCProductLine.WJCID LEFT OUTER JOIN tblWJCStockItem ON tblWJCProductLine.WJCProductLineID = tblWJCStockItem.tblWJCStockItem INNER JOIN tblStockFamily ON tblWJCItem.ProductFamilyID = tblStockFamily.StockFamilyID WHERE tblCustomer.CustomerName = 'NAME' AND tblWJCStockStatus.WJCStockStatus <> 'Stock Usage Confirmed' ORDER BY tblWJC.WJCID"; I have a stored procedure on MSSQL that returns a list of results. This works fine. The results that are returned may or may not be allowed to be seen by the end user. So, if 10 results are returned, maybe 7 of them are allowed to be viewed by the end user. There is a PHP SESSION variable that contains a bunch of codes that is compared to a variable returned from the search result. When these match, the result can be seen by the user. So, I am using a php if statement that echos the result based on the result of the if statement. That works fine. What does not work is the found count for the array. For example, if 10 are found, it returns 10, but the if statement removes 3 making 7 of the results viewable. So, is there a way to iterate through an array result set in PHP and remove results based on an if statement creating a new array? Hope this is not too confusing! Hi all,
I am trying to do a query on a database that takes a variable from a html drop down box. I've tried so many different forums and can't find the answers.
HTML PAGE:
<html> hi i have the script below which copies data from one table to another but will only insert new data update current data or delete old data from tempproducts to products then it will delete the tempproducts from the db however i keep getting this error: Warning: mssql_query() [function.mssql-query]: Query failed in E:\UpdateProducts.php on line 33 updateproducts.php Code: [Select] <?php include('../../otherscripts/functions.php'); $log = new Logging(); // create DB connection $host = "localhost"; $user = "user"; $pass = "pass"; $mydb = "db"; $db = mssql_connect($host,$user,$pass); //Select Database mssql_select_db($mydb); // delete all old data $sql0 = "SELECT * FROM tempproduct"; $sql1 = "INSERT INTO products SELECT * FROM tempproduct WHERE manf_part_no NOT IN (SELECT manf_part_no FROM products) AND supp_id NOT IN (SELECT supp_id FROM products)"; $sql2 = "DELETE FROM products WHERE manf_part_no NOT IN (SELECT manf_part_no FROM tempproduct) AND supp_id NOT IN (SELECT supp_id FROM tempproduct)"; $sql3 = "UPDATE p1 SET p1.avail_qty = t1.avail_qty, p1.cost_price = t1.cost_price, p1.rrp = t1.rrp, p1.date_added = t1.date_added, p1.description = t1.description FROM Products p1 INNER JOIN tempproduct t1 ON (p1.manf_part_no = t1.manf_part_no AND p1.supp_id = t1.supp_id)"; $sql4 = "TRUNCATE TABLE tempproduct"; //If tempproduct is empty done Execute Commands if it is full then execute commands $query = mssql_query($sql0) or die($log->lwrite('Failed to select for count from db')); $rowcount = mssql_num_rows($query); if($rowcount == 0){ $log->lwrite('Teh tempproduct am emptyish'); } else{ mssql_query($sql1) or die($log->lwrite('Failed to insert to db'.$sql1)); mssql_query($sql2) or die($log->lwrite('Failed to Delete from db')); mssql_query($sql3) or die($log->lwrite('Failed to Update db')); mssql_query($sql4) or die ($log->lwrite('Failed to TRUNCATE db')); } ?> if i run $sql1 command in the sql manager it runs fine and no errors occur? Hi Guys Again, another noob question that I can't seem to find a concise answer to. What is the best way to append a query string to a url? I've tried using the Header() function but this didn't seem to work - i.e. Code: [Select] Header("Location: enc.php?ID=test"); My goal is to change the query string depending on the output of various if else statements. For instance if $test isset then execute some code. The script will be self contained so it'll be posting back to itself with the query string and reacting differently depending on the query string. Any ideas? PS sorry if i am asking too many questions. Eager to learn and struggling to find answers to some things. What I basically want to do (and not sure if this is possible) is to append a different query string based on Is it possible to have a link like this, http://www.somesite.com/?id=243 and transfer the "id" (or another query string) into the website from the link but at the same time, not affect the website (since "id" isn't a query string originally used in the website's URL)? I hope that makes sense.. Hello there, i`m trying to figure out a way to reorder a query string depending on variables. So the url would have the following query: /result/index.php?page=Weissenfels+Clack+and+Go&tyre=155_70_12&option1=30_02&option2=43_02&v_t=car&options=2 It should depend what page they come from if the page is equal to option2 then option1 is op1 and option2 is op2 To make it myself easier i have converted some variables On every first two digits ie 43 from the 43_02 can have 13 different ending digits, ie _03 or _04 or _05 etc... So i have converted all 43_02 43_03 etc to one variable ie $chain1 or $chain2 which holds in this case 43 a 43 is equal to Weissenfels+Clack+and+Go in the index page i have included the following process. At the moment i can only use php coding, unfortunately no mysql atm thank you in advance. Code: [Select] <?php $tyre = $_GET['tyre']; // finds the tyre size $op1 = $_GET['option1']; // finds the first chain size if applicable $op2 = $_GET['option2']; // finds the second chain size if applicable $op3 = $_GET['option3']; // finds the third chain size if applicable $op4 = $_GET['option4']; // finds the fourth chain size if applicable $op5 = $_GET['option5']; // finds the fith chain size if applicable $op6 = $_GET['option6']; // finds the sixth chain size if applicable $op7 = $_GET['option7']; // finds the seventh chain size if applicable $op8 = $_GET['option8']; // finds the eighth chain size if applicable $options = $_GET['options']; $page = $_GET['page']; $error = $_GET['info']; $vehicle_type = $_GET['v_t']; // finds the vehicle type $b = "Weissenfels+WeissTech+Tecna"; $c = "Weissenfels+Clack+and+Go"; if (($op1 == "30_02") || ($op1 == "30_03")) { $chain1 = "m30"; } elseif (($op2 == "43_02") || ($op2 == "43_03")) { $chain2 = "m43"; } else echo "error"; switch ($page) { case (($page == $c) && ($chain1 == "m43")); $option1 = $op1; $option2 = $op2; break; case (($page == $c) && ($chain2 == "m43")); $option1 = $op2; $option2 = $op1; break; default; $option1 = $op1; $option2 = $op2; break; } if ($options=="1") // calculates the possible combination { // outputs the results include 'options_1.php'; // type the amount of possible chains in here ie options_3 options_4 etc } elseif ($options=="2") { // outputs the results include 'options_2.php'; // type the amount of possible chains in here ie options_3 options_4 etc } elseif ($options=="3") { // outputs the results include 'options_3.php'; // type the amount of possible chains in here ie options_3 options_4 etc } elseif ($options=="4") { // outputs the results include 'options_4.php'; // type the amount of possible chains in here ie options_3 options_4 etc } elseif ($options=="5") { // outputs the results include 'options_5.php'; // type the amount of possible chains in here ie options_3 options_4 etc } elseif ($options=="6") { // outputs the results include 'options_6.php'; // type the amount of possible chains in here ie options_3 options_4 etc } elseif ($options=="7") { // outputs the results include 'options_7.php'; // type the amount of possible chains in here ie options_3 options_4 etc } elseif ($options=="8") { // outputs the results include 'options_8.php'; // type the amount of possible chains in here ie options_3 options_4 etc } else echo "there is an error on process_action2"; ?> I am building a project that requires I store query strings in a table (stage_reqs) which are called to determine permissions. These strings will look something like this: Code: [Select] select salesman from jobs where salesman is not NULL and job_id='".$this->job_id."' limit 1 The variable value needs to be determined from within the function it is being accessed in. Can I use eval to do this? Thanks, Chris Hello. First I'd like to say that I know my way around pretty good but I am not a veteran PHP programmer so talk to me like I'm stupid please Ok... I have a simple chat script set up that will only be used by a few people. I figured out how to truncate the chat when someone logs out, but I can't figure out how to truncate only if ALL users log out. Can someone lead me in the right direction? Thanks I have a line in a php email that is .....
$message .= '<table role="presentation" width="640" style="width:640px;" cellpadding="0" cellspacing="0" border="0" align="center"><tr><td align="center" bgcolor="#EEEEEE" background="https://safebuy.nz/images/background.png" valign="top" style="background: url("https://safebuy.nz/images/background.png") center / cover repeat #EEEEEE;">'; But when it arrives in email it reads..... <table role="presentation" width="640" style="width:640px;" cellpadding="0" cellspacing="0" border="0" align="center"><tr><td align="center" bgcolor="#EEEEEE" background="http://safebuy.nz/images/background.png" valign="top" style="background: url(" http:="" safebuy.nz="" images="" background.png?)="" center="" cover="" repeat="" #EEEEEE;?=""> So I get no background in my email. The rest of email sends fine.
Where have I gone wrong. Hi all I am populating a List menu with values from Mysql table. The values are populated correctly. Once a selection is made and upon submit when I display POST submit values using print_r($_POST) the result is correct only when the value do not contain spaces. For example if the list selection is "One" the $_POST[sel] value is correct. But if the selection is "Two Three" it is only first part before space. How do I get it to give complete value as in the list. Thanks and Regards SKN Hi all, I am relatively new to coding but wondered if someone can helpe me with this. I currently have a PHP file which outputs text that has been entered into a text box and saved. The original output code was this: if ( $this->theme->conf->item_description AND $this->doc->data->dmdescription ) : <?php echo $this->doc->data->dmdescription;?> but I want the text that shows to be truncated to say, 100 characters plus '...' so I came up with the following... <?php $someText = ($this->doc->data->dmdescription);?> <?php echo(substr($someText, 0,100)).'...'; ?> However it shows nothing. I guess $someText is bing linked to dmdescription rather than the text within dmdescription? Any ideas greatly appreciated! bplumb I have a full-text RSS component for Joomla that I was trying to modify. I wanted to be able to truncate the article to a given number of words and place a link at the end for the rest of the article. I need help figuring out how to add a link to the end of the source article. Added this script to the post.php file. It sort of works but the "$ending" tag doesn't seem to function since there is no "..." appended to the end of articles posted, when I tested it. public function truncate($text, $length = 200, $ending = '...', $exact = true, $considerHtml = false) { if ($considerHtml) { // if the plain text is shorter than the maximum length, return the whole text if (str_word_count(preg_replace('/<.*?>/', '', $text)) <= $length) { return $text; } // splits all html-tags to scanable lines preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER); $total_length = str_word_count($ending); $open_tags = array(); $truncate = ''; foreach ($lines as $line_matchings) { // if there is any html-tag in this line, handle it and add it (uncounted) to the output if (!empty($line_matchings[1])) { // if it's an "empty element" with or without xhtml-conform closing slash (f.e. <br/>) if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) { // do nothing // if tag is a closing tag (f.e. </b>) } else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { // delete tag from $open_tags list $pos = array_search($tag_matchings[1], $open_tags); if ($pos !== false) { unset($open_tags[$pos]); } // if tag is an opening tag (f.e. <b>) } else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) { // add tag to the beginning of $open_tags list array_unshift($open_tags, strtolower($tag_matchings[1])); } // add html-tag to $truncate'd text $truncate .= $line_matchings[1]; } // calculate the length of the plain text part of the line; handle entities as one character $content_length = str_word_count(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2])); if ($total_length+$content_length> $length) { // the number of characters which are left $left = $length - $total_length; $entities_length = 0; // search for html entities if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) { // calculate the real length of all entities in the legal range foreach ($entities[0] as $entity) { if ($entity[1]+1-$entities_length <= $left) { $left--; $entities_length += str_word_count($entity[0]); } else { // no more characters left break; } } } $truncate .= substr($line_matchings[2], 0, $left+$entities_length); // maximum lenght is reached, so get off the loop break; } else { $truncate .= $line_matchings[2]; $total_length += $content_length; } // if the maximum length is reached, get off the loop if($total_length>= $length) { break; } } } else { if (str_word_count($text) <= $length) { return $text; } else { $truncate = substr($text, 0, $length - str_word_count($ending)); } } // if the words shouldn't be cut in the middle... if (!$exact) { // ...search the last occurance of a space... $spacepos = strrpos($truncate, ' '); if (isset($spacepos)) { // ...and cut the text in this position $truncate = substr($truncate, 0, $spacepos); } } // add the defined ending to the text $truncate .= $ending; if($considerHtml) { // close all unclosed html-tags foreach ($open_tags as $tag) { $truncate .= '</' . $tag . '>'; } } return $truncate; } I have attached the whole file as well if you want to see the rest. This part of the script has the function that originally automatically added the link to the source article: if($link_original==1){ if(strlen($ret->fullText)) $ret->fullText.="<p><a href='". $item->link."' target='_blank'>".$text_link_original."</a></p>"; else $ret->introText.="<p><a href='". $item->link."' target='_blank'>".$text_link_original."</a></p>"; } The problem is that it gets truncated with the rest of the article now. $to = $notifyemail; $from = "xxx@xxx.com"; $subject = "[ $rawticket ] Random $notifytype Notification"; Currently the output is an email with a total of 15 numbers, i.e. SUBJECT: [ 000000001718989 ] Random $notifytype Notification I want to remove the 0's Would something like this work or is this not possible? "[ (substr($rawticket,9)) ] Random $notifytype Notification"; Hello, am having problem with connection to mssql server and database. Am using Xampp and i think there is some problem about his configuration. All the time am getting error : missing driver php_mssql.dll in xampp/php/ext/ ....and file php_mssql.dll is there (same problem with php_pdo_mssql.dll). Also am getting this error: "Fatal error: Call to undefined function mssql_connect() " I tried everything i could find on forums, blogs, and php documentation...but always same. btw... i removed ";" in php.ini from necessary lines and i copy ntwdblib.dll in necessary folders. This is my connection code: Code: [Select] $dbHost = 'Workstation:<port>'; $dbUser = 'user'; $dbPass = 'pass'; $dbName = 'database'; $con = mssql_connect ($dbHost, $dbUser, $dbPass) or die ('MsSQL connect failed. ' . mssql_error()); mssql_select_db($dbName) or die('Cannot select database. ' . mssql_error()); I also tried with: Code: [Select] $connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);but always same.... Can anybody please provide me some working solution ? Thanks. HI, I've upgraded from PHP version 5.2 to PHP version 5.4 and are not able to load / find mssql extension ? How do I use MSSQL in version 5.4 ?? Is MSSQL not supported in this version or am I (hopingly) missing something ??? Regards Dieter Hi, I have been working with inserting unicode characters into mssql database from a php application. My php web application displays the chinese characters as i entered and retrieves from database properly. But when I take a look at the database the fields are populated with some other values other than the characters i inserted. When i developed the same application in ASP.net the database has correct values inserted into it. Here is my code in php: Code: [Select] <?php // direct insert $var = mssql_connect('RAL-DEV-SQL01','TestDBSa','TestDB$@'); $selected = mssql_select_db('TestDB', $var); $myVar="日常生活"; $myChineseVar=$myVar; echo $myChineseVar; $query = "insert into TestChinese (TestName) values (N'{$myChineseVar}')"; $result = mssql_query($query,$var); echo "Inserted<br>"; echo $query; echo "<br>Result<br>"; $result2 = mssql_query("SELECT TOP 10 * FROM [TestChinese] order by SeqNum desc",$var); while($nt=mssql_fetch_array($result2)){ echo "$nt[SeqNum] - "."$nt[TestName] -"; echo "<br>"; } mssql_close($var); ?> Can someone help me with this problem Thanks |