Clear SQL table before importing data (php) - php

Hi guys as the title says i'm trying to clear My SQL table before importing new data , but the problem only the last two rows appears not all of them .
I tried this using "TRUNCATE"
// clear table
$sqli = "TRUNCATE TABLE MyTable";
mysql_query($sqli);
$sql = 'INSERT INTO MyTable VALUES("'.$Data1.'","'.$Data2.'") ';
mysql_query($sql);
mysql_close();
$sql = 'SELECT * FROM MyTable';
$req = mysql_query($sql);
while($data = mysql_fetch_array($req)){
$D1 = $data['data1'];
$D2 = $data['data2'];
echo "$D1<br />
$D2<br />";
}
i can see only the last two rows.

this is full code
part 1 : adding data into MySQL table
<?php
include( "............" );
$table = mta::getInput();
$Kills = $table[0];
$Deaths = $table[1];
// Send infos in MySQL
$base = mysql_connect ('........', '.........', '........');
mysql_select_db ('.........', $base);
$sql = 'INSERT INTO AccountsData VALUES("'.$Kills.'","'.$Deaths.'") ';
mysql_query($sql);
mysql_close();
// Return true if is added
// an other code here
?>
part 2 : get data from Mysql Table
<?php
$base = mysql_connect ('.........', '.........', '.........');
mysql_select_db ('..........', $base);
$sql = 'SELECT * FROM AccountsData';
$req = mysql_query($sql);
while($data = mysql_fetch_array($req)){
$Kills = $data['Kills'];
$Deaths = $data['Deaths'];
echo "$Kills<br />
$Deaths<br />";
}
mysql_close();
?>
before using "TRUNCATE TABLE" it's working fine and i can see all rows
like this:
example
player 1
player 2
player 3
player 4
after using "TRUNCATE TABLE" i can see only the last row
player 4.
All what i need is i want to clear the table before adding new rows.
Sorry it's the first time that i use https://stackoverflow.com/

Related

I need help getting my sub-query to work? I would like to know if it is faster using a sub-query then my foreach loop way?

// I will be transferring data from one table to another for a frontend-app, which will involove many tables. Time matters here. Original way I would repopulate one table to another
and my DB class works, it is not the problem so no need to put it up here. Thank you for help
$SQL = "SELECT * FROM staging_flows";
$RES = $db->query($SQL);
// looping thruogh the results of my query into staging_flows table
foreach($RES as $row){
$id = $row['id'];
$flow_name = $row['flow_name'];
$coops = $row['coops'];
$changed_flag = $row['changed_flag'];
$removed_flag = $row['removed_flag'];
// Updating the flows table with the data collected from staging_flows table
$SQL1 = "UPDATE flows SET flow_name='$flow_name', coops='$coops',
changed_flag='$changed_flag', removed_flag='$removed_flag' WHERE id='$id'";
$RES = $db->query($SQL1);
//I have been trying a truncate function I developed and the use of sub-query but I
can't get it to sub-query to work. I think the performTruncateTable() function is
sounds.
// Function for Truncate Table
function performTruncateTable($table_name){
$db = new Db();
$link = $db->connect();
$query = "TRUNCATE TABLE `" . $table_name . "`";
$query_response = $db->query($query);
return $query_response;
}
function performTruncateTable_DB($db,$table_name){
$link = $db->connect();
$query = "TRUNCATE TABLE `" . $table_name . "`";
$query_response = $db->query($query);
return $query_response;
}
// Truncate flows table
performTruncateTable_DB($db,'flows')
// Populate flows table with data from the staging flows table using a sub-query
$sql2 = "INSERT INTO flows (id,flow_name,coops) SELECT (id,flow_name,coops) FROM
staging_flows";
$res2 = $db->query($sql2);

How to update data using one select in other database table

I have two databases and i have one table "TabelaX" in database "Servidor1" with out data and other database "Servidor2" with one table "TabelaY". And i want do one select in table "TabelaY" and with her data do one Update in table "TabelaX" which is in another database. I already made some code but it is not working correctly.
<?php
$conn= mysqli_connect('localhost','root',null,'Servidor2') or die
(mysqli_connect_error());
if (!$conn) {
die("Falha de conexao: ". mysqli_connect_error());
}
$ID = $_POST['ID'];
$sql = "SELECT * FROM TabelaY WHERE ID = $ID";
$result = mysqli_query($conn, $sql);
mysqli_select_db($conn,"Servidor1");
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$row1 = $row["ID"];
$row2 = $row["Data"];
}
} else {
echo "0 results";
}
$sql = "INSERT INTO Servidor1.TabelaX (ID, Data)
SELECT ID, Data
FROM Servidor3.TabelaW
WHERE ID = $ID;";
$sql = "UPDATE Servidor1.TabelaX SELECT ID, Data FROM
Servidor3.TabelaW SET Data = $row2 WHERE $row1 = $ID;";
if (mysqli_multi_query($conn, $sql)) {
echo "Dados Inseridos";
} if (mysqli_multi_query($conn, $sql)) {
echo "Dados Atualizados";
}
mysqli_close($conn);
I have no idea what your query is trying to do, because you assign to $sql twice without ever executing the first query, but if you're asking how to update a row in tableX based on data from tableY, then:
UPDATE Servidor1.TabelaX as x, Servidor2.TabelaY as y
SET x.Data = y.Data
WHERE x.id = y.id
AND x.id = $someIdForWhichYouWantToUpdate
Also, do not do this:
$ID = $_POST['ID'];
$sql = "SELECT * FROM TabelaY WHERE ID = $ID";
Imagine what happens when the user posts 1; DROP DATABASE Servidor1 into the form. This is called SQL injection and your code is full of vulnerabilities to it.

