PHP & SQL to get a certain row - php

Hello i am just starting out and not too bad with HTML but no hardly anything on PHP, I have created a basic PHP code to get data from my sql database but limit to the first 3 and echo them out.
However i would really like to be able to put these in fancy looking divs later on in a different order, so say row 1 in div 1 row 2 in div 2 etc.
I know how to select and echo line based on a unique id but i want to be able to say query by date order then limit to 3 and echo those in different divs on the website.
Ive looked everywhere so any help would be very good, here is my code:
$sql = mysql_query("SELECT * FROM dbc_posts ORDER BY ID ASC limit 3");
$unique_id = 'unique_id';
while ($rows = mysql_fetch_assoc($sql)){
echo $rows[type] . '<br/>';
}
?>

Related

Which is the query i should do so that i could fetch the previous from last row?

I want to read the last 3 rows of my table seperate and then place them in 3 different div's of a slider. The problem is that i cant use 'where id=xxx' because i insert rows dynamically every time that i make a post item.
if i use query('select * from news order by id desc limit 3') and then a loop while ($result->fetch_assoc()) then i have the last 3 rows.
My problem is that i want to place every row in a different div so that i will have 3 divs.
I suppose i must do 3 different queries for that but i dont know how.
I have this one right now.
$result = $myDb->query('select * from news order by id desc');
while ($nI = $result->fetch_assoc()) {
$title = $nI['title'];
$date = $nI['date'];
$author = $nI['author'];
$mainobjective = $nI['mainobjective'];
$contents = $nI['contents'];
$keywords = $nI['keywords'];
and then i have my html where with the use of echo i place every variable in the div i want.
It sounds like the problem you are describing is more with your PHP code, that you haven't posted, than your MySQL. Don't read them separately. Use a single query to get all 3 and then iterate through them separately with PHP.
You can use the query you already had:
$result = $myDb->query('SELECT * FROM news ORDER BY id DESC LIMIT 0,3');
Make sure you are placing the results in separate containers in PHP:
foreach($result as $row)
{
echo "<div>".$row."</div>";
}

Accessing records in descending order in database

retrieving the enteries :
<?php
$query1 = "SELECT * FROM table2 WHERE id='{$user}' order by srno desc " ;
$result1=mysql_query($query1,$con);
if (!($result1) )
{
die('Error: ' . mysql_error($con));
}
else
{
$values1= mysql_fetch_array($result1);
}
mysql_close($con);
?>
for descending order display I am doing this:
while($values1)
{
echo $row['srno'];
echo " " . $row['Symptoms'];
echo "<br>";
}
This makes all the records to be displayed on the same page in descending order. How do i make them display on different pages.Is there a way of accessing values of array values1 using index so that I can manipulate different records on different pages.
If you wanna display different results on different pages, you can always filter them in the query with limit and offset:
// In PAGE 1
$query1 = "SELECT * FROM table2 WHERE id='{$user}' order by srno desc LIMIT 10" ;
// In PAGE 2
$query2 = "SELECT * FROM table2 WHERE id='{$user}' order by srno desc LIMIT 10 OFFSET 10" ;
Note: This is called server side pagination.
Edit: It is clear you should never write that query more than once, automate the pagination process and make it add limit of offset to the query in the php script according to the parameters it gets.
Then, you would call the php script and pass it a parameter such as
myPHPScript.php?page=2
By the way, in your code, you are only displaying the first result.
You should do:
while ($row = mysql_fetch_array($result1, MYSQL_NUM)) {
}
You can't use a result set like that across pages. You'd have to select the single result you want to use on a page, then link the next/previous page with the next/previous result's ID in the link, then select that result on the next page.
To get the next and previous ids from your current result, have a look at this post.
You'll probably want to check that the next/previous results exist before outputting a link to them as well.

order by id not working with me

I have made " news & updates " simple script
my query is:
$query = mysql_query("SELECT * FROM a_commants WHERE postid='$postid' ORDER BY id DESC LIMIT 0,10");
it shows last comment
i want to make it show all comments or at least 10 comments
if i change it to:
$query = mysql_query("SELECT * FROM a_commants WHERE
postid='$postid'");
it shows first comment only
idk whats wrong :(
I think that the problem is in your php code, not MySQL.
The query seems fine, as long as you have more than one comment, but it seems that you are not iterating through results, just printing the first row you get from db.
This should show last 10 comments:
$res = mysql_query("SELECT * FROM a_commants WHERE postid='$postid' ORDER BY id DESC LIMIT 0,10");
while($row = mysql_fetch_array($res)){ // iterate through results
print_r($row); // print the row
}
And you should definitely switch to mysqli or PDO, and sanitize your inputs.
The mysql_* functions are deprecated and going to be removed from PHP.

show row only 100 times PHP

How can I make a limit of showing the results? I need to limit it for 100 views.
In DB I have:
ID|NAME|PAGE|COUNT|DATE
In count I want to count untill 100 and then stop showing that ID. I could do it with count < 100. And then update the specific ID. I could get records with less than 100 views, but I couldn't manage to update count on the specific ID.
Row is showed with:
php code:
foreach($bannerGroups[0] as $ban) {
echo '<li class="right1">'.$ban->html().'</li>';
}
But I just don't know where to put the update in there. I tried, but all I got was to update only one ID. But it shows 4 on one page and randomizes them on refresh. So I don't know what to do.
Also I would like to say I am only learning php. Sorry for all the mess.
Code at http://pastebin.com/A9hJTPLE
If I understand correctly, you want to show all banners that have been previously-displayed less than 100 times?
If that's right, you can just add that to your WHERE clause:
$bannerResult = mysql_query("SELECT * FROM table WHERE page='cat' WHERE `COUNT` < 100");
To update them all, you can either run a query while displaying each individual banner, or "record" the id of each and run a single query at the end, like:
$ids = array();
foreach($bannerGroups[0] as $ban) {
$ids[] = $ban['ID']; // record the ID; don't know how Banner
// class works, assuming uses indexes; maybe ID() method?
echo '<li class="right1">'.$ban->html().'</li>';
}
...
mysql_query('UPDATE table SET `COUNT` = `COUNT` + 1 WHERE ID IN (' . join(',', $ids) . ')');
UPDATE:
Based off of a comment, your Banner class doesn't have a method to retrieve the individual banner's ID. In this case, you can record the ID values when you're building your banners array:
$ids = array();
while($row=mysql_fetch_assoc($bannerResult)) {
$banners[] = new Banner($row);
$ids[] = $row['ID']; // record the ID
}
// update the `count` on each record:
mysql_query('UPDATE table SET `COUNT` = `COUNT` + 1 WHERE ID IN (' . join(',', $ids) . ')');
sorry, but I got your question wrong...
first you have to insert a new sql-column like "viewcount" to the db...
on every read, you have to increment the value in viewcount...
for that behaviour (because, mysql does not allow sub-selects on update-clause on the same table), you have to fetch the results from db, as you do that, and pass all the primary-keys of the records to an array...
after the view-logic you have to fire up a query like:
UPDATE foo SET viewcount = viewcount + 1 WHERE id IN (1,2,3,4,5,6...,100);
where the IN-clause can be easily generated using your primary-keys-array with "implode(',', $arr);"
hope this helps.
$bannerResult = mysql_query("SELECT * FROM table WHERE page='cat' AND `count`<100");
#newfurniturey figured it out. in each foreach($banneruGroups added: $ids = $ban->getValue('id'); and then mysql_query("UPDATE dataa SET COUNT = COUNT + 1 WHERE id = '$ids'"); but is there any way to update them by adding query only once? And if the id is showed already 100 times i get Warning: Invalid argument supplied for foreach() in. Any idea how to fix it? I have 4 ids in DB . If one of them already have 100 views (count) then i get error!
Try to limit your data source for 100 items.
It's like OFFSET x LIMIT 100 in MySQL/PostgreSQL query or TOP 100 in MSSQL.

PHP - Matching search terms mysql_query

im having issue with a php script that retrieves values from a database and see's which ones match an input term. I either get the resourceID #4 error or no response.
Im basically trying to retrieve similar entries and of those similar entries show the name and date of their submission
$input = mysql_real_escape_string($_POST["interest"]);
$query1 = "SELECT name,interest_desc,date, MATCH(interest_desc) AGAINST('$input') AS score
FROM interests
WHERE MATCH(interest_desc) AGAINST('$input')
ORDER BY score DESC
LIMIT 10
";
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo $row['name'];
echo $row['interest_desc'];
echo $row['date'];
}
I must be going wrong with my syntax, any pointers? im pulling my hair out over this!
Have you indexed the field as full text and is your table myisam. See this for more detail.

Categories