I have a php script that is gathering information from 6 different databases. And if that list includes 50 items, than I have around 50-200 queries to get all the information for all the items in a while loop.
I have today tried different "function()" in PHP to use on all the items, and I tried to fiddel around with inner join in MySQL without any results. And all my databases have similair names, like id, device etc.
PHP code i use for viewing the page (a bit altered, but just to show my thinking):
<?php
if(!empty($_GET['tel_sort'])) {
global $conn; //SQL connection
global $device_kost;
$tel_sort = $_GET['tel_sort'];
$result = mysqli_query($conn,"SELECT * FROM $device_kost WHERE id = $tel_sort ORDER BY prod, model");
} elseif(!empty($_GET['prod_sort'])) {
global $conn; //SQL connection
global $device_kost;
$prod_sort = $_GET['prod_sort'];
$result = mysqli_query($conn,"SELECT * FROM $device_kost WHERE prod = '$prod_sort' AND priv = 1 ORDER BY prod, model");
} else {
$result = mysqli_query($conn,"SELECT * FROM $device_kost WHERE priv = 1 ORDER BY prod, model");
}
$telesort = mysqli_query($conn,"SELECT * FROM $device_kost WHERE priv = 1 ORDER BY prod, model");
$prodsort = mysqli_query($conn,"SELECT * FROM $device_kost WHERE priv = 1 GROUP BY prod ORDER BY prod");
function ab($tel) {
global $conn;
global $device_max;
$telque = mysqli_query($conn,"SELECT * FROM $device_max WHERE device = '$tel'");
$telres = mysqli_fetch_assoc($telque);
return $telres;
}
function ts($tel) {
global $conn;
global $device_ts;
$current_date = date("Y-m-d");
$telque = mysqli_query($conn,"SELECT * FROM $device_ts WHERE device = '$tel' AND date_f <= '$current_date' AND date_t >= '$current_date'");
$telres = mysqli_fetch_assoc($telque);
return $telres;
}
function device($tel) {
global $conn;
global $device_kost;
$telque = mysqli_query($conn,"SELECT * FROM $device_kost WHERE type = '1'");
$telres = mysqli_fetch_assoc($telque);
return $telres;
}
function color($price, $kost, $ukon, $ts, $ab = NULL) {
if(!isset($price)) { $priceumoms="0"; } else { $priceumoms = $price; }
$priceumoms = $priceumoms / 1.25;
if($ts=="n/a" || empty($ts)) { $ts="0"; }
$totpris = $priceumoms + $ts;
$db = $totpris - $kost;
$dg = $db * 100;
if ($totpris == 0) {
$dg = "";
} else {
$dg = $dg / $totpris;
}
$dg = round($dg, 2);
$dg = $dg . "%";
if($db>=1200) { $colors="#006600"; } elseif($db>=900) { $colors="#00AA00"; } elseif($db>=700) { $colors="#00FF00"; } elseif($db>=500) { $colors="#FFFF00"; } elseif($db>=300) { $colors="#FF6600"; } else { $colors="#FF0000"; }
if($price==NULL) { $db = "n/a"; $colors = "#888888"; $price = "n/a"; $dg = "n/a";}
$color = array(
"db" => "$db",
"color" => "$colors",
"ts" => "$ts",
"price" => "$price",
"dg" => "$dg", );
return $color;
}
?>
<p>
<table>
<thead>
<tr>
<th>Device sort</th>
</th>Prod sort</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<form action="" method="get">
<?php
if(isset($_GET['ab_sort'])) {
echo "<input type=hidden name=\"ab_sort\" value=\"" . $_GET['ab_sort'] . "\">\n";
}
?>
<select name="dev_sort" style="width: 20em;" onchange="this.form.submit()">
<option value="">Show all</option>
<?php
while($row = mysqli_fetch_array($telesort)) {
$id = $row['id'];
$prod = $row['prod'];
$model = $row['model'];
echo " <option value=\"$id\">$prod $model</option>\n";
}
?>
</select>
<input type=submit value="Update"></form></td>
<td>
<form action="" method="get">
<?php
if(isset($_GET['tel_sort'])) {
echo "<input type=hidden name=\"tel_sort\" value=\"" . $_GET['tel_sort'] . "\">\n";
}
if(isset($_GET['prod_sort'])) {
echo "<input type=hidden name=\"prod_sort\" value=\"" . $_GET['prod_sort'] . "\">\n";
}
?>
<input type=submit value="Oppdater"></form></td>
</tr>
<tr>
<td>
<form action="" method="get">
<?php
if(isset($_GET['ab_sort'])) {
echo "<input type=hidden name=\"ab_sort\" value=\"" . $_GET['ab_sort'] . "\">\n";
}
?>
<select name="prod_sort" style="width: 20em;" onchange="this.form.submit()">
<option value="">Show all</option>
<?php
while($row = mysqli_fetch_array($prodsort)) {
$id = $row['id'];
$prod = $row['prod'];
echo " <option value=\"$prod\">$prod</option>\n";
}
?>
</select>
<input type=submit value="Update"></form></td>
</tr>
</tbody>
</table>
</p>
<?php
if (is_array($ab_vis)) {
$count = count($ab_vis);
} else {
$count = 1;
}
$width = $count*130;
$width = $width+400;
echo "<table border='1' width=\"" . $width . " px\" id=\"mytable\">
<thead>
<tr>
<th><div title=\"$db_table_naming\">prod</th>
<th>model</th>\n";
if($count > 1) {
foreach($ab_vis as $ab) {
echo "<th>" . $ab . "</th>\n";
}
} else {
echo "<th>$ab_vis</th>\n";
}
echo "</tr>
</thead>\n";
$mndpr = "";
while($row = mysqli_fetch_array($result)) {
echo "<tbody>\n<tr class=\"mark\">";
$device = $row['id'];
$ab = ab($device);
$kost = $row['kost'];
$ts = ts($device);
$ukon = $row['ukon'];
if($ukon=="1") { $tdcolor = "#0099FF"; } else { $tdcolor = "#FFFFFF"; }
echo "<td bgcolor=\"" . $tdcolor . "\">" . $row['prod'] . "</td>\n";
echo "<td bgcolor=\"" . $tdcolor . "\"><div title=\"model: " . $row['model'] . " (id:" . $device . ")\">" . $row['model'] . "</a></div></td>\n";
foreach($abo_vis as $abs) {
$ab = "$abs";
$if_model_one = if_model_one($ab, $device);
if(isset($if_model_one[1])) {
$name = $if_model_one[0];
$mndpr = $if_model_one[1];
} else {
$pro = $if_model_one[0];
}
$price = $abo[$ab];
if($ts[$ab] == 1) {
$ts_sum = $ts["ts_sum"];
} else {
$ts_sum = 0;
}
$totpr = $price;
$color = color($price, $kost, $ukon, $ts_sum, $ab);
$kategori_sjekk = kategori_pris($ab);
if (is_numeric($kategori_sjekk)) {
$tellKat = count($kategori_sjekk);
} else {
$tellKat = 0;
}
if( $tellKat > 0 ) {
$deviceAboVis = "";
for ($abo_row = 0; $abo_row < $kat_count; $abo_row++) {
$abo_navn = $kategori_abo_sjekk[$abo_row][0];
$grupris = $kategori_abo_sjekk[$abo_row][1];
$mndpris = $kategori_abo_sjekk[$abo_row][2];
$totalpris = $mndpris*12+$color["pris"];
$mnduten = $grunnpris*12;
$devicepris = $totalpris-$mnduten;
if($color["pris"] == "n/a") {
$totalpris = 0;
$devicepris = 0;
} else {
$deviceAboVis .= "Abo: " . $abo_navn . "
";
$deviceAboVis .= "Mndpris: " . $mndpris . " (" . $grupris . ")
";
$deviceAboVis .= "Totalpris: " . $totalpris . "
";
$deviceAboVis .= "devicepris: " . $devicepris . "
";
}
}
echo "<td bgcolor=\"" . $color['color'] . "\"><div title=\"" . $ab . "
model: " . $row['model'] . "
Utpris: " . $color['pris'] . "
Kost: " . $kost . "
TS: " . $color['ts'] . "
DB: " . $color['db'] . "
DG: " . $color['dg'] . "
" . $deviceAboVis . "\">" . $color['pris'] . "</div></td>\n";
} else {
if (!isset($mnduten)) { $mnduten = $prov['grunnpris']*12; $devicepris = $totalpris - $mnduten; }
$abo_short = strtolower(str_replace(' ', '', $ab));
echo "<td bgcolor=\"" . $color['color'] . "\"><!-- t" . $color['tXt'] . " --><div title=\"" . $ab . "
model: " . $row['model'] . "
Utpris: " . $color['pris'] . "
Kost: " . $kost . "
Prov: " . $color['prov'] . "
IMEI: " . $color['imei'] . "
TS: " . $color['ts'] . "
DB: " . $color['db'] . "
DG: " . $color['dg'] . "
Månedspris: " . $prov['mndpris'] . "
Totalpris: " . $totalpris . "\">" . $color['pris'] . "</div></td>\n";
}
}
echo "</tr>\n</tbody>";
}
echo "</table>\n";
function kategori_sjekk($kat) {
global $conn;
global $pro;
$telque = mysqli_query($conn,"SELECT * FROM $pro WHERE abonnement = '$kat'");
$telres = mysqli_fetch_assoc($telque);
return $telres;
}
function if_model_one($ab, $device) {
$pro = pro($ab);
if(isset($navn)) {
$return = array($pro, $navn, $mndpris);
} else {
$return = array($pro);
}
return $return;
}
mysqli_close($conn);
And the MYSQL databases will be something like this:
CREATE TABLE `device_kost` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`prod` varchar(64) NOT NULL,
`model` text NOT NULL,
`kost` decimal(10,2) NOT NULL,
`ukon` int(10) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `device_kost` VALUES
(1,'Samsung','Galaxy',1361.00,0),
(2,'Apple','iPhone',2510.00,0);
CREATE TABLE `device_ts` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`device` int(10) DEFAULT NULL,
`nick` varchar(32) DEFAULT NULL,
`ts_sum` int(10) DEFAULT NULL,
`date_f` date NOT NULL,
`date_t` date NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `device_ts` VALUES
(1,1,'gal',200,'2019-01-01','2019-02-28'),
(2,2,'ip',200,'2019-01-01','2019-02-28');
The "device_ts" tables has 3 other similair tables. And I tried to inner join them together, with no luck. First I had to set similair columnd name "as".
SELECT *
FROM
(
select a.`id` , a.`prod` , a.`model` , a.`kost` , a.`ukon` ,
b.`device` as `device_ts` , b.`ts_sum` , b.`date_f` , b.`date_t` , b.`Ub` as `ubt_ts` ,
c.`device` , c.`Ub` as `ubt_ma`
from
device_kost a
inner join device_ts b on a.`id` = b.`device`
inner join device_max c on a.`id` = c.`device`
) as T
order by T.id Desc limit 10
The page/script works great. But it takes around 30-40 seconds because there is too many devices. I think if I could create a better inner join function and then sort out the info after I would remove a lot of unneccessary sql queries.
I have a mysql table which I'm trying to turn into jQuery listview widgets. The part that I'm having trouble with is how best to extract the info from the table without doing multiple queries on the db itself. I have the following code which works:
global $conn;
$sql = "SELECT `id` FROM `implantFamilies` WHERE `familySelector` = '" . $_POST['implantFamily'] . "'"; //need to sanitise this
$result = $conn->query($sql);
if ($result->num_rows == 1) {
while($row = $result->fetch_assoc()) {
$familyId = $row["id"];
}
} else {
die("Error: " . $result->num_rows . " family groups in result. Alert administrator.");
}
?>
<form class="ui-filterable">
<input id="filterBasic-input" data-type="search">
</form>
<?php
$sql = "SELECT DISTINCT `familySection` FROM `implants` WHERE `familyGroupId` = '" . $familyId . "'";
$sections = $conn->query($sql);
if ($sections->num_rows > 0) {
while ($sectionRow = $sections->fetch_assoc()) {
$output .= "<b>" . $sectionRow["familySection"] . "</b><br>";
//DISPLAY COLLAPSIBLE DIV HERE
$sql = "SELECT DISTINCT `sectionHeading` FROM `implants` WHERE `familyGroupId` = '" . $familyId . "' AND `familySection` = '" . $sectionRow["familySection"] . "'";
$dividers = $conn->query($sql);
if ($dividers->num_rows > 0) {
while ($headingRow = $dividers->fetch_assoc()) {
$output .= "<i>" . $headingRow["sectionHeading"] . "</i><br>";
//DISPLAY list-divisers DIV HERE
$sql = "SELECT `reference`, `description`, `filterText`, `requiresLot` FROM `implants` WHERE `familyGroupId` = '" . $familyId . "' AND `familySection` = '" . $sectionRow["familySection"] . "' AND `sectionHeading` = '" . $headingRow["sectionHeading"] . "'";
$implants = $conn->query($sql);
if ($implants->num_rows > 0) {
while ($implantRow = $implants->fetch_assoc()) {
$output .= $implantRow["description"] . "<br>";
//DISPLAY implants DIV HERE
}
} else {
$output = "0 results";
}
}
} else {
$output = "0 results";
}
}
} else {
$output = "0 results";
}
$conn->close();
echo $output;
On the first lot of (simple) data which I've put into the table, this code executes 23 mysql queries. There must be a simpler way!
I want to echo sql database records in my page and I am using this code. When I run it doesn't display the result from the db. I have records in the database that match the criteria. I am new in php and sql so please tell where I have mistakes.
session_start();
if (!isset($_SESSION['name'])) {
header('Location:vhod.php');
exit;
}
$pageTitle = 'СЪОБЩЕНИЯ';
include 'includes/header.html';
$email = $_SESSION['email'];
$name = $_SESSION['name'];
include 'php/db_connect.php';
$msgs = '';
$query = 'SELECT `timestamp`, `to`, `sender`, `subject`, `msg` FROM msg WHERE `to`="$name"';
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$msgs = "ДАТА: " . $row["timestamp"] . " >> От: " . $row["sender"] . " >> Тема: " . $row["subj"] . " >> Съобщение: " . $row["msg"] . "<br>";
}
} else {
$msgs = "Нямате съобщения :(";
}
try modifying the lower part of your code like this.
$msgs = '';
$query = "SELECT * FROM msg WHERE to=$name";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$msgs = "ДАТА: " . $row['timestamp'] . " >> От: " . $row['sender'] . " >> Тема: " . $row['subj'] . " >> Съобщение: " . $row['msg'] . "<br>";
}
} else {
$msgs = "Нямате съобщения :(";
}
I have this script where I am for first time returning records in to the table. It works quite fine but, it is returning only last added record. Can you look at it, because I cant find what is wrong.
$query = 'SELECT character_id, alias, real_name, alignment
FROM comic_character
ORDER BY ' . $order[$o];
$result = mysql_query($query, $db) or die (mysql_error($db));
if (mysql_num_rows($result) > 0) {
echo '<table>';
echo '<tr><th>Alias</th>';
echo '<th>Real Name</th>';
echo '<th>Alignment</th>';
echo '<th>Powers</th>';
echo '<th>Enemies</th></tr>';
$odd = true;
while ($row = mysql_fetch_array($result)) {
echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">';
$odd = !$odd;
echo '</td><td>' . $row['alias'] . '</td>';
echo '<td>' . $row['real_name'] . '</td>';
echo '<td>' . $row['alignment'] . '</td>';
$query2 = 'SELECT power FROM comic_power p
JOIN comic_character_power cp
ON p.power_id = cp.power_id
WHERE cp.character_id = ' . $row['character_id'] . '
ORDER BY power ASC';
$result2 = mysql_query($query2, $db) or die(mysql_error($db));
if (mysql_num_rows($result2) > 0) {
$powers = array();
while ($row2 = mysql_fetch_assoc($result2)) {
$powers[] = $row2['power'];
}
echo '<td>' . implode(',', $powers) . '</td>';
} else {
echo '<td>none</td>';
<?php
require_once("../../db_connect/db_connect.php");
$decoded = json_decode($_GET['json']);
$ias = $decoded->{'ias'};
$ian = $decoded->{'ian'};
$select = "select iacode,ianame from isdsmot_ia_creation";
$res = mysql_query($select);
while ($row = mysql_fetch_array($res)) {
$ias = $row['iacode'];
$ian = $row['ianame'];
$ia=$ias."|".$ian;
echo "<OPTION value = \"" . $row[0] . "|" . $row[1] . "\">" . $ia . "</OPTION>";
}
$result=$ia;
if ($result) {
echo "Successful" . mysql_error();
} else {
echo "Unsuccess" . mysql_error();
}
?>
Will this help you : add around your
<?php
require_once("../../db_connect/db_connect.php");
$decoded = json_decode($_GET['json']);
$ias = $decoded->{'ias'};
$ian = $decoded->{'ian'};
$select = "select iacode,ianame from isdsmot_ia_creation";
$res = mysql_query($select);
echo '<select name="myDropDown">';
while ($row = mysql_fetch_array($res)) {
$ias = $row['iacode'];
$ian = $row['ianame'];
$ia=$ias."|".$ian;
echo "<OPTION value = \"" . $row[0] . "|" . $row[1] . "\">" . $ia . "</OPTION>";
}
echo '</select>';
$result=$ia;
if ($result) {
echo "Successful" . mysql_error();
} else {
echo "Unsuccess" . mysql_error();
}
?>