PHP Delete from database [duplicate] - php

This question already has answers here:
delete row in my database using php pdo [closed]
(2 answers)
Closed 7 years ago.
I want to delete a selected row from my database, but I don´t know how to accomplish it. I am completly new to this topic, so it would be really nice, when you could explain to me.
Here is my code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> PHP F1 - Datenbank</title>
<style>
button {
margin:5px;
margin-left:-0px;
}
.insert {
position:relative;
margin:0px;
margin-bottom:5px;
margin-top:-25px;
}
</style>
</head>
<body>
<h2> Insert Dokument für deine Datensätze ! </h2>
<form action="F1_PHP.php" method="post">
<input type="text" name="jahr" placeholder="Albert-Park"> <br><br>
<input type="text" name="sieger" placeholder="Australien"> <br><br>
<input type="text" name="schnellster" placeholder="Melbourne"> <br><br>
<input type="text" name="strecke" placeholder="Laenge"><br><br>
<input type="submit" name="formdaten" class="insert" value="Insert"> <br>
</form>
<table border="1">
<tr>
<th></th>
<th>Strecke</th>
<th>Land</th>
<th>Stadt</th>
<th>Länge</th>
</tr>
<?php
try {
$server = 'mysql:dbname=f1;host=localhost';
$user = 'root';
$password = '';
$options = array
(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$pdo = new PDO($server, $user, $password, $options);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($pdo){
if (isset($_POST["formdaten"])) {
$jahr = $_POST["jahr"];
$sieger = $_POST["sieger"];
$schnellster = $_POST["schnellster"];
$strecke = $_POST["strecke"];
$eintrag = $pdo->prepare("INSERT INTO Strecke
(pk_Strecke, Land, Stadt, Laenge) VALUES (?, ?, ?, ?);");
$eintrag->execute(array($jahr, $sieger, $schnellster, $strecke));
if ($eintrag == true) {
echo "Eintrag war erfolgreich";
} else {
echo "Fehler";
}
}
}
}
catch (PDOException $error) {
echo 'Verbindung fehlgeschlagen: ' . $error->getMessage();
}
try {
$query= 'SELECT pk_Strecke, Land, Stadt, Laenge FROM Strecke';
$stmt = $pdo -> query($query);
$deleteString = "";
while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
echo '<tr>';
echo ' <td>'."<input type='radio' name='markiert'>".'</td>';
echo ' <td>'. $row["pk_Strecke"].'</td>';
echo ' <td>'. $row["Land"]. '</td>';
echo ' <td>'. $row["Stadt"]. '</td>';
echo ' <td>'. $row["Laenge"]. '</td>';
echo '</tr>';
}
echo '</table>';
}
catch (PDOException $error) {
echo 'Fehler beim Lesen der Daten ' . $error->getMessage();
}
?>
<br>
<input type="submit" name="delete" value="Delete">
<?php
// delete button
?>
<input type="submit" name="update" value="Update">
<?php
//Update button
?>
<br>
<table border="1">
<tr>
<th> </th>
<th> Jahr </th>
<th> Sieger </th>
<th> Schnellste Runde </th>
<th> Strecke </th>
</tr>
<?php
try {
$query= 'SELECT pk_Jahr,Sieger,SchnellsteRunde,pk_fk_Strecke
FROM Rennen join Strecke on pk_fk_Strecke = pk_Strecke
order by pk_Jahr';
$stmt = $pdo -> query($query);
while( $row = $stmt->fetch(PDO::FETCH_ASSOC) )
{
echo '<tr>';
echo ' <td>'."<input type='radio' name='markiert'>".'</td>';
echo ' <td>'. $row["pk_Jahr"].'</td>';
echo ' <td>'. $row["Sieger"]. '</td>';
echo ' <td>'. $row["SchnellsteRunde"]. '</td>';
echo ' <td>'. $row["pk_fk_Strecke"]. '</td>';
echo '</tr>';
}
echo '</table>';
}
catch (PDOException $error) {
echo 'Fehler beim Lesen der Daten ' . $error->getMessage();
}
?>
</body>
</html>
I appreciate every tip from you.

Use the following statements:
$sql = "DELETE FROM `Rennen` WHERE `pk_Jahr` = :id_to_delete";
$query = $db->prepare( $sql );
$query->execute( array( ":id_to_delete" => 'Value of pk_Jahr of the row to delete' ) );

It looks like you have a good idea of how to set up the PHP side of things, so I'll just deal with the SQL part.
The general syntax for a delete using PDO and MySQL would be as follows:
$query = "DELETE FROM TableName WHERE Field = :value";
$stmt = $pdo->prepare($query);
$stmt-> bindParam(':value', $value);
$stmt->execute();
The field you're querying against on that table needs to be unique, unless you're wanting to delete multiple rows (so best to use a primary key for the field and value).
It looks like you're using racing circuits in one of your tables, so your table may look like this (I'll call the table "circuits"):
id (primary key), circuit, country.
You may have the following data in it:
1, Albert Park, Australia
2, Silverstone, Great Britain
3, Adelaide, Australia
To delete Albert Park from the database, you'd run this:
$value = 1;
$query = "DELETE FROM circuits WHERE id = :value";
$stmt = $pdo->prepare($query);
$stmt-> bindParam(':value', $value);
$stmt->execute();
To delete all circuits in Australia (2 of the 3 above records):
$value = "Australia;
$query = "DELETE FROM circuits WHERE country = :value";
$stmt = $pdo->prepare($query);
$stmt-> bindParam(':value', $value);
$stmt->execute();
You could pass the value for $value through a form, and using bindParam() protects against SQL injection.

