I have been trying to get this to work for some time. I'm just stumped. Here is the original MYSQL and then the PHP Ive been working on.
MYSQL:
SET #COD_PAIS = 3;
SET #F_HASTACORTE = '2012-03-31 01:00:00';
SELECT mcarteras.DES_CARTERA_CC AS 'Short Name of Fund'
, mcarterasflias.DES_CARTERAFLIA AS 'I/C'
, msociedades.DES_SOCIEDAD_CORTO AS 'Fund Manager Company'
, mcarteras_clases.DES_CARTERACLASE AS 'Class'
, mcarteras_clasesesp.DES_CARTERACLASE_ESP AS 'Special Class'
, dr_rentmovil_carteras.POR_RENTCARTERA AS 'TTR year-to-date %'
, dficha_mes.POR_REMUNERA_COBRADA AS 'Mgmt Fee Effectively Charged'
, dficha_mes.POR_GASTOS_TOTALESC AS 'Total Expenses %'
, dficha_mes.VR_CARTERA_FCORTE AS 'Fund Size'
, dr_rentmovil_carteras.F_HASTACORTE AS 'Date'
FROM mcarteras
INNER
JOIN mcarterasflias
ON mcarterasflias.ID_CARTERAFLIA = mcarteras.ID_CARTERAFLIA
INNER
JOIN msociedades
ON msociedades.ID_SOCIEDAD = mcarteras.ID_SOCIEDADADM
INNER
JOIN mcarteras_clases
ON mcarteras_clases.ID_CARTERACLASE = mcarteras.ID_CARTERACLASE
INNER
JOIN mcarteras_clasesesp
ON mcarteras_clasesesp.ID_CARTERACLASE_ESP = mcarteras.ID_CARTERACLASE_ESP
INNER
JOIN dr_rentmovil_carteras
ON dr_rentmovil_carteras.ID_CARTERA = mcarteras.ID_CARTERA
AND dr_rentmovil_carteras.COD_PAIS = #COD_PAIS
AND dr_rentmovil_carteras.F_HASTACORTE =#F_HASTACORTE
AND dr_rentmovil_carteras.ID_FORMATO = 1
AND dr_rentmovil_carteras. ID_COLUMNA = 5
INNER
JOIN dficha_mes
ON dficha_mes.ID_CARTERA = mcarteras.ID_CARTERA
AND dficha_mes.COD_PAIS = #COD_PAIS
AND dficha_mes.F_CORTE = #F_HASTACORTE
WHERE mcarteras.COD_PAIS = #COD_PAIS
AND mcarteras.ID_CARTERATIPO = 4
AND mcarteras.ID_CARTERAFLIA IN ( 3,4 )
AND mcarteras.IND_PUBLICACION = 1
AND mcarteras.COD_ESTADO= 1
PHP:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("bdcarterascv2", $con);
$COD_PAIS = '3';
$F_HASTACORTE = '2012-03-31 01:00:00';
$result = mysql_query("SELECT DES_CARTERA_CC AS 'Short Name of Fund'
, mcarterasflias.DES_CARTERAFLIA AS 'I/C'
, msociedades.DES_SOCIEDAD_CORTO AS 'Fund Manager Company'
, mcarteras_clases.DES_CARTERACLASE AS 'Class'
, mcarteras_clasesesp.DES_CARTERACLASE_ESP AS 'Special Class'
, dr_rentmovil_carteras.POR_RENTCARTERA AS 'TTR year-to-date %'
, dficha_mes.POR_REMUNERA_COBRADA AS 'Mgmt Fee Effectively Charged'
, dficha_mes.POR_GASTOS_TOTALESC AS 'Total Expenses %'
, dficha_mes.VR_CARTERA_FCORTE AS 'Fund Size'
,dr_rentmovil_carteras.F_HASTACORTE AS'Date'
FROM mcarteras
INNER
JOIN mcarterasflias
ON mcarterasflias.ID_CARTERAFLIA = mcarteras.ID_CARTERAFLIA
INNER
JOIN msociedades
ON msociedades.ID_SOCIEDAD = mcarteras.ID_SOCIEDADADM
INNER
JOIN mcarteras_clases
ON mcarteras_clases.ID_CARTERACLASE = mcarteras.ID_CARTERACLASE
INNER
JOIN mcarteras_clasesesp
ON mcarteras_clasesesp.ID_CARTERACLASE_ESP = mcarteras.ID_CARTERACLASE_ESP
INNER
JOIN dr_rentmovil_carteras
ON dr_rentmovil_carteras.ID_CARTERA = mcarteras.ID_CARTERA
AND dr_rentmovil_carteras.COD_PAIS = $COD_PAIS
AND dr_rentmovil_carteras.F_HASTACORTE = $F_HASTACORTE
AND dr_rentmovil_carteras.ID_FORMATO = 1
AND dr_rentmovil_carteras. ID_COLUMNA = 5
INNER
JOIN dficha_mes
ON dficha_mes.ID_CARTERA = mcarteras.ID_CARTERA
AND dficha_mes.COD_PAIS = $COD_PAIS
AND dficha_mes.F_CORTE = $F_HASTACORTE
WHERE mcarteras.COD_PAIS = $COD_PAIS
AND mcarteras.ID_CARTERATIPO = 4
AND mcarteras.ID_CARTERAFLIA IN ( 3,4 )
AND mcarteras.IND_PUBLICACION = 1
AND mcarteras.COD_ESTADO= 1
")
or die(mysql_error());
// HTML ... Aliases from Mysql
echo "<table border='1'>
<tr>
<th>Short Name of Fund</th>
<th>I/C</th>
<th>Fund Manager Company</th>
<th>Class</th>
<th>Special Class</th>
<th>TTR year-to-date %</th>
<th>Mgmt Fee Effectively Charged</th>
<th>Total Expenses %</th>
<th>Fund Size</th>
<th>Date</th>
</tr>";
//<tr> specifies table row. for each <td> (table data) will specify a new column. The $row specifies the mysql column name (in this case using an alias)
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Short Name of Fund'] . "</td>";
echo "<td>" . $row['I/C'] . "</td>";
echo "<td>" . $row['Fund Manager Company'] . "</td>";
echo "<td>" . $row['Class'] . "</td>";
echo "<td>" . $row['Special Class'] . "</td>";
echo "<td>" . $row['TTR year-to-date %'] . "</td>";
echo "<td>" . $row['Mgmt Fee Effectively Charged'] . "</td>";
echo "<td>" . $row['Total Expenses %'] . "</td>";
echo "<td>" . $row['Fund Size'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Here is the current error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '01:00:00 AND dr_rentmovil_carteras.ID_FORMATO = 1 AND dr_rentmovil_carte' at line 28
Any help would be greatly appreciated.
You need to wrap the datetime string in quotes:
='$F_HASTACORTE'
Otherwise it is evaluating 2012-03-31 01:00:00 as a number, not a datetime.
you probably want to put quotes around your '$VARIABLES'. And escape them if you didn't.
Try:
AND dr_rentmovil_carteras.F_HASTACORTE = '$F_HASTACORTE'
If your variables aren't integers, you should quote them - there are a few other places where you should do that, too.
You should also look at using PDO instead of the mysql_* functions, too - if you use bound parameters, you can sidestep a lot of this sort of problem very neatly.
Related
How do i replace column name 'product_name' to 'TOTAL' and other column name 'inv_date' to 'TOTAL'. Tried with as below but to no avail.
$query = "SELECT IFNULL(pm.product_name,'TOTAL') product_name,IFNULL(pt.inv_date,'TOTAL') inv_date, SUM(pt.qty) as total_qty,(SUM(pt.amount)/SUM(pt.qty)) as avg_rate, SUM(pt.amount) as total_amount1,SUM(pt.tcs) as total_tcs, SUM(pt.total_amount) as total_amount_net,
am.acc_name,pgm.product_group,psgm.sub_product_group,pim.pre_insp_name, ptm.purch_type_name, pt.* FROM purch_tble pt LEFT JOIN product_master pm ON pm.product_id = pt.product_id LEFT JOIN acc_master am ON am.acc_id = pt.acc_id LEFT JOIN purch_type_master
ptm ON ptm.purch_type_id = pt.purch_type_id LEFT JOIN prod_group_master pgm ON pgm.prod_group_id = pm.prod_group_id LEFT JOIN prod_sub_group_master psgm ON psgm.prod_sub_group_id = pm.prod_sub_group_id LEFT JOIN pre_insp_master pim ON pim.pre_insp_id
= pt.pre_insp_id $where GROUP BY pt.product_id, DATE_FORMAT(pt.inv_date,'%Y-%m') WITH ROLLUP"; $retval = mysql_query($query); $num_row = mysql_num_rows($retval); echo mysql_error(); if($num_row >= 1){ while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{ echo "
<tr>"; echo "
<td>" . date('m-Y',strtotime($row['inv_date']))."</td>"; echo "
<td>" . $row['product_name']."</td>"; echo "
<td>" . round($row['total_qty'])."</td>"; echo "
<td>" . round($row['avg_rate'],2)."</td>"; echo "
<td>" . round($row['total_tcs'])."</td>"; echo "
<td>" . round($row['total_amount1'])."</td>"; echo "
<td>" . round($row['total_amount_net'])."</td>"; echo "</tr>"; } // while loop finishes } // if statement finishes
I also tried with following but to no avail.
COALESCE(pm.product_name,'TOTAL') product_name,
COALESCE(pt.inv_date,'TOTAL') inv_date,
LASTLY, I also tried with following but to no avail.
IF(GROUPING(pm.product_name),'TOTAL',pm.product_name) product_name,
IF(GROUPING(pt.inv_date),'TOTAL',pt.inv_date) inv_date,
The picture is self explanatory.
I am stuck don't where am I going wrong.
Hope I have been able to express question clearly.
I have 3 tables i want to display the 3 table data in single table based on primary key, foreign key the result came perfectly! But i need to calculate rank based on the total marks from my second table.
result screenshot:
Please anyone tell me the query to calculate rank
I used the following mysql query
if(isset($_POST['submit']))
{
$result = mysqli_query($con,"
SELECT s.student_name
, s.contact_number
, m.total
, m.rank
, p.father_name
FROM student_details s
JOIN mark m
ON s.student_id = m.student_id
JOIN parents_details p
ON p.student_id = s.student_id
WHERE s.student_name = '".$_POST['student_name']."'
");
echo "<table border='1' align='center' cellpadding='15' bgcolor='#FFFFFF'>
<tr>
<th>NAME</th>
<th>CONTACT NUMBER</th>
<th>TOTAL MARK</th>
<th>RANK</th>
<th>FATHER NAME</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['student_name'] . "</td>";
echo "<td>" . $row['contact_number'] . "</td>";
echo "<td>" . $row['total'] . "</td>";
echo "<td>" . $row['rank'] . "</td>";
echo "<td>" . $row['father_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
}?>
SELECT * FROM
(
SELECT #rank := #rank+1 finalrank,ZZ.* FROM
(
SELECT student_details.student_name,
student_details.contact_number, mark.total,
mark.rank, parents_details.father_name
FROM student_details
INNER JOIN mark ON student_details.student_id=mark.student_id
INNER JOIN parents_details ON parents_details.student_id=student_details.student_id ,(SELECT #rank:=0)z
ORDER BY mark.total desc
)ZZ
)ZZZ
WHERE ZZZ.student_name = '".$_POST['student_name']."'
Just try above query.
Here I had used SELECT #rank:=0 and #rank := #rank+1.
I made some changes in the code and now it's working. But there is a new problem.
In the database, I have registered 3 Company Cods: G-1001, G-1002 and G-1004.
If I type "g", "g-", or for instance "g-100" and click search, it returns me all the results correctly. But if I type just "g-1001" or even "01", "001", it returns no results.
Why it's not working when I search the last characters of the registered code?
<?php
if(isset($_POST['action']) && $_POST['action'] == 'send'){
$codsearch = preg_replace('#[^a-z 0-9?()!-]#i', '', $_POST['searchcompanycod']);
$getresultsquery = mysql_query('SELECT p.*, a.idProduct, a.idVehic, a.year, v.nameVehic, GROUP_CONCAT(a.year SEPARATOR "<br>"), GROUP_CONCAT(a.idVehic SEPARATOR "<br>"), GROUP_CONCAT(v.nameVehic SEPARATOR "<br>")
FROM products p
INNER JOIN application a ON p.idProduct = a.idProduct
INNER JOIN vehic v ON a.idVehic = v.idVehic
WHERE codCompany LIKE "%'.$codsearch.'%"
GROUP BY p.codCompany') or die(mysql_error());
$resultsrow = mysql_num_rows($getresultsquery);
if($resultsrow > 1){
echo "$codsearch";
echo "<table class='table table-bordered' border=1>";
echo "<tr>";
echo "<th>Company Code</th><th>Original Code</th><th>Descr.</th><th>idProduct</th><th>idVehic</th><th>Vehic Name</th><th>Year</th>";
echo "</tr>";
while($getresultsline = mysql_fetch_array($getresultsquery)) {
echo "<tr>";
echo "<td>" . $getresultsline['codCompany'] . "</td>";
echo "<td>" . $getresultsline['codOriginal'] . "</td>";
echo "<td>" . $getresultsline['typeDesc'] . "</td>";
echo "<td>" . $getresultsline['idProduct'] . "</td>";
echo "<td>" . $getresultsline['GROUP_CONCAT(a.idVehic SEPARATOR "<br>")'] . "</td>";
echo "<td>" . $getresultsline['GROUP_CONCAT(v.nameVehic SEPARATOR "<br>")'] . "</td>";
echo "<td>" . $getresultsline['GROUP_CONCAT(a.year SEPARATOR "<br>")'] . "</td>";
echo "</tr>";
}
} else{
echo "No results";}
}
?>
</tbody>
</table>
Any ideas?
My testing page is: http://flyingmail.com.br/test/produtos.php (only Company Code filter working)
The highlighting on your SQL should show you the main issue.
Change
'SELECT table1.*, table2.productID, table2.veicleID, table2.date, table3.veicleNAME, GROUP_CONCAT(table2.date SEPARATOR "<br>"), GROUP_CONCAT(table3.veicleNAME SEPARATOR "<br>")
FROM table1
INNER JOIN table2 ON table1.productID = table2.productID
INNER JOIN table3 ON table2.veicleID = table3.veicleID
GROUP BY productID
HAVING productID LIKE '%".$codsearch."%''
to
"SELECT table1.*, table2.productID, table2.veicleID, table2.date, table3.veicleNAME, GROUP_CONCAT(table2.date SEPARATOR "<br>"), GROUP_CONCAT(table3.veicleNAME SEPARATOR "<br>")
FROM table1
INNER JOIN table2 ON table1.productID = table2.productID
INNER JOIN table3 ON table2.veicleID = table3.veicleID
GROUP BY productID
HAVING productID LIKE '%".$codsearch."%'"
(note that I changed the first and last single quote to a double quote)
Found it. Hahaha terrible... it could never be if($resultsrow > 1){ because if I search for something specific, it will return only 1 row. So, it must be if($resultsrow >= 1){
LOL
I have two tables.
visitors_details, with id,scanner_id,time columns
and visitors_info with scanner_id, name,surname columns
I want to get back
id,name,surname,time in a table
i have written this but is not working
$result = mysql_query("SELECT visitors_details.id AS id,
visitors_info.name AS name, visitors_info.surname AS surname, visitors_details.time
AS time FROM visitors_details AS d LEFT JOIN visitors_info AS i ON
d.scanner_id=i.scanner_id ");
echo "<table border='1'>
<tr>
<th>id</th>
<th>name</th>
<th>surname</th>
<th>Time</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "</tr>";
}
echo "</table>";
any ideas??
Its better to enable some debugging for your code like this:
<?php
error_reporting(E_ALL);
$sql = "
SELECT d.id AS id, i.name AS name, i.surname AS surname, d.time AS time
FROM visitors_details AS d
LEFT JOIN visitors_info AS i ON d.scanner_id=i.scanner_id
";
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
?>
try this query
$result = mysql_query("SELECT d.id , i.name , i.surname , d.time
FROM visitors_details AS d LEFT JOIN visitors_info AS i
ON d.scanner_id=i.scanner_id ");
Add this to catch errors. saves a lot of time:
if(!$result) {
echo mysql_error();
}
I've got two tables from which I need to extract information, but the data from the second table depends on the information I get from the first one. Is there an easy way to handle this?
<?php
mysql_connect('localhost', 'root', 'root') or die(mysql_error());
mysql_select_db('stadium') or die(mysql_error());
$result = mysql_query("SELECT * FROM events");
$result2 = mysql_query("SELECT name FROM competitions WHERE id='$row[competition_id]' ");
while($row = mysql_fetch_array($result)) {
echo "<tr id=\"" . $row['id'] . "\"> \n<td>" . $row['name'] . "</td>";
echo "<td>" . $row['competition_id'] . "</td>";
echo "<td>" . $row['date'] . "</td></tr>";
}
?>
Use a JOIN.
SELECT e.*, c.name as competition_name FROM events e LEFT JOIN competitions c on c.id = e.competition_id