Select specific content from mysql database with php - php

I need to echo text from a specific ID in my table..
If i use
<?php
database_connect();
$navquery = "SELECT * from content
WHERE id = 1
ORDER by position;";
$navresult = mysql_query($navquery);
?>
it will echo the text in ID 1..
but when i try
<?php
database_connect();
$navquery = "SELECT * from content
WHERE id = 2
ORDER by position;";
$navresult = mysql_query($navquery);
?>
it wont echo the text in ID 2..
help me please?

You do not have a row on your content table with an id of 2. You can try the query below to get another row.
SELECT *
FROM content
WHERE ID<>1
LIMIT 1

Related

PHP mysql query loop to get lowest value

I need to create loop, get lowest ID value from mysql.
So I tried this script:
<?php
include "configuration.php"; // mysql konfiguration
$jungiam = mysql_connect("$db_host", "$db_user", "$db_pass");
mysql_select_db($db_name, $jungiam);
$darom = mysql_query("SELECT * from task where id = (
SELECT
MIN(id)
FROM
task)");
$rez = mysql_num_rows($darom);
while ($rez > 1) {
$row = mysql_fetch_array($darom);
echo $komanda = $row['komanda'];
mysql_query('DELETE * FROM task WHERE komanda = ' .$row['komanda'].'');
}
return true;
?>
I need to get lowest id and print to page $row['komanda'], after printing delete from that table where komanda = $row['komanda'] (printed text).
After deleting record, I need to do script from the start, so it will print text with lowest id from mysql and after that text will be deleted and proccess will start from start and will be repeating until all records in table 'task' will be deleted.
"SELECT * from task ORDER BY id ASC LIMIT 1;"
This will return the first element with the lowest ID.

SELECT duplicate columns in MYSQL database PHP

I have a database table titled products.
I have 2 columns in the table titled name and image
I have 4 entries in my database
item 1 | image1.jpg
item 2 | image1.jpg
item 3 | image1.jpg
item 4 | image2.jpg
I need to display the results on a page but I only need to display image.1jpg once, my while() loop is obviously displaying all 4 images.
I've been at this for hours, please help!
My code:
$query = mysql_query("SELECT * FROM products WHERE page='Birthdays' ORDER BY id ASC");
while($fetch = mysql_fetch_assoc($query)){
$image = $fetch['image'];
$name = $fetch['name'];
echo ''.$name.'';
echo '<img src="'.$thumbnail_image.'"/>';
}
What you need is using GROUP BY in your query. So change the query as
SELECT * FROM products WHERE page='Birthdays' GROUP BY image ORDER BY id ASC
Try the code below;
$query = mysql_query("SELECT * FROM products WHERE page='Birthdays' GROUP BY image ORDER BY id ASC");
while($fetch = mysql_fetch_assoc($query)){
$image = $fetch['image'];
$name = $fetch['name'];
echo ''.$name.'';
echo '<img src="'.$thumbnail_image.'"/>';
}
You can also use a trick like not using while statement at all. If you just write the
$fetch = mysql_fetch_assoc($query) without while you would just get the first result in the query. I hope this helps.

i want to count specific IDs from my tables

i have this code
<?php
$votes_query=mysql_query("select * from votes where CandidateID='$id'");
$vote_count=mysql_num_rows($votes_query);
echo $vote_count;
?>
which fetches individual *ID*s from my votes table.
I need a code which can help fetch the sum of specific *ID*s.
Lets say, I have ID-205 and ID-209 in my table.
<?php
$ids = array('205','209');
$votes_query=mysql_query("select * from votes where CandidateID IN($ids) ");
$vote_count=mysql_num_rows($votes_query);
echo $vote_count;
?>
Put all ID's in an array & Use IN Clause of mysql.
<?php
$ids = array('205','209');
$sql "SELECT SUM(field_score) AS vote_score FROM votes WHERE CandidateID IN ($ids)";
$vote_score = mysql_query($sql);
echo $vote_score;
?>

Preventing dynamic PHP table display correctly

