this PHP-line:
$xx_val = (string) $xx;
generate the following output:
string(16) "XYZ1"
string(11) "XYZ2"
string(12) "XZY1"
string(19) "XZY2"
string(34) "8997451"
when I insert it to my db:
$xx_val = (string) $xx;
$sql = "INSERT INTO attributes (article) VALUES ($xx_val)";
if ($conn->query($sql) === TRUE) {
echo "\n\n### DONE ###\n\n";}
else {
echo "\n\n### ERROR ###" . $sql . "\n" . $conn->error . "\n\n";}
+-----+-----------+------------+--------+----------+-----------+-------------+
| id | article | attr1 | attr2 | attr3 | attr4 | attr5 |
+-----+-----------+------------+--------+----------+-----------+-------------+
| 001 | XYZ1 | NULL | NULL | NULL | NULL | NULL |
| 002 | XYZ2 | NULL | NULL | NULL | NULL | NULL |
| 003 | XZY1 | NULL | NULL | NULL | NULL | NULL |
| 004 | XZY2 | NULL | NULL | NULL | NULL | NULL |
| 005 | 8997451 | NULL | NULL | NULL | NULL | NULL |
+-----+-----------+------------+--------+----------+-----------+-------------+
but I would like to have all the attributes in one row:
+-----+-----------+------------+--------+----------+-----------+-------------+
| id | article | attr1 | attr2 | attr3 | attr4 | attr5 |
+-----+-----------+------------+--------+----------+-----------+-------------+
| 001 | 1 | XYZ1 | XYZ2 | XZY1 | XZY2 | 8997451 |
+-----+-----------+------------+--------+----------+-----------+-------------+
I'm pretty sure that my "insert into" is wrong, but I don't know how to fix it.
You have to update your SQL query, it should looks like:
$sql = "
INSERT INTO attributes (article , attr1, attr2, attr3, attr4 , attr5)
VALUES ('$article', '$attr1', '$attr2', '$attr3', '$attr4', '$attr5')
";
$result = $conn->query($sql);
and now you have to have variables $article, $attr1, $attr2, $attr3, $attr4, $attr5 which contains appropriate values.
And when you doing $xx_val = (string) $xx; please don't forget about validate and escape actual value, with purpose to prevent SQL-Injection.
You're forgetting the qoutes around the string values like so:
INSERT INTO attributes (article, attr1, attr2, attr3, attr4, attr5)
VALUES ('$xx_val', '$xx_val', '$xx_val', '$xx_val', '$xx_val', '$xx_val')
Related
I tried to calculate using SQL and found out that the result was different with the excel one,
My table setting :
+--------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------------+------+-----+---------+----------------+
| qty_final | int(11) | NO | | NULL | |
| price_final_usd | decimal(20,2) | NO | | NULL | |
+--------------------+---------------+------+-----+---------+----------------+
My table content:
+-----------+-----------------+
| qty_final | price_final_usd |
+-----------+-----------------+
| 1 | 53.22 |
| 2 | 125.05 |
| 3 | 23.15 |
| 4 | 114.79 |
| 4 | 395.06 |
+-----------+-----------------+
my sql query :
UPDATE $table SET amount_final = round((qty_final * price_final_usd),2)
my php code :
$amount_final = round($qty_final * $price_final_usd, 2);
my excel formula :
=ROUND(qty_final * price_final_usd,2)
Here's the result comparison
Can someone tell me how to correct the PHP code ?
I want exactly the same result between them
I have two tables.
Table 1 contains fields :
| Ensemble_ID | varchar(50) | NO | PRI | | |
| Target | text | YES | | NULL | |
| Gene_Length | int(5) | YES | | NULL | |
| miRNA | varchar(50) | NO | PRI | | |
| position | int(4) | YES | | NULL | |
| Prediction | text | YES | | NULL | |
and my table 2 contains fields :
|Ensemble_ID | varchar(50) | NO | PRI | | |
| miRNA | varchar(50) | NO | PRI | | |
| miRNA_Length | int(2) | YES | | NULL | |
| mfe | decimal(2,0) | YES | | NULL | |
| pvalue | decimal(4,0) | YES | | NULL | |
| no_of_seeds | int(1) | YES | | NULL | |
I need a result like
|Ensemble_ID |Gene Length|miRNA|miRNA Length|mfe|P-value|Position|Prediction|No of Seeds|
I am newbie in mysql . Can anyone help me in writing a query out of it.
Help appreciated.
Here is my php attachment .. I could not obtain result since its showing query error
<?php
$a = $_REQUEST["miRNA"];
$b = $_REQUEST["target"];
// $result = db::table("`table`") -> pluck("*") -> where("miRNA",$a) -> select() -> get();
$mysqli = new mysqli("localhost", "root", "password", "mysql");
$a = $mysqli -> escape_string($a);
$b = $mysqli -> escape_string($b);
$a = $mysqli->query("SELECT * FROM bio3 WHERE miRNA = '$a' AND Target LIKE '%$b' INNER JOIN bio4 on bio3.ensemble_id = bio4.ensemble_id ORDER BY bio4.pvalue ASC;");
// $result = $a -> fetch_assoc();
$i = 0;
while ($row = $a -> fetch_assoc()) {
$result[$i] = $row;
$i++;
}
$mysqli->close();
for($a=0;$a<sizeof($result);$a++){
print '<tr>
<td>'.htmlentities($result[$a]["Target"]).'</td>
<td>'.htmlentities($result[$a]["Gene Length"]).'</td>
<td>'.htmlentities($result[$a]["miRNA"]).'</td>
<td>'.htmlentities($result[$a]["miRNA Length"]).'</td>
<td>'.htmlentities($result[$a]["mfe"]).'</td>
<td>'.htmlentities($result[$a]["pvalue"]).'</td>
<td>'.htmlentities($result[$a]["position"]).'</td>
<td>'.htmlentities($result[$a]["Prediction"]).'</td>
<td>'.htmlentities($result[$a]["No of Seeds"]).'</td>
</tr>';
}
?>
Join two tables like:
SELECT t1.Ensemble_ID, t1.Gene_Length, t1.miRNA, t2.miRNA_Length, t2.mfe, t2.pvalue, t1.position, t1.Prediction, t2.no_of_seeds
FROM table1 t1 INNER JOIN table2 t2
ON t1.Ensemble_ID = t2.Ensemble_ID
WHERE t1.miRNA = 'abc'
AND t1.target LIKE '%xyz'
ORDER BY t2.pvalue ASC;
Use Join to get your desired result. See below:
SELECT
A.Ensemble_ID,
Gene_Length,
A.miRNA,
miRNA_Length,
mfe,
Pvalue,
Position,
Prediction,
No_of_Seeds,
FROM Table1 A
JOIN
Table2 A ON A.Ensemble_ID=B.Ensemble_ID
You can have your query like this...
SELECT
A.Ensemble_ID,
Gene_Length,
A.miRNA,
B.miRNA_Length,
B.mfe,
B.Pvalue,
Position,
Prediction,
No_of_Seeds,
FROM Table1 A
JOIN
Table2 B ON A.miRNA=B.miRNA
I have a MySql table and I want to list things by type and insert headers. What type of query would I use?
From This:
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Whistler | Gwen | bird | f | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
| Dalli | Alli | canine | m | 2001-12-20 | NULL |
| Tara | David | canine | f | 2002-05-17 | NULL |
| Mimi | Alli | guinea pig | m | 2004-05-17 | NULL |
To this:
<h2>Cat</h2>
<ul>
<li>Fluffy</li>
<li>Claws</li>
</ul>
<h2>Dog</h2>
<li>Buffy</li>
<li>Fang</li>
<li>Bowser</li>
</ul>
etc.
Don't try to do all of this with SQL (just do a standard select query), use PHP to group the result, then present it. The best way to do this would be to have an object that will do the grouping for you, as you'll probably need it more than once. For example, your class could look something like this:
<?php
class Arrays
{
public static function group($array,$key)
{
if(NULL == $array)
return NULL;
$grouped = NULL;
foreach($array as $item)
{
$grouped[$item[$key]][] = $item;
}
return $grouped;
}
}
?>
And your use case could be something like:
<?php
$result = ...; // The result of your database query.
$grouped_by_type = Arrays::group($result,"type");
foreach($grouped_by_type as $type => $group)
{
echo "<h2>".ucwords($type)."</h2>";
echo "<ul>";
foreach($group as $animal)
{
echo "<li>".$animal['first_name']."</li>"; // Assumes the query brought back first_name...
}
echo "</ul>";
}
?>
Your first query would be:
SELECT DISTINCT type FROM table ORDER BY type ASC
With your PHP, I am sure you can get a result from this query and loop through it. Next one is to put another query inside the loop that gives you the list of animals that belong to current type:
SELECT name FROM table WHERE type='$type' ORDER BY name ASC
$type is just variable holding current type. I assumed column and table names so please change it to suit your code.
I have php-script running on top of apache. Every time when user goes to specific URL, he/she will get csv-file.
Column names are fetched like this (thanks to Daniel Figueroa :)
$csv_output .= "\n";
// get the column name from the first DB (ins.data)
mysql_select_db($db, $link) or die("Can not connect to DB1.");
$result = mysql_query("SHOW COLUMNS FROM ".$table." WHERE Field NOT IN
('ID','Key','Text')");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field']."; ";
$i++;
}
}
// get the column names from the second DB (Cu.data)
mysql_select_db($db2, $link) or die("Can not connect to DB2.");
$result = mysql_query("SHOW COLUMNS FROM ".$table2." ");
;
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field']."; ";
$i++;
}
}
$csv_output .= "\n";
Actual query on PHP-script goes like this:
$values = mysql_query(" SELECT ins.data.`Date`, ins.data.`Number`,
ins.data.`Email`, ins.data.`TargetId`, ins.data.`CSW`,
ins.data.`TSW`, ins.data.`CType`,
Cu.data.`Cus`, Cu.data.`Co`,Cu.data.`Ci`,
Cu.data.`SID`, Cu.data.`SType`
FROM ins.data
LEFT JOIN Cu.data ON (ins.data.TargetId = Cu.data.TargetID)
ORDER BY ins.data.ID DESC");
Output of 'desc':
mysql> desc ins.data;
+-------------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------------------+----------------+
| ID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| Date | timestamp | NO | | 0000-00-00 00:00:00 | |
| Number | text | NO | | NULL | |
| Text | text | NO | | NULL | |
| Email | text | NO | | NULL | |
| TargetId | varchar(20) | NO | | NULL | |
| CSW | text | NO | | NULL | |
| TSW | text | NO | | NULL | |
| Key | text | NO | | NULL | |
| CType | text | NO | | NULL | |
+-------------------+------------------+------+-----+---------------------+----------------+
10 rows in set (0.00 sec)
mysql> desc Cu.data;
+----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| Title | decimal(15,0) | NO | | NULL | |
| Cu | text | NO | | NULL | |
| Co | text | NO | | NULL | |
| Ci | text | NO | | NULL | |
| SID | text | NO | | NULL | |
| TargetID | varchar(20) | NO | MUL | NULL | |
| SType | text | NO | | NULL | |
| empty1 | int(11) | NO | | NULL | |
| empty2 | int(11) | NO | | NULL | |
| empty3 | int(11) | NO | | NULL | |
| empty4 | int(11) | NO | | NULL | |
| empty5 | int(11) | NO | | NULL | |
| empty6 | int(11) | NO | | NULL | |
| empty7 | int(11) | NO | | NULL | |
+----------+---------------+------+-----+---------+-------+
12 rows in set (0.00 sec)
UPDATE 3:
This is no more NATURAL LEFT JOIN-issue. Replaced with LEFT JOIN.
Added fields empty1-5 to ins.data to get data to csv-file. Without fields empty1-5, only data from first db (ins.data) was on csv.file.
Now i have data on all fields but field (or column names on excel) names on csv are on wrong order and not wanted fields (columns) are visible like Title and empty1-5.
Any ideas how to fix this? Some other way to get Field names to csv-file without "SHOW COLUMNS"?
I could write with 'echo' in the beginning of csv-file values what i want. ie
"Date; Number; Email; TargetID, CSW; TSW; CType; Cu; SID; Co; Ci; SType;" but i am so newbie with PHP that i don't know how :(
Another issue is that if field ID is first column on excel, excel cannot handle that and it must be excluded from SHOW COLUMNS output.
UPDATE4: Added more empty-fields to DB2 (Cu.data) and reordered SQL-query, now all values are visible and on right order.
EDIT
First of all, your table naming schema is weird and unusual... but assuming I understand it correctly then this query should work (if it does not then rename your tables without the dots (periods) to make things less confusing:
mysql_query('SELECT ins.data.Date, ins.data.Number, ins.data.Email, ins.data.TID, ins.data.CSW, ins.data.TSW, ins.data.CType, CU.data.SID, cu.data.SType, cu.data.CU, cu.data.CO, cu.data.Ci, FROM ins.data, cu.data ORDER BY ins.data.ID DESC');
According to to the mysql_query function reference, data should not end with a semicolon when using mysql_query in PHP... i never put the semicolon in there so I don't ever have a problem, that's what I initially noticed with your script (as I said i've never tried it so I don't know if thats the issue). Should be:
$values = mysql_query("SELECT Date, Number, Email, TID, CSW, TSW, CType, SID, SType, Cu, Co, Ci FROM ins.data NATURAL LEFT JOIN Cu.data ORDER BY ID DESC");
Also, when doing JOINS, usually you specify what column belongs to what table... like in the standard mysql example here:
<?php
// Make a MySQL Connection
// Construct our join query
$query = "SELECT family.Position, food.Meal ".
"FROM family LEFT JOIN food ".
"ON family.Position = food.Position";
$result = mysql_query($query) or die(mysql_error());
// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
echo $row['Position']. " - ". $row['Meal'];
echo "<br />";
}
?>
I don't always use the JOIN commands either... you can use an alternative syntax like so:
mysql_query('SELECT ... FROM t1, t2, t3 WHERE t1.b = t2.b AND t2.c = t3.c AND t1.a = t3.a');
Try putting aliases on table names. It will make the query more readable, too:
SELECT
i.`Date`, i.Number, i.Email, i.TID, i.CSW, i.TSW, i.CType,
c.Cu, c.Co, c.Ci, c.SID, c.TID, c.SType
FROM
ins.data AS i
LEFT JOIN
Cu.data AS c
ON i.TID = c.TID
ORDER BY
i.ID DESC ;
My MySQL query returns different results depending on how the query is submitted. When the query is submitted through the MySQL Console results in.
mysql> SELECT `modx`.coverage_nation.id,
-> `modx`.coverage_nation.name,
-> `modx`.coverage_national_region.id,
-> `modx`.coverage_national_region.name
-> FROM `modx`.coverage_nation_part
-> RIGHT JOIN `modx`.coverage_national_region ON (`modx`.coverage_nati
on_part.nation_regionID = `modx`.coverage_national_region.id)
-> RIGHT JOIN `modx`.coverage_nation ON (`modx`.coverage_nation_part.n
ationID = `modx`.coverage_nation.id)
-> ORDER BY `modx`.coverage_nation.name ASC, `modx`.coverage_national_region
.name ASC;
+----+---------------+------+------+
| id | name | id | name |
+----+---------------+------+------+
| 3 | Canada | NULL | NULL |
| 18 | Chad | NULL | NULL |
| 17 | Germany | NULL | NULL |
| 15 | Italy | NULL | NULL |
| 2 | Mexico | NULL | NULL |
| 19 | Nigeria | NULL | NULL |
| 14 | Russia | NULL | NULL |
| 16 | Spain | NULL | NULL |
| 1 | United States | NULL | NULL |
+----+---------------+------+------+
9 rows in set (0.00 sec)
When the same query is submitted using PHP's mysql_query it returns only one row.
$query .= "SELECT `modx`.coverage_nation.id,
`modx`.coverage_nation.name,
`modx`.coverage_national_region.id,
`modx`.coverage_national_region.name
FROM `modx`.coverage_nation_part
RIGHT JOIN `modx`.coverage_national_region ON (`modx`.coverage_nation_part.nation_regionID = `modx`.coverage_national_region.id)
RIGHT JOIN `modx`.coverage_nation ON (`modx`.coverage_nation_part.nationID = `modx`.coverage_nation.id)
ORDER BY `modx`.coverage_nation.name ASC, `modx`.coverage_national_region.name ASC;";
$resultSet = mysql_query($query) or die("query failed ".mysql_error());
while($row = mysql_fetch_array($resultSet,MYSQL_NUM)) {
// handle each result here
}
Returns only Canada. Does anyone have any ideas as to how I might solve this?
You have more than one field with the names id and name. Try changing the SELECT portion of your SQL (in the PHP code) to:
SELECT `modx`.coverage_nation.id,
`modx`.coverage_nation.name,
`modx`.coverage_national_region.id AS coverage_national_region_id,
`modx`.coverage_national_region.name AS coverage_national_region_name
Or something similar so returned fields have distinct names.
There was an error in my code somewhere. I sat down and rewrote it and it now works.