I want to export a data from database into an excel file. i've tried to use PHPExcel
im trying to get some data with id as the parameter with this code, but the result of the export is showing all data. meanwhile, when i do the var_dump at the '$rowExcel->name', its showing a data based on id (not all data).
$this->db->select('id, name, r_value');
$this->db->where_in('id', $id);
$result = $this->db->get("person");
$rows = 3;
foreach ($result->result() as $rowExcel){
$excel->getActiveSheet()->setCellValueByColumnAndRow(0,$rows,'');
$excel->getActiveSheet()->setCellValueByColumnAndRow(1,$rows,$rowExcel->name);
$rows++;
}
what i want is to export the data with the id as the parameter (not export all data).. btw, the id is an array
Related
I have file PHP by this file I update the data in MySQL table. I send data to this PHP file from flutter app but there are one problem I have 3 field in this file so user can update data in those 3 Column in MySQL table to here every thing is ok but my problem if user send just one Column data from flutter app to PHP file the data will update in this Column but the others Column will will become null.
So how I can make this file make skip to any empty column the user not send data to it?
I need the old data not be changed in the database if the file does not get new data for that column.
Thank you.
PHP file:
<?php
require_once 'con.php';
$id = $_POST['id '];
$IDbook= $_POST['IDbook'];
$IDbookset= $_POST['IDbookset'];
$sql="UPDATE topics SET IDbook= ? ,IDbookset=? WHERE id=?";
$stmt = $con->prepare($sql);
$stmt->bind_param("sss",$IDbook,$IDbookset,$id);
$stmt->execute();
$result = $stmt->get_result();
$exeQuery = mysqli_query($con, $sql) ;
if($exeQuery){
echo (json_encode(array('code' =>1, 'message' => 'Modifier avec succee')));
}else {echo(json_encode(array('code' =>2, 'message' => 'Modification Non Terminer')));
}
?>
One way to do it is to
Fetch the data for the current $id say into $OldIdbook and $OldIdbookset
get updated values like this $IDbook= $_POST['IDbook'] ?? $oldIDbook; This uses the old value if the $_POST is null.
then execute the query to update.
The second way is to construct the query by adding only the field=value pairs that have changed to the query. Its a little bit more work to handle the comma.
I'm have a live website that uses PHP and a mySQL database. I'm looking to use d3 to create a few visualizations. But I don't know how to alter the data as it is stored in the SQL database before directly encoding it in a JSON for d3. My PHP:
$activity_array = $array;
while ($activity = mysqli_fetch_assoc($get_all_activities)) {
// $user_name = find_user_by_id($activity['user_id']);
// echo $user_name['last_name'];
$activity_array[] = $activity;
}
echo json_encode($activity_array);
However, due to how my mysql database is set up, users are set up as numbers, so that what I get back in the JSON looks like:
[{"id":"93","date":"2020-05-01","user_id":"37","user_notes":"This user has blah blah."},...]
When I use this JSON to generate a graph in d3, I want it to show the actual user's name, not "37". In the PHP code above I commented out the query I have to get the user's name from their user_id, but then I have no idea how to get that into my JSON.
Thanks for the help!
You just need to modify the appropriate entry in the $activity array with the username retrieved by the second query:
$activity_array = $array;
while ($activity = mysqli_fetch_assoc($get_all_activities)) {
$user_name = find_user_by_id($activity['user_id']);
$activity['user_id'] = $user_name['last_name'];
$activity_array[] = $activity;
}
echo json_encode($activity_array);
It would however probably be easier to modify the $get_all_activities query to JOIN to the users table and fetch the username in that query directly.
When I'm trying to perform basic CRUD operation on my database, but here when I tried to get a single row from my database whit a php script, I see the correct data from my database, but when I'm put more rows on de data base, and I tried to get the data, it never appears.
I'm using this PHP Script
<?php
//Importing Database Script
require_once('Connectdb.php');
//Creating sql query
$sql = "SELECT * FROM ensaladas ";
//getting result
$r = mysqli_query($con,$sql);
//creating a blank array
$result = array();
//looping through all the records fetched
while($row = mysqli_fetch_assoc($r)){
//Pushing name and id in the blank array created
array_push($result,array(
"ensid"=>$row['ensid'],
"nombre"=>$row['nombre'],
"precio"=>$row['precio'],
));
}
//Displaying the array in json format
echo json_encode(array('result'=>$result));
mysqli_close($con);
?>
And gives me the next result:
I use this when the table has 2 rows, and when I put another rows, the code stop Working. The table of the database has 3 fields: ensid,nombre,precio. As you can see here:
PD: I'm using this script to get the data on an Android APP
Error checking
Do some error checking on your part. You can also try to run the query at the back-end of your system (PhpMyAdmin).
Fetching data
Since you are using mysqli_* API already, try using prepared statement instead:
$stmt = $con->prepare("SELECT ensid, nombre, precio FROM ensaladas");
$stmt->execute();
$stmt->bind_result($ensid, $nombre, $precio);
while($stmt->fetch()){
array_push($result, array(
"ensid"=>$ensid,
"nombre"=>$nombre,
"precio"=>$precio,
));
}
$stmt->close();
Displaying the data
You can then now display all the data using:
echo json_encode($result);
Or if you want a specific row from the result, you can use an index:
echo json_encode($result[$x]); /* $x REPRESENTS THE INDEX; INTEGER VALUE */
Or get a specific data from a specific row:
echo json_encode($result[$x]['ensid']); /* EITHER ensid, nombre, or precio */
When I took a look into your database selection example(screen), the table name that you are querying is 'bebidas', while in the screenshot of the database you have used 'ensaladad'.
Try to use:
// Creating SQL query
$sql = "SELECT * FROM ensaladas ";
or use:
// looping through all the records fetched
while($row = mysqli_fetch_assoc($r)){
// Pushing name and id in the blank array created
array_push($result,$row));
}
I generate chart using jpgraph. I succesfully get data for the plot from database and i want the title also take from database. i already use this script
$sql = $this->db->select("title from table")->get()->first_row();
$title = $sql->title;
$graph->title->Set($title);
But that not work. can anyone solve this issue? thank you
This will help to get first_row along with field:
$this->db->select('title'); // your column
$this->db->from('table'); // your table
$result = $this->db->get()->result(); // get result
$title = $result->first_row()->title; // get ist row using first_row with your field name
$graph->title->Set($title); // as you are using for graph
Also note that, in CI function row() also return the ist row of your query in object form.
From the manual:
You can walk forward/backwards/first/last through your results using
these variations:
$row = $query->first_row()
$row = $query->last_row()
$row = $query->next_row()
$row = $query->previous_row()
Or if you want to print result in array instead of object than you can use a word 'array' as param, like:
$query->first_row(‘array’)
You can also follow the CI manual for better understanding: http://www.codeigniter.com/userguide3/database/results.html
Ok, I am new here and if my question has already been answered, please direct me, but I cannot find it.
I have a database with products in it. I receive a list of product codes from my supplier in a csv format. It has only 1 column. I want to take that list of product codes and compare to the list of codes in my database. If a product code is on the list in the csv, I want to modify a stock status value for that product in the database. so basically,
if "item_a" exists, change "value_b".
I hope I have been clear enough to get the correct answer...
Thank you in advance.
P.S. This>>PHP MYSQL import CSV and then compare and remove redundant entries
seems to be in the right direction, however instead of deleting, I want to change a different value
store all the csv data in an array then use this:-
foreach($csvId as $productID){
$productID = mysql_real_escape_string($productID);
$res = mysql_query('select count(*) from products where product_id = ' .$productID) or die();
$row = mysql_fetch_row($res);
if ($row[0] > 0)
{
//productexists
$updated_result = mysql_query('UPDATE products SET product.item_a="value_b" where product_id = ' .$productID) or die();
}
else
{
//It doesn't
}
}
you can user php function fgetcsv() get colum, than strcmp() compare diff value update.
>> fgetcsv()