AutoUpdate the last five entrys of a table in a MySQL database - php

On this page you see at the bottom a "real time" updates of a twitter search with a very simple but elegant animation, i want to do the same but showing the last five entrys on my MySQL data base, is out there a good way to do this?, maybe a combination of php and jquery?
Thanks for any help!

Yes, it's pretty straightforward.
Server side:
Create a PHP page which selects the latest 5 rows from your database and outputs them as JSON.
//connect to db
$result = mysql_query("SELECT * FROM table ORDER BY some_timestamp_column DESC LIMIT 5");
$data = array();
while ($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}
echo json_encode($data);
Client side:
Every X seconds, make an AJAX request to your PHP script to get the 5 newest rows. Compare them to the ones you've already displayed and add the newer ones to the top of the 'update area'. You can use jQuery to do this.

Related

MySQL update value +1 (increment) on button click

I know this question has been answered over a million times, but I'm very new to the whole programming scene and I just can't get it to work. Sorry, I hope you can help me!
HTML:
<div class="the_score">+76</div>
<div id="increment_value">Like</div>
I have a MySQL connection and I can fetch the data, but I want the score to update with +1 when someone clicks the Like div. I have a table "websites" with row "WebScore" that has the function int(10) (?? I hope that is correct).
I use this to fetch the data and show it on my website:
$r = rand(2,3);
$sql = "SELECT * FROM websites WHERE ID = $r";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
I presume you have a column, not a row, called WebScore.
You need the query
UPDATE websites SET WebScore = WebScore + 1 WHERE ID = $r
As you can see, it finds the right row of your table and updates the value of the WebScore column.
This sort of things can only be done from a PHP program. It can't be done directly from a Javascript method invoked within a web browser. So, if you want to react to a user click, you'll need to post a form or invoke an Ajax style call to a php endpoint. How to do that is the province of another question.
Keep plugging away; you'll figure out this database stuff.

Show an advert every five MYSQL search results

I am currently using PHP and MYSQL to show some results from my database, this all works fine, but I want to show an advert every five search results.
At the moment the results are just pulled from the database using the
while($row = mysql_fetch_array($result))
But i am not sure of the best way to count every five results, i did consider doing this using an id, but because the results are filtered they would not necessarily be in sequence and so i was hoping somebody could give me a nudge in the right direction.
$i=0;
while($row = mysql_fetch_array($result)){
$i++;
// show your mysql data here
if($i%5==0){
// show your ad here
}
}
TIP: depending on how big your SQL result is and how exactly you are echoing it, you might probably save some mem/time by using mysql_fetch_assoc() instead of mysql_fetch_array(), but YMMV

How to allocate one ID to another and show this in HTML

For my iPhone web app I have created a database in php MyAdmin, it contains two tables (bookings, waiters and allocations) the I wish to view the bookings within a table in HTML where i can allocate a waiter to that booking. Any tips on how this would be done, any help will be appreciated as this is my first contact with web app development and MyAdmin. Thanks!
Mysql work with autoincrement id. You will have an id after of an insertion. You can to create a table only to id's and after to use it in the insertion of the waiter.
First you will need to get the data from the table via MySQL
$sql = "SELECT * FROM table_name";
$result = mysql_query($sql);
Then you would loop through the result
while($row = mysql_fetch_assoc($result)){
extract($row);
}
Then you can do as you wish with the returned data...
You really need to give us a little more... What have you got so far?

Optimizing My Random Row Fetch

I have been reading about speed issues in relation to getting a random row via MYSQL and PHP, and wondered how my code might be improved for speed.
I have banner information in my database in a table called "banners" and i want to display a random banner in relation to the position on the page, and add +1 to the view_count for that banner. My method works, but for a busy site where this happens on each page load, can this be improved for speed? Thanks
/* Get banners for position 1 then choose a random one to display */
$banners = mysql_query("SELECT id,title,banner_url,destination FROM ".TBL_BANNERS." WHERE position = '1' AND status = '1'");
$banner_count = mysql_num_rows($banners) - 1;
$rand_offset = mt_rand(0,$banner_count);
$result = mysql_query("SELECT id,title,banner_url,destination FROM ".TBL_BANNERS." LIMIT $rand_offset, 1 ");
$banner_id = mysql_result($result,0,"id");
$banner_title = mysql_result($result,0,"title");
$banner_url = mysql_result($result,0,"banner_url");
$banner_dest = mysql_result($result,0,"destination");
/* Add view to this banner */
$database->addViewToBanner($banner_id);
The last function uses the query:
"UPDATE banners SET view_count = view_count+1 WHERE id = '$banner_id'"
I also need to say, that there probably wont be any more than 100 records in the "banners" table at any one time but there will be holes in the ID. The IDs might go up to say 200 but only half of those will still exist.
Generate a random number in php and drill it that way into the PK
The SQL would be
SELECT id,title,banner_url,destination
FROM TBL_BANNERS
WHERE id = $rand_offset
If you miss, run it again. This gives a very efficient seek to one row which will be better than using limit/offset processing
The ideal way to do it, as you can read in lots of places around the internet, e.g. Anton Titov's blog is to do 2 queries:
SELECT COUNT(*) AS banners FROM quotes
Then generate a random number in your programming language (i.e. PHP use mt_rand). and feed it into this Query:
SELECT * FROM banners LIMIT $generated_number, 1
NOTE: this isn't good to use if your table only has a small number of rows. Generally I still use ORDER BY RAND() up until I know there are going to be more than 100 rows in the table.
Instead of the first SELECT, use this:
$banners_count_result = mysql_query("SELECT COUNT(*) AS num_banners FROM ".TBL_BANNERS." WHERE position = '1' AND status = '1'");
$banner_count = mysql_result($banners_count_result, 0, "num_banners");
Unless you add and remove new banners every couple of seconds, consider caching this result somewhere.

Jquery - finding range between two unique id's in mysql

Another question which has me perplexed:
I have a table which enables users to enter as many rows as they like based on their userid and unique id (auto incremental).
I need to be able to get this information from mysql and place the previously entered information into the fields on the web application (they may need to be edited before confirming that they're correct).
I store the total number of records for that user so far in one variable, and the total number of records for all users in another variable.
The question is: how do I get the range of ids for the records the user has already enterered.
Example: User 1 has 2 records in the database and there is 7 in total (5 by another user). How would I get the unique IDs of the 2 records that already exist?
Thanks for any suggestions you may have!
I'm not entirely sure what you mean, so this may or may not be helpful.
This SQL should give you the record ids:
SELECT id FROM tableofuserrows WHERE userid = [User Id]
You can then fetch this from the database with PHP, e.g.
$q = mysql_query('SELECT id FROM tableofuserrows WHERE userid = ' . (int) $_GET['userid']) or die(mysql_error());
$result = array();
while ($row = mysql_fetch_assoc($q)) {
$result[] = $row['id'];
}
mysql_free_result($q);
echo json_encode($result);
So if you wanted to fetch these IDs from the browser using jQuery:
$.getJSON("http://url", { userid: 3 }, //set userid properly
function(data){
$.each(data, function(i,id){
//do something with the recordid
alert(id);
});
}
);
Do you have to do this dynamically using jquery or can you load the fields in the web form with the rest of the page using php ?
Either way, you're going to need to query the database table for all rows where userid = a certain user. Once you get these results, you'll need to create a page you can call and get results from using jquery if you're going that route.
Someone just posted what I'm saying with code examples :-)
I decided to use MIN(id) in the select statement, counting how many rows there are and then populating the form fields accordingly, starting with the min value and adding the counted rows. Works well ;)

Categories