Having a query that draws multiple lines from a DB, each with an ID, a path and another ID. Is it possible to pass the path by the filesize() function and mark if two or more lines have the same file size?
Here's my code:
$q2 = "SELECT idMultimedia, pathURLMult, Type_idType FROM Multimedia";
$e2 = mysqli_query( $conn, $q2 );
while( $r2 = mysqli_fetch_array($e2) ){
list($width, $height) = getimagesize($r2["pathURLMult"]);
$size = filesize($r2["pathURLMult"]);
echo $r2["idMultimedia"] . " | " . $r2["pathURLMult"] . " | " . $width. " | " . $height. " | " . $size . "<br>";
}
Example of a possible Database output:
1 | randomguy1.jpg | 2
2 | A-GUY.jpg | 3
3 | lol.jpg | 2
4 | randomguy2.jpg | 1
randomguy1.jpg & randomguy2.jpg have the same file size... I need to show that for both pics! How can I do this?
Any help? Thanks in advance!
Related
How can I insert Multiple array value in mysql table at same time?
I can handle single value Insertion.
Eg:
$product = json_decode($_POST['product']); // (1,2)
$sub_product = json_decode($_POST['sub_product']); // (3,4)
$plan = json_decode($_POST['plan']);// (5,6)
$months = json_decode($_POST['months']); // (7,8)
I want to insert value with same index in mysql
Something like:
+----+---------+-------------+------+--------+
| id | product | sub_product | plan | months |
+----+---------+-------------+------+--------+
| 1 | 1 | 3 | 5 | 7 |
+----+---------+-------------+------+--------+
| 2 | 2 | 4 | 6 | 8 |
+----+---------+-------------+------+--------+
I've tried Insertion for single column and it works perfectly.
for ($i = 0; $i < count($product); $i++) {
$product = mysql_real_escape_string($product[$i]);
mysql_query("INSERT INTO i_product(product) VALUES('$product')");
}
you can execute multiple INSERT operations in the following format,
INSERT INTO tbl_name
(a,b,c)
VALUES
(d,e,f),
(g,h,i),
(j,k,l);
Here's the reference:
INSERT Syntax
Since you're getting the data as an array, your INSERT operation should be like this:
// your code
mysql_query("INSERT INTO i_product(product, sub_product, plan, months)
VALUES
('" . mysql_real_escape_string($product[0]) . "', '" .
mysql_real_escape_string($sub_product[0]) . "', '" .
mysql_real_escape_string($plan[0]) . "', '" .
mysql_real_escape_string($months[0]) . "'),
('" . mysql_real_escape_string($product[1]) . "', '" .
mysql_real_escape_string($sub_product[1]) . "', '" .
mysql_real_escape_string($plan[1]) . "', '" .
mysql_real_escape_string($months[1]) . "')");
Sidenote: Please don't use mysql_ database extensions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7. Use mysqli or PDO instead.
Im new to PHP MySQL. Im developing a songbook site.
I want to create tables alphabetically in separate pages for each alphabet.
When a user click an alphabet in the menu it will direct to this page site/publicsearch.php?browse=a
This is the database :
| ID | TITLE | ARTIST | CATEGORY | ALPHABET |
+----------------------------------------------------+
| 1 | Amazing love | XXXXXX | Love | a |
| 2 | Above all | XXXXXX | Worship | a |
| 3 | BXXXX | XXXXXX | Love | b |
| 4 | BXXXX | XXXXXX | Worship | b |
I pull the above database table like this : It works fine.
<?php
$servername = "localhost";
$username = "xxxxxxx";
$password = "xxxxxxxx";
$db_name = "xxxxxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $db_name);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
echo "Connected successfully";
exit();
}
// Attempt select query execution
$sql = "SELECT * FROM lyrics_a";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table'>";
echo "<tr>";
echo "<th>#</th>";
echo "<th>Title</th>";
echo "<th>artist</th>";
echo "<th>cateogry</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td> " . $row['eng_title'] . " " . $row['tel_title'] . " </td>";
echo "<td>" . $row['artist'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
I need separate alphabetical tables like this :
Alphabet "a" table results `site/publicsearch.php?browse=a`
| ID | TITLE | ARTIST | CATEGORY | ALPHABET |
+----------------------------------------------------+
| 1 | Amazing love | XXXXXX | Love | a |
| 2 | Above all | XXXXXX | Worship | a |
Alphabet "b" table results `site/publicsearch.php?browse=b`
| ID | TITLE | ARTIST | CATEGORY | ALPHABET |
+----------------------------------------------------+
| 3 | BXXXX | XXXXXX | Love | b |
| 4 | BXXXX | XXXXXX | Worship | b |
I tried the below code doesn't work.
<?php
$servername = "localhost";
$username = "xxxxxx";
$password = "xxxxxxx";
$db_name = "xxxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $db_name);
// Check connection
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$alphabet = $_GET['alphabet'];
$id = mysqli_real_escape_string($conn,$id);
$query = "SELECT * FROM `lyrics` WHERE `alphabet`='" . $alphabet . "'";
$result = mysqli_query($conn,$query);
while($row = mysqli_fetch_array($result)) {
echo "<br><br>";
echo $row['id'];
echo $row['title'];
echo $row['lyrics'];
echo $row['alphabet'];
}
?>
I'm newbie. Please help. Thank you.
$alphabet = $_GET['alphabet'];
$id = mysqli_real_escape_string($conn,$id);
$query = "SELECT * FROM `lyrics` WHERE `alphabet`='" . $alphabet . "'";
replace with:
$alphabet = mysqli_real_escape_string($conn,$_GET['browse']);
$query = "SELECT * FROM `lyrics` WHERE `alphabet`= '" . $alphabet . "' ";
You propose the following URL: publicsearch.php?browse=a
But in your script you are using the request parameter $_GET['alphabet']
In addition, you don't need a separate column alphabet in your database, you can use SQL's LIKE:
$letter = $_GET['browse'];
$sql = "SELECT * FROM `lyrics` WHERE title LIKE '{$letter}%'"
I'm making a table with translation:
English | Italian | French
help | ayuto | amo
and am creating a functionality to add new language (Russian). I have it all set up but the import doesn't work correctly. It imports the values under already existing fields. So it doesn't add them directly under the newly added language. Looks like this:
English | Italian | French | Russian(added)
help | ayuto | amo |
| | | помощь(imported)
| | | извините(imported)
I need that to start it from the top like this:
English | Italian | French | Russian(added)
help | ayuto | amo | помощь(imported)
| | | извините(imported)
Here is the code:
if (isset($_POST['submit'])){
$lang_name = htmlspecialchars($_POST['lang_name'], ENT_QUOTES);
$str = strtolower($lang_name);
$lang_lawname = str_replace(' ', '_',$str);
$sql = "INSERT INTO translation_lang (languages) VALUES ('".$lang_name."')";
$sql2 = "ALTER TABLE translation ADD $lang_lawname VARCHAR( 255 ) DEFAULT ''";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
if (!mysqli_query($con,$sql2)) {
die('Error: ' . mysqli_error($con));
}
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
//echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
//echo "<h2>Displaying contents:</h2>";
readfile($_FILES['filename']['tmp_name']);
//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
do {
if ($data[0]) {
$import="INSERT into translation($lang_lawname) values('$data[0]')";
mysqli_query($con,$import) or die(mysqli_error());
}
}
while ($data = fgetcsv($handle,1000,",","'"));
}
I bought an iPhone wallpaper gallery PHP script a year ago, and it has been running great. I've been slowly learning PHP and MySQL by doing little tweaks here and there.
To view a category, the URL is www.example.com/index.php?catid=27, and I want to change it to www.example.com/index.php?catid=apple for SEO reasons. I know I can futher tidy up the URL with .htaccess but I just want to get this word slug to begin with.
This is the code (inside index.php) that I think generates the category pages:
if (($_REQUEST['catid']) && ($_REQUEST['catid'] > 1)) {
$catid = $_REQUEST['catid'];
if (is_numeric($catid)) {
$tempitemarray = $myitems->getItemsByCategory($catid);
$catnav = "&catid=" . $catid;
$tempcat = new Category();
$tempcat->getCategory($catid);
$maintitle = "<h1>" . $tempcat->getCategoryName() . " " . $itemplural . "</h1>";
}
That code calls another PHP page called itemList.php, and inside that page I found this code:
public function getItemsByCategory($catid) {
$this->retrieveSQL = "select * from items i, item_category_lookup l where livedate < '" . $this->currentdate . "' and i.active = " . $this->showApproved . " and l.categoryid = $catid and i.id = l.itemid order by i.livedate desc limit " . $this->startRow . "," . $this->numRows;
$this->countSQL = "select * from items i, item_category_lookup l where livedate < '" . $this->currentdate . "' and i.active = " . $this->showApproved . " and l.categoryid = $catid and i.id = l.itemid";
$this->retrieveItems();
return $this->items;
}
This is the 'items' table:
id | itemname | itemdec | itemkeywords | createdate | viewcount | active | rating | livedate | sourcedesc | sourceurl
-----------------------------------
64 | Apple Logo
This is the 'item_category_lookup' table:
categoryid | itemid
-------------------------
27 | 64
This is the 'category' table below. It's not called in the query above but does have the actual category names. How would I implement it?
id | categoryid
------------------
27 | Apple
You might change the SQL of $this->retrieveSQL = ... into
if (!is_number($catid)) {
$this->retrieveSQL = "select * from items i, item_category_lookup l, category j where livedate < '" . $this->currentdate . "' and i.active = " . $this->showApproved . " and i.id = l.itemid and j.id = l.categroyid and j.categoryid = "$catid" order by i.livedate desc limit " . $this->startRow . "," . $this->numRows;
}
Try this:
URL example: www.example.com/index.php?catid=27-apple
if ( isset( $_REQUEST['catid'] ) ) {
$catSEO = explode( '-', $_REQUEST['catid'] );
}
$catid = is_numeric( $catSEO[0] ) ? $catSEO:0;
Basic question!
I have 2 tables
PRODUCE
+-----+--------------+
| id | fruit_name |
+--------------------+
| 1 | Apple |
| 2 | Banana |
| 3 | Carrot |
+-----+--------------+
VARIETIES
+-----+---------------+----------------+
| id | fk_fruit_id | variety_name |
+-----+---------------+----------------+
| 1 | 1 | Cox |
| 2 | 1 | Braeburn |
| 3 | 2 | Chester |
| 4 | 3 | Kotaka |
| 5 | 3 | Imperial |
| 6 | 3 | Oneal |
+-----+---------------+----------------+
I'd like to output a list of varieties per fruit e.g.
APPLE - Cox, Braeburn
BANANA - Chester
CARROT - Kotaka, Imperial, Oneal
My current code is
$query = "SELECT * FROM produce, varieties WHERE produce.id = varieties.fk_fruit_id";
$result = mysql_query($query) or die('Error : ' . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$produce_fruit_code = $row['fruit_code'];
$variety_name = $row['variety_name'];
echo $produce_fruit_code.' - '.$variety_name.'<br/>';
}
which outputs:
Apple - Cox
Apple - Braeburn
Banana - Chester
Carrot - Kotaka
Carrot - Imperial
Carrot - Oneal
Not a million miles away, but still not there. Any help is much appreciated, thanks!
You probably could get one chunky sql statement to do that for you but I'd opt for data juggling with arrays.
For example (not tested and excuse the formatting):
$query = "SELECT * FROM produce, varieties WHERE produce.id = varieties.fk_fruit_id";
$result = mysql_query($query) or die('Error : ' . mysql_error());
$res=array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$produce_fruit_code = $row['fruit_code'];
$variety_name = $row['variety_name'];
if(isset($res[$produce_fruit_code])){
$res[$produce_fruit_code]+=','.$variety_name;
}else{
$res[$produce_fruit_code]=$variety_name;
}
}
print_r($res);
If you are using MySQL you can use the group_concat extension on grouping. Something along the lines of:
SELECT
f.fruitname as fruit,
GROUP_CONCAT(distinct v.varietyname separator ',') as variety
FROM fruit f JOIN varieties v ON produce.id = varieties.fk_fruit_id;
or similar. Sorry my sql is a little rusty quite now.
For more look at this article
http://www.mysqlperformanceblog.com/2006/09/04/group_concat-useful-group-by-extension/
and of course here:
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
If you dont work with MySQL and your database doesn't support group_concat think about buffering those results. In large databases and with many simultaneous users your application can considerably slow down when having to download all data and store it in locally every time.
This won't get you all the way, but it will get you most of what you want. There are some edge cases that are problematic.
$query = "SELECT * FROM produce, varieties WHERE produce.id = varieties.fk_fruit_id";
$result = mysql_query($query) or die('Error : ' . mysql_error());
$produce_fruit_code = "";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($produce_fruit_code != $row['fruit_code'])
{
$produce_fruit_code = $row['fruit_code'];
echo "<br/>".$produce_fruit_code." - ". $row['variety_name'];
} else {
echo ", ".$row['variety_name'];
}
}
$query = "SELECT * FROM produce, varieties WHERE produce.id = varieties.fk_fruit_id";
echo "<dl>";
$result = mysql_query($query) or die('Error : ' . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if($row['fruit_name'] != $current_fruit_name) {
$current_fruit_name = $row['fruit_name'];
echo "<dt>$current_fruit_name</dt>";
}
echo "<dd>" . $row['variety_name'] . "</dd>";
}
echo "";
If you want some CSS that will make a definition list look like the Name - X,Y,Z like in the question, let me know.
You can query this directly
SELECT
f.fruitname as fruit,
GROUP_CONCAT(distinct v.varietyname separator ',') as variety
FROM fruit f JOIN varieties v ON produce.id = varieties.fk_fruit_id;
GROUP BY produce.id
Sort everything into arrays in your while loop. This should work:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$produce_fruit_code = $row['fruit_code'];
$variety_name = $row['variety_name'];
if ($produce_fruit_code == "Apple") {
$apple_array[] = $variety_name;
}
if ($produce_fruit_code == "Banana") {
$banana_array[] = $variety_name;
}
if ($produce_fruit_code == "Carrot") {
$carrot_array[] = $variety_name;
}
}
echo "Apples:" . implode(", ", $apple_array) . "<br/>";
echo "Bananas:" . implode(", ", $bananas_array) . "<br/>";
echo "Carrots:" . implode(", ", $carrots_array) . "<br/>";