Jump to content
techker

array in mysql database?

Recommended Posts

Hey guys i have a database set up and im trying to figure out how to show and edit the content in a table that has 

 

["http:\/\/streams.com:\/live\/A245\/23424\/6207.ts","http:\/\/streams\/live\/123\/43r3222\/27357.ts"]

 

so when i eco this i get that? is this stored as an array? and why the \/ is that separate values?

 

 

Share this post


Link to post
Share on other sites

The \ is escaping the /. Why is your data in the DB like that in the first place?

  • Haha 1

Share this post


Link to post
Share on other sites

It's JSON, by the way. If you're using MySQL 5.7+ and the JSON data type then it's okay, otherwise it's not very okay.

Share this post


Link to post
Share on other sites

that explains it ..json..hmm

its the panel that was made like that..im just connecting to the backend of it ..

ya the Mysql is the latest..

so how can i show that text in a way that we can modify and resave?

Share this post


Link to post
Share on other sites

If you want to work with it in PHP then you need to decode/encode it.

If you want to work with it in MySQL then use its JSON functions.

Share this post


Link to post
Share on other sites

last question..maybe..lol sorry

LEGEND: 1-admin 2 user ,3 user level 1 ,4 user level4 ....

in my table i have a column groups with [1,2,4]

how can i make a query to select the tables associated to groups [1] only

cause i have some that 

i tried..also tried 

JSON_CONTAINS but the mysql error said does not exist?

SELECT * 
FROM `packages` 
WHERE 2 IN (".implode(',','groups').")LIMIT 0 , 30

 

Share this post


Link to post
Share on other sites

What is the actual query you tried to run, as in whatever your PHP produced and not the code that you used to create it, and what was the error message?

Share this post


Link to post
Share on other sites

Here's an example

mysql> SELECT name, role FROM json_test;
+-------+--------------+
| name  | role         |
+-------+--------------+
| Peter | [1, 2, 3]    |
| Paul  | [1, 3]       |
| Mary  | [1, 2, 4]    |
| Jane  | [2, 4]       |
| Fred  | [1, 2, 3, 4] |
+-------+--------------+
5 rows in set (0.00 sec)

mysql> SELECT name, role FROM json_test WHERE JSON_CONTAINS(role, "3");
+-------+--------------+
| name  | role         |
+-------+--------------+
| Peter | [1, 2, 3]    |
| Paul  | [1, 3]       |
| Fred  | [1, 2, 3, 4] |
+-------+--------------+
3 rows in set (0.00 sec)

 

Share this post


Link to post
Share on other sites

odd cause no matter what i try i get an error like

 

SELECT * FROM `packages` WHERE  JSON_CONTAINS (groups , "4") ;

MySQL said: Documentation

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 

PHP/5.5.9-1ubuntu4.27

mysql 5.5.62

Share this post


Link to post
Share on other sites

ok i found that just the simple loop works..lol

 

$dataArray = json_decode($groups,true);
foreach($dataArray as $key => $value){
echo $value;
}

Share this post


Link to post
Share on other sites
On 4/7/2019 at 4:07 AM, techker said:

ya the Mysql is the latest..

 

1 hour ago, techker said:

mysql 5.5.62

One of those statements can't be true.

(JSON functions require MySQL 5.7+)

Share this post


Link to post
Share on other sites
Posted (edited)

odd so how come it is inserted like that..

how can i make a function to edit this:

"http:\/\/streams.com:\/live\/A245\/23424\/6207.ts","http:\/\/streams\/live\/123\/43r3222\/27357.ts"]

guessing i need to remove the\/ and replace by // . and when i save i need to re add the \/

 

im messing around with SELECT streams_source  REPLACE(streams_source,'\/','//') as output FROM Stream WHERE 1;

but getting mysql errors ..

Edited by techker

Share this post


Link to post
Share on other sites

Ok i think i got it..

 

$result= str_replace('\\', '', $stream_source);
$result = str_replace("'", "", $result);  //remove single quotes

so i remove the \

 

all i need to do is reverse that to save

Share this post


Link to post
Share on other sites

No. The backslashes are optional but okay. It does not matter if they are there. Do not try to manipulate the JSON string like that.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.