how to print data of SUBSTRING_INDEX( ) field in mysql - mysql

$qry=mysql_query(SELECT title,SUBSTRING_INDEX( blog, ' ', 15),user_id,date_time
FROM blog_tab WHERE status='Active');
while($res=mysql_fetch_array($qry))
{
echo $res['blog'];
}
i use above query to show some content of paragraph in ma page but it do not allow to write the field which uses the function SUBSTRING_INDEX().how i can print the field of table with SUBSTRING_INDEX().

Well, the column name in this case is not blog, it is SUBSTRING_INDEX( blog, ' ', 15), so you would access it with $res["SUBSTRING_INDEX( blog, ' ', 15)"], or since you are using mysql_fetch_array you can access it with $res[1]. However, I suggest you alias it, for easier acces, and use mysql_fetch_assoc, like:
$qry=mysql_query("SELECT title,SUBSTRING_INDEX( blog, ' ', 15) AS blog,user_id,date_time
FROM blog_tab WHERE status='Active'");
while($res=mysql_fetch_assoc($qry))
{
echo $res['blog'];
}
NOTE: You should not use mysql_* functions as they are deprecated and will no longer be supported. Read more at http://au1.php.net/mysql_query

Related

What should be the structure if I want to use concat and encodeURI in the same JSON for Azure DevOps pipeline

I have a code in my logic app that gets a list of warehouses from an API URL. In it, I am trying to define a parameter that picks my URL for the azure DevOps pipeline when deploying my logic app, but when I run the pipeline I get the error
Deployment template validation failed: 'The template resource
'MyLogicApp' at line '1' and column '1465' is not valid: Unable to
parse language expression
'concat('/v2/datasets/#{encodeURIComponent(encodeURIComponent(''https://',parameters('myApiUrl'),'.com''))}/tables/#{encodeURIComponent(encodeURIComponent('msdyn_warehouses'))}/items')':
expected token 'RightParenthesis' and actual 'Identifier'.. Please see
https://aka.ms/arm-template-expressions for usage details.'.
This is what my json looks like
"
[
concat
(
'
/v2/datasets/#
{
encodeURIComponent
(
encodeURIComponent
(
'
'
https://
',
parameters
(
'
myApiUrl
'
),
'
.com
'
'
)
)
}/tables/#
{
encodeURIComponent
(
encodeURIComponent
(
'
msdyn_warehouses
'
)
)
}/items
'
)
]
"
If I use it without concat, it works
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('https://bla.bla3.com'))}/tables/#{encodeURIComponent(encodeURIComponent('msdyn_warehouses'))}/items"
Is there a specific way to format concat and encodeURI together that I am missing?
After checking thoroughly, you are receiving the above-mentioned error as because of the incomplete template (i.e., it misses a right parenthesis).
the uri from the parameter within the encoding statement
In that case you need to include concat() inside the encodeURIComponent and below is how you need to frame.
/v2/datasets/#{encodeUriComponent(encodeUriComponent(concat('https://',parameters('myApiUrl'),'.com')))}/tables/#{encodeURIComponent(encodeURIComponent('msdyn_warehouses'))}/items

Save the xml result to a column in a SQL table

