i want make a different link for array data in mysql database.
for example i store data like below in my database:
data1,data2,data3 in one column
and i want fetch them like below by one query:
echo'
data1
data2
data3';
If your data is stored as "data1,data2,data3" in a column named "column" of your table:
$data = explode(",", $row["column"]);
echo 'data1';
But as said above, you should avoid storing multiple data in one column.
Something like this in your database retrieving loop :
$data = explode(',',$row['COLOUMN_NAME']);
echo'
data1
data2
data3';
Related
I have this table:
id
data
1
{"customers":[{"name": "test1","number": 1111},{"name": "test2","number": 2222}],"enableCheck": true}
And my new values:
$new_customer_name = "test3";
$new_customer_number = "3333";
I need a query to add this new values to the data column, I want this:
id
data
1
{"custumers":[{"name": "test1","number": 1111},{"name": "test2","number": 2222},{"name": "test3","number": 3333}],"enableCheck": true}
I try to use select query and edit json and update data but i need a query to update JSON directly into JSON of data
I found this and work:
UPDATE inbunds SET data = JSON_INSERT(data, '$.customers[2]', json('{"name": $new_customer_name,"number": $new_customer_number}')) WHERE id = 1
But now i need count JSON/s in $.customers and add JSON/s into end of $.customers
I have data from MySQL database and data from base64 file out of database
I want to combine them so i can have one JSON data with both
$PaFile="JVBERi0xLjcKMS.....";
$NaFile="JVBERi0xLjvvm.....";
$sqlRun = mysqli_query($conn, "SELECT laimCode, laimYear, laimMonth FROM laim_folio");
$row = mysqli_fetch_assoc($sqlRun);
$json_array[] = $row;
$jasondata = json_encode($json_array[]);
What i expect as output is
[{
"laimCode":"1234",
"laimYear":"2021",
"laimMonth":"11",
"PaFile":"JVBERi0xLjcKMS.....",
"NaFile":"JVBERi0xLjvvm....."
}]
If i put these two variable in SQL as static column with value i can get the result i want
But is there way to combine them outside SQL ?
Like extends array with two extra field and then convert to JSON
You can either add them to the array after fetching the data from the database and before adding into the overall array....
$row = mysqli_fetch_assoc($sqlRun);
$row['PaFile'] = "JVBERi0xLjcKMS.....";
$row['NaFile'] = "JVBERi0xLjvvm.....";
$json_array[] = $row;
$jasondata = json_encode($json_array);
Or add the values to the SQL so they become part of the result set. So the value is just a literal and the alias becomes the column name...
SELECT laimCode, laimYear, laimMonth,
"JVBERi0xLjcKMS....." as PaFile,
"JVBERi0xLjvvm....." as NaFile
FROM laim_folio
I have recently used this concat operator to join columns in JSON.
select concat('{"laimCode":',laimCode,',"laimYear":',laimYear,',"laimMonth":',laimMonth,',"PaFile":"JVBERi0xLjcKMS.....", "NaFile":"JVBERi0xLjvvm....."}') as json FROM laim_folio
I have a table which holds truck info . I have another table which holds driver info (name and truck id there two of them in each trip) . I make a daily report from the first table regarding it's current status and I want to have a cell which holds the two current drivers. When I do join on the tables I get two rows from each truck and the only difference is the name of the driver.
Is there a way to produce a single row (either PHP or SQL query) ?
to get an idea my current result is this :
Truck:DRIVER1:Date:Destination:Cargo:Remarks
Truck:DRIVER2:Date:Destination:Cargo:Remarks
And I want to get something like this :
Truck:DRIVER1 DRIVER2:Date:Destination:Cargo:Remarks
is that even possible ? I'm fairly new to this
Say for example you have stored first fetched string in $str1 and second in the second variable $str2.
You can now just use ltrim() and rtrim() functions as
<?php
$str1 = 'Truck:DRIVER1:Date:Destination:Cargo:Remarks';
$str2 = 'Truck:DRIVER2:Date:Destination:Cargo:Remarks';
$result = ltrim($str2,'Truck:');
$result = rtrim($result,':Date:Destination:Cargo:Remarks');
?>
After this you get the value of $result as DRIVER2
And then you can just concatenate the result into string one as
$str1 = 'Truck:DRIVER1 '.$result.':Date:Destination:Cargo:Remarks';
And when you echo the $str you will get the desired output
Output
Truck:DRIVER1 DRIVER2:Date:Destination:Cargo:Remarks
How can I add a value into an array in a database row, and then later on, when I want to get values of the array, simply display each different array value on a new line?
Also, how do arrays work in mysql and how to get the value of it?
Filed testField has serialized data.
$arrayData = array('one','two','three');
$serializedData = serialize($arrayData);
Mysql insertion:
insert INTO table ('testField') Values ($serializedData);
To get data:
select testField from table where id=1
You are getting here some string value. Then you should unserialize this string to get array:
$arrayData = unserialize($selectResultValue);
Look here for more details about serialize function:
http://php.net/manual/en/function.unserialize.php
MySQL does not have a native array data type. So, your first step is to figure out how you will store the elements of the array in a MySQL table.
Will you store each array element in its own database row?
elem val
1 value1
2 value2
...
n valuen
Or will you store the arraw as a series of concatenated values in a single row, something like this?
values
value1,value2,value3,...,valuen
In the first case, you can update a single array element easily:
UPDATE array SET val=newValue
WHERE elem=updatedElement
In the second case, you'll have to read the values column, break it out (deserialize it) into an array, change the element or elements you want to change, then gather it up (serialize it) back into a values column, and update your mySQL table. #Anthony pointed this out.
You need to answer the question about how you're storing the array before you can start to figure out how you will update it.
save array
$foo = array(1,2,3);
mysql_query(sprintf("INSERT INTO some_table ('foo') VALUES ('%s')", serialize($foo));
foo will appear as a string 1,2,3 in the database now
fetch array
$result = mysql_query("SELECT id, foo FROM some_table")
$item = mysql_fetch_object($result);
$foo = $item->foo;
$foo = unserialize($foo);
add data to array
$foo[] = 4;
$foo = array_uniq($foo); // you might want to ensure only unique values
mysql_query(sprintf("UPDATE some_table SET foo='%s' WHERE id=%d", serialize($foo), $item->id);
foo will appear as a string 1,2,3,4 in the database now
What is the best way to get the all values of one multiple select box, and then i want to save those values in one row in the database, then from that row i want to get each value separate. What is the best way to do such a thing ? My goal is to save the values and then get each separate.
You could do it this way
<select name="foo[]" multiple="multiple">
<option value="dog">Dog</option>
<option value="cat">Cat</option>
<option value="fish">Fish</option>
</select>
<?php
$pets = $_POST['foo'];
//sanitize and verify the input here. Escape properly and what not
$query = mysql_query("INSERT INTO `pets` (`pet1`, `pet2`, `pet3`) VALUES ('".$pets[0]."', '".$pets[1]."', '".$pets[2]."')");
?>
But to be honest it's an awful way to go about building a database. It'll be very annoying or difficult to do anything meaningful with.
Why not have the following database setup:
users:
id | name
----------
1 | Tim
pets:
id | user_id | type
-------------------
1 | 1 | Fish
2 | 1 | Cat
3 | 4 | Kakapo
And then you would have a much more easily searchable and manipulatable database that's consistent.
I believe you should receive the value selected as an array in the $_POST variable so say your select has a name="products"
The values selected will be in $_POST["products"] assuming you are submitting the form via POST.
Then you can use the implode function to generate a string. For example:
$myProducts = implode("|", $_POST["products"]); //This will give you a pipeline delimeted string like : computer|laptop|monitor
$myProducts = mysql_real_escape_string($myProducts); //Just to santize before inserting in DB
Then just insert that string into the DB.
When retrieving the data you can reverse the process by using the explode function:
$myProducts = explode("|" , [The value retrieved from the database]); //This will give you an array which you can iterate and thus accessing the values individually.
Hope this helps :)
if i understand your question well...
$dataFromMultipleBox = array('data1', 'data2', 'data3');
$data = implode("||", $dataFromMultipleBox);
/*
After that,
write $data into database
fetch from the database again
*/
$pieces = explode("||", $rowFromDatabase);
foreach($pieces as $value) {
echo $value;
echo '<br>';
}
What is the best way to get the all values of one multiple select box,
Since it is PHP. Give the select element a name ending in [], then you can access them as $_POST['foo'][]
and then i want to save those values in one row in the database, then from that row i want to get each value separate.
Don't do that. Have a second table with two columns. The id (as a foreign key) of the row in the other table (where you were planning to store the data) and the value itself.