I have the following code to display a block of six products from a mysql database.
It displays as two columns three rows and I get individual photos and correct page links in each of the six positions but the alt tags for the three products in column one are repeated in column 2. I cannot work out why. Any thoughts, and ways to improve the code?
<?php
// create query
$query = "SELECT * FROM photogear WHERE qty != 0 ORDER BY id DESC LIMIT 6";
// execute query
$result = mysql_query($query) or die(MYSQL_ERROR);
?>
<table>
<?php
while($row = mysql_fetch_array($result)){
$product2=$row['product'];
$img2=$row['img'];
$manuid2=$row['manuid'];
$id2=$row['id'];
$price=$row['price'];
//GET MANUFACTURER FOR DISPLAY IN img title
$manu_q = "SELECT * FROM manufacturers WHERE manuid = '$manuid2' ORDER BY name";
$manu_r = mysql_query($manu_q) or die(mysql_error());
$manu_info = mysql_fetch_array($manu_r);
$name2=$manu_info['name'];
?>
<tr>
<td>
<?php // for each product show photo with a link to product page
echo "<a href='product-".$row['id']."'><img src='".$row['img']."'alt='$name2,$product2 $price' title='$name2 $product2 £$price' width='85'></a>";
?>
<?php $row=mysql_fetch_assoc($result); // make one record out.?>
</td>
<td>
<?php // for each product show photo with a link to product page
echo "<a href='product-".$row['id']."'><img src='".$row['img']."' alt='$name2, $product2 $price' title='$name2 $product2 £$price' width='85'></a>";?>
</td>
</tr>
<?php
} // End loops.
?>
</table>
Any help much appreciated
Instead of using $product2, $price try using $row['product'],$row['price'] in alt
Just try this but im not sure
Use this
SELECT * FROM manufacturers WHERE manuid = '$row['manuid']' ORDER BY name
Instead of this
SELECT * FROM manufacturers WHERE manuid = '$manuid2' ORDER BY name
After this
$row=mysql_fetch_assoc($result);
Add this line
$manu_info = mysql_fetch_assoc($manu_r);

SELECT * FROM table_name ORDER BY column_name?

ok so i coded in a news section and everytime i insert new news,its shows below the old one.
i want to make it ORDER BY id but make it start like backwards.i dont know how to explain but yeh
i want them to be ordered by the newest added by the id so if the 1st row that was inserted's id is 1 then i want it to show below the next id.
so row with id = 2 will be here
so row with id = 1 will be here
thats how i want it to be, instead of it being like this
so row with id = 1 will be here
so row with id = 2 will be here
.
Sorry for my bad explanation i hope this is understandable
heres my code so far
<?php
require("include/config.php");
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id");
while($row = mysql_fetch_array($sqlnews)) {
$dbdate = $row['date'];
$dbnews = $row['news'];
echo "<h1><strong>$dbdate</strong></h1>";
echo "<div class='content'>$dbnews</div><br><br>";
}
?>
add DESC in your ORDER BY clause
SELECT * FROM news ORDER BY id DESC
by default, it is in ASC mode.
SELECT * FROM news ORDER BY id DESC
DESC is the descending keyword ASC is ascending
If you specify neither then default behaviour is ascending
Just use DESC keyword in your sql query.
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id DESC");
However, it isn't really such a good idea to use id, because semantically, there is nothing preventing somebody from changing the sequence which counts up automatically assigning the ID.
Therefore, you should add a column created_at. Everytime you insert a row, you can use the SQL function NOW().
The advantage is that you can say:
SELECT * FROM news WHERE created_at <= NOW() ORDER BY created_at DESC
This means that you can schedule news items ahead of time, and it will automatically display when the date/time arrives!
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id DESC");
try this:
just have to add
order by id DESC
Just replace your code by this code:
<?php
require("include/config.php");
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id DESC");
while($row = mysql_fetch_array($sqlnews)) {
$dbdate = $row['date'];
$dbnews = $row['news'];
echo "<h1><strong>$dbdate</strong></h1>";
echo "<div class='content'>$dbnews</div><br><br>";
}
?>

Categories