Related

SQL query using string concatenation

I am new to php and I have a table "abc" where I have columns: id, name and age.
I want to do the following:
Only if a name is entered in the input field, corresponding data (id and age) should be shown using string concatenation to build SQL query.
This is for search functionality
What should be the SQL query for this question?
// Create connection
$conn = new mysqli($SERVER_NAME, $USER_NAME, $PASSWORD, $DATABASE_NAME);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = " SELECT name, CONCAT(id,'.',age) FROM personene WHERE name = 'VALUE_FROM INPUT_NAME'";
$result = $conn->query($sql);
$error = mysqli_error($conn);
// Store results
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php if(!empty($error))
echo "<p style='color:red'>$error</p>";
?>
<p>Please enter the name:</p>
<form action="<?=$_SERVER['PHP_SELF']?>" method="GET">
<input type="input" name="name" value="" />
<br/>
<input type="submit" name="sendbtn" value="Send" />
</form>
<?php
if(!empty($data)) {
echo "<h1>Persons:</h1><table border='1'><tr><th>Id</th><th>Firstname</th><th>Age</th></tr>";
foreach($data as $row) {
echo "<tr><td>".$row["id"]."</td>";
echo "<td>".$row["name"]."</td>";
echo "<td>".$row["age"]."</td></tr>";
}
echo "</table>";
}
else
echo "No data available";
echo '(Query: '.$sql.')';
?>
</body>
</html>
It is not clear why you want to concatenate fields in the SQL query when clearly in the html these fields are displayed in their own columns. The code you have is wide open to SQL Injection so you need to consider using a prepared statement to handle the user supplied input safely.
<?php
$data=[];
error_reporting( E_ALL );
if( $_SERVER['REQUEST_METHOD']=='GET' && !empty( $_GET['name'] ) ){
$SERVER_NAME='';
$USER_NAME='';
$PASSWORD='';
$DATABASE_NAME='';
mysqli_report( MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
$conn=new mysqli( $SERVER_NAME, $USER_NAME, $PASSWORD, $DATABASE_NAME );
try{
$sql = 'select `name`, `id`, `age` from `personene` where `name` = ?';
$stmt=$conn->prepare( $sql );
$stmt->bind_param('s', $_GET['name'] );
$stmt->execute();
$stmt->bind_result( $name, $id, $age);
while( $stmt->fetch() )$data[]=[
'name' => $name,
'id' => $id,
'age' => $age
];
$stmt->free_result();
$stmt->close();
$conn->close();
}catch( mysqli_sql_exception $e ){
exit( $e->getMessage() );
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Fetch user details</title>
</head>
<body>
<p>Please enter the name:</p>
<form method='GET'>
<input type='input' name='name' />
<br/>
<input type='submit' name='sendbtn' value='Send' />
</form>
<?php
if( !empty( $data ) ) {
echo "
<h1>Persons:</h1>
<table border='1'>
<tr>
<th>Id</th>
<th>Firstname</th>
<th>Age</th>
</tr>";
foreach( $data as $row ) {
echo "
<tr>
<td>{$row["id"]}</td>
<td>{$row["name"]}</td>
<td>{$row["age"]}</td>
</tr>";
}
echo "</table>";
} else {
echo "No data available";
}
?>
</body>
</html>
For the SQL query to find records related with name, you have to use this query,
select concat('id', '-', 'age') as user_data from abc where name = $REQUEST['search_name'];
OR you can use LIKE(check here) condition to get the records from the table.
select concat('id', '-', 'age') as user_data from abc where name like
$REQUEST['search_name'];
Here is an update for your code,
<?php
// Create connection
$conn = new mysqli($SERVER_NAME, $USER_NAME, $PASSWORD, $DATABASE_NAME);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// echo "Connected successfully";
$data = [];
$sql = "Please submit the form.";
if(isset($_GET['sendbtn']) ) {
$sql = " SELECT id, name, age FROM personene WHERE name = '". $_GET['name'] ."'";
$result = $conn->query($sql);
$error = mysqli_error($conn);
// Store results
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
}
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
if(!empty($error))
echo "<p style='color:red'>$error</p>";
?>
<p>Please enter the name:</p>
<form action="<?=$_SERVER['PHP_SELF']?>" method="GET">
<input type="input" name="name" value="" />
<br/>
<input type="submit" name="sendbtn" value="Send" />
</form>
<?php
if(isset($data) && !empty($data)) {
echo "<h1>Persons:</h1><table border='1'><tr><th>Id</th><th>Firstname</th><th>Age</th></tr>";
foreach($data as $row) {
echo "<tr><td>".$row["id"]."</td>";
echo "<td>".$row["name"]."</td>";
echo "<td>".$row["age"]."</td></tr>";
}
echo "</table>";
} else {
echo "No data available";
}
echo '(Query: '.$sql.')';
?>
</body>
</html>

Search facility results PhP

Ok so the problem is very simple, basically when you put for example "W" it should output hotel names and guest's surnames that contain that character. It doesn't that hotel names however it never gives me an output for guest's no matter what I put. There are several matching for guest's that should appear however I get nothing. I can't see any mistakes with my code... Help.
<!DOCTYPE html>
<html>
<head>
<title>Database</title>
<link href="style.css" rel="stylesheet" type="text/css"> <!-- This is linking style sheet (css)into this HTML page-->
<link href='https://fonts.googleapis.com/css?family=PT+Serif:400italic' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="navigation">
<form action="index.php" method="get">
<input type="submit" name="mainpage" value="Main Page" class="submitbut" id="but1" />
</form>
</div>
<form action="index.php" method="post">
<input type="text" name="search" id="searching" />
<input type="submit" name="data_submit" value="Search" id="scan" />
</form>
<?php
if( isset( $_GET['mainpage'] ) ) exit( header( "Location: mainpage.php" ) );
if ( isset( $_POST["data_submit"] ) ){
$search_term = strip_tags( trim( $_POST['search'] ) );
$conn = new PDO( 'mysql:host=localhost;dbname=u1358595', 'root' );
$stmt = $conn->prepare("SELECT * FROM `hotel` h
INNER JOIN `booking` b ON h.`hotel_id`=b.`hotel_id`
INNER JOIN `guest` g ON g.`guest_id`=b.`guest_id`
WHERE `name` LIKE :search_term;");
$stmt->bindValue(':search_term','%' . $search_term . '%');
$stmt->execute();
echo "
<table>
<tr>
<th>Hotels Matched</th>
</tr>";
while($hotel = $stmt->fetch()) {
echo "
<tr>
<td><a href='details.php?name=".$hotel['name']."'>".$hotel['name']."</a></td>
</tr>";
}
echo "</table>";
$stmt = $conn->prepare("SELECT * FROM `guest` g
INNER JOIN `booking` b ON g.`guest_id`=b.`guest_id`
INNER JOIN hotel ON b.`hotel_id`=h.`hotel_id`
WHERE g.`last_name` LIKE :search_term;");
$stmt->bindValue(':search_term', '%' . $search_term . '%');
$stmt->execute();
echo "
<table>
<tr>
<th>Guests Matched</th>
</tr>";
while($hotel = $stmt->fetch()) {
echo "
<tr>
<td><a href='details.php?name=".$hotel['first_name']."'>".$hotel['last_name']."</a></td>
</tr>";
}
echo "</table>";
$conn = NULL;
}
?>
</body>
</html>
With PDO Prepared statements with LIKE prepare FULL literal first.See PDO Wiki
ie.
$name = "%$name%";
I have simplified your code using one query. I have tested it on 2 tables, you will need to JOIN other table
<!DOCTYPE html>
<html>
<head>
<title>Database</title>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="search" id="searching" />
<input type="submit" name="data_submit" value="Search" id="scan" />
</form>
<?php
$host= "localhost";
$username="XXXX";
$password="XXXX";
$database="XXXX";
function writeTable($host,$database, $username, $password,$search_term) {
//Create query
$sql = "SELECT hotel.name AS hotel, guest.name AS guest
FROM `hotel`
LEFT JOIN `guest` ON hotel.guest = guest.id
WHERE hotel.name LIKE ?
OR guest.name LIKE ?
";
$html = '<table cellpadding="1" cellspacing="1">'. "\n";
//array for column names
$columnNames = array("hotel","guest");
//table header
$html .= '<tr>';
foreach ($columnNames as $value){
$html .= '<th>' . $value . '</th>';
}
$html .= '</tr>'. "\n";
// connect to the database
$db = new PDO("mysql:host=$host;dbname=$database", $hotelname, $password);
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
//Prepare and execute query
$stmt = $db->prepare($sql);
$stmt->execute(array($search_term,$search_term));
// setting the fetch mode
$stmt->setFetchMode(PDO::FETCH_ASSOC);
//Add content
while($row = $stmt->fetch()) {
$html .= '<tr>';
$html .= '<td>' . $row['hotel'] . '</td>';
$html .= '<td>' . $row['guest'] . '</td>';
$html .= '</tr>'. "\n";
}
$html .= '</table>';
echo $html;
// close the connection
$dbh = null;
}
$search = strip_tags(trim($_POST['search'] ) );
if(isset($search) ){
if ($search != ''){
$search_term = '%'.$search.'%';
}else{
$search_term ='';
}
}
writeTable($host,$database, $hotelname, $password,$search_term);
?>
You should be able to modify to suit.

pdo to retrieve data and populate a record

I have already asked a question about PDO user add records to database PDO, now I am unable to select data and insert them into a html form in order to allow a user what to choose and as a consequence to add record into a db table
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
?>
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
try {
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected to database<br />';
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
<?php
if ($_GET['action'] == 'edit') {
//retrieve the record's information
$sth = $dbh->prepare = 'SELECT
nome, cognome, indirizzo, civico, citta,
prov
FROM
tagesroma
WHERE
id = ' . $_GET['id'];
$sth = $dbh->execute();
extract($sth = $dbh->fetch());
} else {
//set values to blank
$nome = '';
$cognome = '';
$indirizzo = '';
$civico = 0;
$citta = '';
$prov = '';
}
?>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo ucfirst($_GET['action']); ?> Tages</title>
<style type="text/css">
<!--
#error { background-color: #600; border: 1px solid #FF0; color: #FFF;
text-align: center; margin: 10px; padding: 10px; }
-->
</style>
</head>
<body>
<?php
if (isset($_GET['error']) && $_GET['error'] != '') {
echo '<div id="error">' . $_GET['error'] . '</div>';
}
?>
<form action="commit.php?action=<?php echo $_GET['action']; ?>&type=tages"
method="post" accept-charset="UTF-8">
<table>
<tr>
<td>Nome</td>
<td><input type="text" name="nome" value="<?php echo $nome; ?>"/></td>
</tr><tr>
<td>Cognome</td>
<td><select name="cognome"></td>
<?php
//seleziona il tipo di cognome
$sth = $dbh->prepare = 'SELECT
cognome
FROM
tagesroma';
$sth->execute();
//popola con i risultati
while ($row = $sth->fetch()) {
foreach ($dbh->$row as $value) {
if ($row['id'] == $cognome) {
echo '<option value="' . $row['id'] .
'" selected="selected">';
} else {
echo '<option value="' . $row['id'] . '">';
}
}
}
?>
</select></td>
</tr><tr>
<td colspan="2" style="text-align: center;">
<?php
if ($_GET['action'] == 'edit') {
echo '<input type="hidden" value="' . $_GET['id'] . '" name="id" />';
}
?>
<input type="submit" name="submit"
value="<?php echo ucfirst($_GET['action']); ?>" />
</td>
</tr>
</table>
</form>
</body>
</html>
the error I am dealing with is the following:
Fatal error: Call to a member function execute() on a non-object on line 76
The error Call to a member function execute() on a non-object means this area of the code is invalid:
$sth = $dbh->prepare = 'SELECT
nome, cognome, indirizzo, civico, citta,
prov
FROM
tagesroma
WHERE
id = ' . $_GET['id'];
$sth = $dbh->execute();
The correct way is:
$sth = $dbh->prepare("
SELECT nome, cognome, indirizzo, civico, citta, prov
FROM tagesroma
WHERE id = ?
");
$sth->execute(array($_GET['id']));
Use double-quote if you want to use newlines
Know that prepare() is a function, so following it with = doesn't make sense
Tidy your code for readability
You are not using prepared statements properly. try this:
<?php
$id = $_GET['id'];
$sth = $dbh->prepare("SELECT
nome, cognome, indirizzo, civico, citta,
prov
FROM
tagesroma
WHERE
id = :id");
$sth->bindParam(":id",$id,PDO::PARAM_INT);
$sth->execute();
?>

How to delete from my database in PHP/mysql?

Hi there I have many implementations of some php files. All of which have some errors. I will start off with an apology as this is my first question on here and I am certain that I will do this incorrectly as I see many first timers do. I will give as much info as possible and make it relevant to as many people as possible.
I have a database and am having trouble deleting from it. The database is simple. It includes resource_id name room description time_available and uer_id.
Although I expect it to output name description and resources_id it only outputs name and description and it will not let me delete name by resources_id.
How to delete from my database in PHP/mysql?
This is my delete_resources.php
{
<html>
<head>
<title>Delete a Record from MySQL Database</title>
</head>
<body>
<?php
$db_host = "#######";
// Place the username for the MySQL database here
$db_username = "#######";
// Place the password for the MySQL database here
$db_pass = "#######";
// Place the name for the MySQL database here
$db_name = "#######";
//
$con = mysqli_connect("$db_host","$db_username","$db_pass","$db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
mysqli_close($con);
}
$result = mysqli_query($con, "SELECT * FROM resources");
echo 'name' . "\t" . 'description' . "\t" . 'resources_id';
echo "<br>";
while($row = mysqli_fetch_array($result))
{
echo $row['name'] . "\t" . $row['description'] . "\t" . $row['resources_id'];
echo "<br>";
}
// Echoes: string
echo gettype($array);
//
if(isset($_POST['delete']))
{
// Query to select an int column
$resources_id = $_POST['resources_id'];
$sql = "DELETE name From resources ".
"WHERE resources_id = $resources_id" ;
//mysql_select_db('b32_13993766_csc411');
//$retval = mysql_query( $sql, $conn );
if(! $result )
{
die('Could not delete data: ' . mysql_error());
}
else if( $result )
{
echo "Deleted data successfully\n";
}
//mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Resource ID</td>
<td><input name="resources_id" type="text" id="resources_id"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="delete" type="submit" id="delete" value="Delete">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
//
}
You are not executing that delete query. Should look like
$recources_id=intval($resources_id);
$sql = "DELETE FROM resources WHERE resources_id = $resources_id" ;
$result = mysqli_query($con, $sql); // This is missing
$sql_query="Delete from your_table_name where id ='".$your_id."'";
$sql = "DELETE FROM resources WHERE resources_id = $resources_id" ;
Your $result is not relevant at all with your delete query (it is referring to the $result above, not the one with the delete). Try changing to this and see if it works.
if(isset($_POST['delete']))
{
// Query to select an int column
$resources_id = $_POST['resources_id'];
$sql = "DELETE name From resources ".
"WHERE resources_id = $resources_id" ;
$result = mysqli_query($con, $sql); //add this line
//mysql_select_db('b32_13993766_csc411');
//$retval = mysql_query( $sql, $conn );
if(! $result )
{
die('Could not delete data: ' . mysql_error());
}
else if( $result )
{
echo "Deleted data successfully\n";
}
//mysql_close($conn);
}

fetch data from array is very slow while display

This is my code:
<?php
if(isset($_POST['submit']) & !empty($_POST['appid'])) {
$app = mysql_real_escape_string($_POST['appid']);
//database parameters
$conp = mysqli_connect($hostname, $user, $password, $database) or die('error in connection' . mysqli_error());
//actual data for appid's
$appsi = mysqli_query($conp, "SELECT distinct package_name FROM `user_app` where `app_id` = '$app'");
$all = array();
while($row = mysqli_fetch_assoc($appsi)) {
$all[] = $row["package_name"]; // array problem
}
foreach ($all as $value) {
$install = mysqli_query($conp, "SELECT COUNT(*) AS installs from `install` where package_name = '$value'");
$row = mysqli_fetch_assoc($install);
$data[] = '<b>' .$row["installs"] . '</b>';
$reg = mysqli_query($conp, "SELECT COUNT( DISTINCT `imei_num` ) AS reg FROM `user_app` WHERE package_name = '$value'");
$row = mysqli_fetch_assoc($reg);
$regd[] = '<b>' .$row["reg"] . '</b>';
}
}
mysqli_close($conp);
?>
<html>
<head>
<title>script</title>
</style>
</head>
<body>
<span style="text-align: center"><h1>Beta</h1></span>
<form name="query" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>Enter Application-Specific Id:</p>
<select name='appid'>
<?php
$conp = mysqli_connect($hostname, $user, $password, $database) or die('error in connection' . mysqli_error());
$getid = mysqli_query($conp, "SELECT distinct `app_id`, `appidt` from `user_app` group by `app_id`") or die('get data failed' . mysqli_error());
while(($row = mysqli_fetch_assoc($getid)) != null) {
echo "<option value = '{$row['app_id']}' selected = 'selected'";
if ($selected == $row['app_id']) {
echo "selected = 'selected'";
}
echo ">{$row['appidt']}</option>";
}
mysqli_close($conp);
?>
</select>
<p><input type="submit" name="submit" value="Go" /></p>
</form>
<div>
<p><?php echo '<br />' .'<b>'. 'Application Id : '. $app . '</b>'; ?> </p>
<hr />
<table border=2px width=100%>
<tr>
<th><b>App Packages</b></th>
<th><b>Registrations</b></th>
<th><b>Installs</b></th>
</tr>
<tr>
<td><?php echo implode("<br><br>", $all); ?></td>
<td align="center"><?php echo implode("<br><br>", $regd); ?></td>
<td align="center"><?php echo implode("<br><br>", $data); ?></td>
</tr>
</table>
<p><?php echo "$name"; ?></p>
</div>
</body>
</html>
I am fetching my all package names in an array: all[], packages might be 10 or 20 in ranges, after this i want all downloads corresponding to packages which is on another table name downloads and packages on another table app_packages.
I can't uses join because package table contain specific packages but downloads contain many number of downloads corresponding to packages.
So, i put all packages in all[] and use them in foreach loop name $value, now i get all installs per packages and i can display it via implode function. But in my frontend, when i select an appid from dropdown as you can see, it will take huge time to retrieve downloads number per packages. This is not what i want to display because it is very time taking.
Please see this problem, and if i missing something in explanation then i apologize, prompt me and i mention it.
Using query in loop is a bad idea. that is the reason you are geting slow result. it touches database on each iteration. you can do this with subquery or join as alternative way.

Categories