Copy 1 sql table to another with some additional data

I have this code:
<?php
//MYSQL
$dbserver="..."; //adresa MySQL
$dblogin="..."; //jméno uživatele MySQL
$dbheslo="..."; //heslo MySQL
$dbnazev="..."; //název databáze MySQL
$mysqli = new mysqli($dbserver, $dblogin, $dbheslo, $dbnazev);
if ($mysqli->connect_error) {
die('Nepodařilo se připojit k MySQL serveru (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
$mysqli->query("SET NAMES 'utf8'"); // nastavíme kódování MySQL
while($row = $mysqli->query("SELECT * FROM `importovat_fyz` WHERE `tempid` IS NOT NULL")->fetch_assoc()){
$tempid = $row['tempid'];
$jmeno = $row['jmeno'];
$prijmeni = $row['prijmeni'];
$email = $row['email'];
$bydliste = $row['bydliste'];
$souhlas = "on";
$aktuality = "on";
$timestamp = time();
$hash = md5("77c0a83besxxxcg1a190ab90d".time().$tempid.rand(10000000, 99999999));
$poznamky = "import19052018";
$vloz ="INSERT into `potvrzenotest` set jmeno='".$jmeno."', prijmeni='".$prijmeni."', bydliste='".$bydliste."', email='".$email."', souhlas='".$souhlas."', aktuality='".$aktuality."', timestamp='".$timestamp."', hash='".$hash."', poznamky='".$poznamky."';";
$result=$mysqli->query($vloz);
$cas = date('H:i');
echo '['.$cas.'] ID '.$tempid.' imported.', PHP_EOL;
}
mysqli_close($mysqli); .
?>
I have one table with some data and I have to copy it to another table with some additional data (like hash etc.).
When I run the code above, I got this:
[17:17] ID 1 imported.
[17:17] ID 1 imported.
[17:17] ID 1 imported.
[17:17] ID 1 imported.
[17:17] ID 1 imported.
So, only 1 row is being copied.
Could you please help me, where is the problem?
while($row = $mysqli->query("SELECT * FROM `importovat_fyz` WHERE `tempid` IS NOT NULL")->fetch_assoc()){ ... }
This loop will never end. And it will fetch the first row again and again.
You should execute the query outside the loop:
$selectResult = $mysqli->query("SELECT * FROM `importovat_fyz` WHERE `tempid` IS NOT NULL");
while ($row = $selectResult->fetch_assoc()) { ... }
As a side note: Consider to use a prepared statement for your INSERT statement in the loop. This will prevent SQL syntax errors if some of the values may contail special characters like '. If you run them in one transaction, you might even improve the performance.

How to display data from a particular column across all row and loop it on a <p> tag?

I want to display data from all rows only for the column named 'text_notes' from my db.
And I want to loop that data in a p tag so that all data from column text_notes is displayed one by one till the end.
Below is the code I tried but it displays all the columns of the latest row and doesn't display the other rows.
I want the data from column 'text_notes' from all the rows to be displayed from latest at the bottom to the oldest at the top.
<?php
$query = "SELECT * FROM notes";
$conn = new mysqli(DBROOT,DBUSER,DBPASS);
$conn->select_db(DBNAME);
if ($conn == TRUE) {
$runQuery = $conn->query($query);
$resultNum = $runQuery->num_rows;
$result = $runQuery->fetch_array();
$resultRow = mysqli_fetch_row($runQuery);
$notes = $result['text_notes'];
for ($i = 0; $i < $resultNum;) {
echo "<p>".$resultRow[$i]."</p>";
$i++;
}
}
?>
$mysqli = new mysqli(DBROOT, DBUSER, DBPASS, DBNAME);
$query = "SELECT text_notes FROM notes";
$result = $mysqli->query($query, MYSQLI_STORE_RESULT);
// Cycle through the result set
$counter = 0;
while(list($text_notes) = $result->fetch_row())
echo "<p>";
$counter++;
echo $counter . " : ";
echo " $text_notes </p>";
// Free the result set
$result->free();
I think it works :) Thanks

How to display the content of more then one SQL database row

I am trying to retrieve the data stored in an unknown number of mySQL database rows and display them using HTML. At the moment I can display data from one row.
Each row has a unique id number, I was planning to iterate through by comparing this number to the variable counter. But it would then leave me with the issue of displaying the results in HTML. At the moment I am just echoing variables that contain data from the rows. However what I want to create is a HTML list that increases in length depending on how many rows are in the table.
Here is my current PHP code for retrieving a row from the database is:
$sql = "SELECT * FROM project_tasks WHERE project_name='$proj_name' AND task_id='$counter'";
$query = mysqli_query($db_conx, $sql);
$row = $query->fetch_assoc();
$task_id = $row["task_id"];
$proj_name = $row["project_name"];
$task_name = $row["task_name"];
$task_importance = $row["task_importance"];
$task_description = $row["task_description"];
$task_deadline = $row["task_deadline"];
$task_members = $row["task_members"];
$task_budget = $row["task_budget"];
At the moment I am just displaying some of the results in HTML using this code:
<div id="inner_container">
<?php echo "$task_id $proj_name $task_name $task_deadline"; ?>
</div>
$sql = "SELECT * FROM project_tasks WHERE project_name='$proj_name' AND task_id='$counter'";
$query = mysqli_query($db_conx, $sql);
while($row = $query->mysqli_fetch_assoc()) {
$task_id = $row["task_id"];
$proj_name = $row["project_name"];
$task_name = $row["task_name"];
$task_importance = $row["task_importance"];
$task_description = $row["task_description"];
$task_deadline = $row["task_deadline"];
$task_members = $row["task_members"];
$task_budget = $row["task_budget"];
echo "$task_id $proj_name $task_name $task_deadline";
}
Since you have built an associative array using fetch_assoc all you need to do is loop through that array. The OO example on http://php.net/manual/en/mysqli-result.fetch-assoc.php should get you what you need. A quick example:
$sql = "SELECT * FROM project_tasks WHERE project_name='$proj_name' AND task_id='$counter'";
$query = mysqli_query($db_conx, $sql);
echo '<div id="inner_container">';
while ($row = $query->fetch_assoc()) {
$proj_name = $row["project_name"];
$task_name = $row["task_name"];
$task_deadline = $row["task_deadline"];
echo "$task_id $proj_name $task_name $task_deadline";
};
/* free result set */
$row->free();
echo '</div>;

Categories