This code was working fine yesterday , and suddenly started showing error today..
The die('Error') gives out error when displaying the users with profile pictures
Thanks in advance for the help ... i am learning php so will convert to pdo later
<?php
include("dbconfig.php");
// Create connection
$conn=mysql_connect($db_host,$username,$password) or die('Error1');;
mysql_select_db("motorklq_glmindb",$conn) or die('Error2');
$result = mysql_query("SELECT adno,fname,lname,profilepic,gscore FROM gtable WHERE contest!='on' ORDER BY gscore DESC LIMIT 3") or die('Error');;
echo "<table><tr><th></th><th>gScore Leader</th><th></th><th></th></tr>";
echo "<tr><th>Firstname</th><th>Lastname</th><th>Image</th><th>Glamour</th></tr>";
while($row = mysql_fetch_array($result)){
print "<tr><td>".$row['fname']."</td><td>".$row['lname']."</td><td>".'<img src="data:image/jpeg;base64,' . base64_encode( $row['profilepic'] ) . '" width="32" height="32">'."</td><td>".$row['gscore']."</td></tr>";
}
print "</table>";
mysql_close($conn);
?>
Link to the db https://www.dropbox.com/s/dv8vhla0fxhw2p7/db.png
SELECT adno,
Change to
SELECT idno,
I think you mis-typed adno for idno in your select query.
Hope that helps
delete extra ; from query
$conn=mysql_connect($db_host,$username,$password) or die('Error1');;
$result = mysql_query("SELECT adno,fname,lname,profilepic,gscore FROM gtable WHERE contest!='on' ORDER BY gscore DESC LIMIT 3") or die('Error');;
to
$conn=mysql_connect($db_host,$username,$password) or die('Error1');
$result = mysql_query("SELECT idno,fname,lname,profilepic,gscore FROM gtable WHERE contest!='on' ORDER BY gscore DESC LIMIT 3") or die('Error');
Related
This is my PHP file:
<?php
$sql = "SELECT * FROM `general_chat` ORDER BY `general_chat`.`id` DESC limit 5";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
echo "<p>";
echo "<span class = 'usr'>".$row['user_name']."</span>";
echo "<br>";
echo $row['message'];
echo "<br>";
if($row['Date'] === date("Y-m-d")){
echo "Today ";
}
else{
echo $row['Date'];
}
echo $row['Time'];
echo "</p>";
}
}
else{
echo "No messages were found!";
}
?>
This is how my database table looks:
An image of the database table
And this is how it displays the data in the PHP file:
An image of the PHP file output
It displays the five latest rows at the top, is it possible to flip and make it display it at the first bottom and then display the others on top of it?
I tried using DESC instead of ASC but then it displays the five oldest rows instead of the latest
I am not sure this is a perfect method
but this could solve your problem
select * from (SELECT * FROM `general_chat` ORDER BY `general_chat`.`id` DESC limit 5) G ORDER BY G.id ASC;
This IS wrapping your SQL with a similar one
so in subquery it will fetch all latest 5 items and in main query it will sort by ASC
Try :
$sql = "SELECT * FROM (SELECT * FROM `general_chat` ORDER BY `general_chat`.`id` DESC limit 5) as G ORDER BY `G`.`id` asc";
I have the following code, that outputs all data from the comment field, however i wish for it to output only two and for them to be random, how can i got about doing this? many thanks.
$query = mysql_query("SELECT * FROM comments ");
//query the database
echo "<b>Reviews</b> ";
WHILE($rows = mysql_fetch_array($query)):
$comment_by = $rows['comment_by'];
$comment= $rows['comment'];
echo "<div style ='font:14px/21px Arial,tahoma,sans-serif;color:#cf5c3f </h>'>$comment_by</h>";
echo "<div style ='font:14px/21px Arial,tahoma,sans-serif;color:#ff0000 <h></h>'>$comment<br><br>";
endwhile;
?>
Use:
SELECT * FROM comments
ORDER BY RAND()
LIMIT 2
LIMIT 2: for two records
and
ORDER BY RAND(): for random comments
See official MySQL documentation:
ORDER BY: https://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html
RAND(): https://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html
LIMIT: https://dev.mysql.com/doc/refman/5.0/en/select.html
From dev.mysql.com
$query = mysql_query("SELECT * FROM comments ORDER BY RAND() LIMIT 2");
my while loop skip the element. looked for similar questions but still cannot understand. enlighten me please. tnx!
$query = "SELECT userid, COUNT(content) as x_count
FROM x GROUP BY userid ORDER BY x_count DESC
LIMIT 5";
$result = mysql_query($query) or die("Error in query:".mysql_error());
$row = mysql_fetch_assoc($result);
echo '<br>';
while(list($id,$no_x) = mysql_fetch_array($result)){
echo $id.'number of x:'.$no_x;
echo '<br>';
}
The problem is you're executing $row = mysql_fetch_assoc($result);, this will advance the result set.
I cannot see why you're calling this, so my suggestion is to just remove this line.
ive got this script that echos the information from a mysql database.
now it all works fine but i only want to echo the last 4 statements inserted
this is the script
<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("blog") or die(mysql_error());
$result = mysql_query("SELECT * FROM blog")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Age</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['username'];
echo "</td></tr>";
}
echo "</table>";
?>
Modify your mysql query (i assume that each entry as an ascending id):
SELECT * FROM blog
ORDER BY id DESC
LIMIT 4;
But this will get them in reverse order. If you want them in correct order you can do:
SELECT *
FROM (
SELECT * FROM blog
ORDER BY id DESC
LIMIT 4
) last_four
ORDER BY id ASC;
If you have an auto-incremented column as an id for each blog post, you could sort on the id in descending order. Use a query like this
mysql_query("SELECT * FROM blog ORDER BY your_id_column DESC LIMIT 4")
Assuming your blog table has a column called created, which contains the post's creation date, this query should do what you need:
SELECT * FROM blog ORDER BY created DESC LIMIT 4
This will order your posts by the most recently created post (time-wise), and will only return the first 4 rows fetched from the table.
You would do this with the query, changing the query to something like:
select * from blog order by blog_id desc limit 4
you'll have to change blog_id to whatever you use.
use this query which will order the records by ID in descending order and fetch only last 4 by using limit.
SELECT * FROM blog order by ID DESC LIMIT 4
You can restrict your SELECT using LIMIT and ORDER BY, but in order to help, we'd need to know a bit more about the database structure. Can you provide the results of "DESCRIBE blog;"?
If your "id" field is set up with auto_increment, then it's a good bet that higher numbers of that field indicate more recent posts. Showing output in reverse order ("ORDER BY id DESC") and restricting the number ("LIMIT 4") should be fairly straightforward.
SELECT * FROM blog ORDER BY id DESC LIMIT 4;
Check out the precise structure of the SELECT command at the MySQL documentation.
<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("blog") or die(mysql_error());
$result = mysql_query("SELECT * FROM `blog` ORDER BY `id` DESC LIMIT 4") or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Age</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['username'];
echo "</td></tr>";
}
echo "</table>";
?>
Just copy and paste the code. I believe the code will works good. Thanks
I generated the below query for php using PHP-MYADMIN,
My question is how to print the 30 rows it generates? when i use "see quote" it just errors out.
i am trying to echo the rows with search term and count in < div >< /div > tags each in its own.
Facebook 38 searches
Another Feed 25 searches
Timeline 18 searches
and so on to row 30.
$result=mysql_query($sql)
$sql = 'SELECT COUNT(*) AS `Rows`, `search`, SUM(searched) FROM `af_timeline_search` GROUP BY `search` ORDER BY SUM(searched) DESC LIMIT 0, 30 ';
/* top searches */
$sqlthis = mysql_query('SELECT COUNT(*) AS `Rows`, `search` FROM `af_timeline_search` GROUP BY `search` ORDER BY `Rows` DESC');
$num=mysql_num_rows($sqlthis);
$arrS = mysql_fetch_array($sqlthis);
$i=0;
while ($i < $num){
echo 'Search '.$arrS[$i].'';
$i++;
}
Screen Shot of Query in PHPmyadmin.
I think the best you can do is to take a look at this: mysql-fetch-array
There you'll find the answer on how to loop over that array :)
$result = mysql_query("SELECT * FROM YOURDATABASE");
while($row = mysql_fetch_array($result))
{
echo $row['search'] . " " . $row['somenumber'];
echo " searches";
}
mysql_close($con);
I have found the solution to this. Thank you all for your guidance. :-) "you all rock"
$sql = "SELECT COUNT(*) AS Rows, search, SUM(searched) FROM anotherfeed.af_timeline_search GROUP BY search ORDER BY SUM(searched) DESC";
$result=mysql_query($sql) or die ('Error! yo.');
$row = mysql_fetch_array($result);
echo '<pre>';
print_r($row);
echo '</pre>';
/* top searches with loop */
$sql = "SELECT COUNT(*) AS Rows, search, SUM(searched) FROM anotherfeed.af_timeline_search GROUP BY search ORDER BY SUM(searched) DESC LIMIT 0, 20";
$result=mysql_query($sql) or die ('Error! yo.');
while($row = mysql_fetch_array($result))
{
echo ' (';
echo urldecode($row['search']) . " " . $row['SUM(searched)'] . "";
echo ') ';
}
I am able to print the array, hopefully i can get the loop down with a while statement. Feel free to add a loop if you have or know one that is better... Thank You.