I have copied the code somewhere in the internet and have created an html table using something like
''FOR XML RAW (''TR''), ELEMENTS, TYPE) AS ''TBODY''',
' FOR XML PATH (''''), ROOT (''TABLE'')'`
in SQL. The result is as expected which is an HTML table, below is the snippet.
Can someone point me on how to get the HTML string and save it into a column in my table. My thought was to get the result and save it as a string then insert it into a column in my table but after sometimes I failed.
The code example can be retrieve from https://www.mssqltips.com/sqlservertip/5025/stored-procedure-to-generate-html-tables-for-sql-server-query-output/
Cheers!! it's simple but I went around the globe. What I did was adding SET #Myvariable = in the dynamic query. Previously, I have tried so hard to assign the result into a variable outside the dynamic query which will never work for me.
Originally:
SET #DynTSQL = CONCAT (
'SELECT (SELECT '
, #columnslist
,' '
, #restOfQuery
,' FOR XML RAW (''TR''), ELEMENTS, TYPE) AS ''TBODY'''
,' FOR XML PATH (''''), ROOT (''TABLE'')'
)
What I did:
SET #DynTSQL = 'SET #SQLQuery1 =('+CONCAT (
'SELECT (SELECT '
, #columnslist
,' '
, #restOfQuery
,' FOR XML RAW (''TR''), ELEMENTS, TYPE) AS ''TBODY'''
,' FOR XML PATH (''''), ROOT (''TABLE'')'
)+')'
The result will be assigned into the #SQLQuery1.
Cheers!!!

MySQL to JSON not formed properly

I am trying to return JSON formatted results from a MySQL query but cannot get the correct format - it needs to be e.g.
{comCom:'test 3', comUid:'63',... etc
But what I'm getting is without apostrophes
{comCom:test 3, comUid:63,... etc
I am running the query in PHP as follows (shortened for ease of reading)
$result = mysql_query("select...
...GROUP_CONCAT(CONCAT('{comCom:',ww.comment, ', comUid:',h.user_id,', comName:',h.name,', comPic:',h.live_prof_pic,',comUrl:',h.url,',comWhen:',time_ago(ww.dateadded),'}')) comment,...
How can I get the punctuation?
I know mysql_query is deprecated btw, just in process of moving things to MySQLi
Can you not just escape the ' character with \'?
...GROUP_CONCAT(CONCAT('{comCom:\'',ww.comment, '\', comUid:\'',h.user_id,'\', comName:\'',h.name,'\', comPic:\'',h.live_prof_pic,'\',comUrl:\'',h.url,'\',comWhen:\'',time_ago(ww.dateadded),'\'}'))
or use a mixture of " with '
...GROUP_CONCAT(CONCAT("{comCom:'",ww.comment, "', comUid:'",h.user_id,"', comName:'",h.name,"', comPic:'",h.live_prof_pic,"',comUrl:'",h.url,"',comWhen:'",time_ago(ww.dateadded),"'}"))

Splitting a string in SQL using MySQL

I need to take a postal code looking like:
S7Y 6H5
that's in a table and display it as
S7Y & 6H5
I can't find a command that splits the string in sql.
MySQL doesn't include a split function, but it does include a replace. You could use:
SELECT REPLACE('S7Y 6HS', ' ', ' & ')
The greater question for me, though, is why don't you do that in your application code?
Try this:
Select REPLACE ('S7Y 6H5', ' ', ' & ')
you can use INSERT INSERT(str,pos,len,newstr)
SELECT INSERT('S7Y 6H5', 4, 0, ' & ');
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_insert

using like in join with code igniter active records

I am having issues with a particular like in my active records query.
When I use join('users parent', 'child.treePath LIKE CONCAT(parent.treePath,"%")')
Code igniter spits out JOIN 'users' parent ON 'child'.'treePath' 'LIKE' CONCAT(parent.treePath,"%") (note that I have replaced all back ticks (`) with (') due to markdown :/)
So, the issue is that code igniter is wrapping LIKE in (`).
How can I tell it to not attempt to format this block?
Complete query:
$this->db->select('child.uuid')
->from('users child')
->join('users parent', 'child.treePath LIKE CONCAT(parent.treePath,"%")')
->where('parent.uuid', $uuid)
->where("LENGTH(REPLACE(child.treePath, parent.treePath, '')) - LENGTH(REPLACE(REPLACE(child.treePath, parent.treePath, ''), '/', '')) <= ", $levels, 'false')
->where("LENGTH(REPLACE(child.treePath, parent.treePath, '')) - LENGTH(REPLACE(REPLACE(child.treePath, parent.treePath, ''), '/', '')) > ", 0, 'false')
->group_by('child.treeId');
If you are chaining all these functions together in a single call, you might as well just use
$this->db->query("Write all your specific SQL here");
Not seeing the benefit of wrestling with Codeigniter's query builder in your case.