while loop skipping query - php

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.

Related

Select query for getting a single value is not working

$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
$value = mysql_fetch_object($result);
$teacheremail2 = $value->temail;
echo $teacheremail2;
echo $teacheremail2 returns nothing.
$teachername is valid and i have checked multiple times.
It should be a two-dimensional array , you need
$value[0]->temail
The result of mysql_fetch_object($result) is an object(stdClass).
The explanation of object(stdClass) ican be found at this link
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
while ($value = mysql_fetch_object($result))
{
$teacheremail2 = $value->temail;
echo $teacheremail2;
}
First off, you'll want to run the query directly against your database to ensure that the query returns some kind of result.
Secondly, if that works, you'll want to echo $value directly to check that you are getting results back on the webpage.
Then you can check if temail is a field of $value
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
while ($value = mysql_fetch_object($result))
{
$teacheremail2 = $value->temail;
echo $teacheremail2;
}
hope this help

MYSQL Order BY "entry" DESC Not showing the highest

i'm having a slight problem with this mysql query.
$sql = "SELECT name FROM videos ORDER BY counter DESC LIMIT 5";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$list = mysql_fetch_assoc($result);
while($list = mysql_fetch_assoc($result)){
echo $list['counter'];
echo "<br>";
}
mysql_free_result($result);
Want i'm wanting to achieve is it listing 5 entrys by order "Counter" But when listing it? it only shows 4 entrys like so :-
5648
4575
1595
35
So where is my 5th entry? and why isn't it posting it? NOTE that the 5th entry is also the highest with a value of
305355
Thanks in advance
You fetch before the loop which pops one record off the result set (i.e. 305355).
$list = mysql_fetch_assoc($result); // REMOVE THIS LINE
while($list = mysql_fetch_assoc($result)) {
// output code
}
Try to reduce the code and use mysqli_ functions
$sql = "SELECT name FROM videos ORDER BY counter DESC LIMIT 5";
$result = mysql_query($sql, $conn);
while($list = mysql_fetch_assoc($result)){
echo $list['counter']."<br>";
}
try out this code..
$sql = "SELECT name FROM videos ORDER BY counter DESC LIMIT 5";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
while($list = mysql_fetch_assoc($result)){
echo $list['counter'];
echo "<br>";
}
mysql_free_result($result);
you are calling mysql_fetch_assoc two time that is why your first row get by first time you called the mysql_fetch_assoc and remaining show in second calls

How to print or echo rows when using COUNT(*) and SUM()

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.

While returning no results

I've got the following query in an existing script - but it's not always returning a value even though it should based off what's in the database. There are plenty of things in the database it SHOULD be grabbing - they are there.
Don't see anything wrong with it - but I barely do this anymore :) See anything?
$query = "SELECT id FROM xtags WHERE tag_id = '$tagid' ORDER BY RAND() Limit 2";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$query = "SELECT * FROM xtable WHERE id = '$row[id]'";
$result = mysql_query($query) or die(mysql_error());
$row2 = mysql_fetch_assoc($result);
echo $row2[title];
}
$result is being used inside the loop and outside, try making a new variable inside and not reusing the outside one.
You're reusing the $result variable inside the loop which overwrites the value for use in the while condition. Use a different name for $query and $result inside the loop.
I don't know if is ok, but you are using $result twice, one before the "while" and another inside the "while".
I would personally split the string and the variable $row.
Why not use var_dump() to see $row and the other variables ???
I don't understand what you are trying to do actually, but try this:
$query = "SELECT id FROM xtags WHERE tag_id = '".$tagid."' ORDER BY RAND() Limit 2";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$query2 = "SELECT * FROM xtable WHERE id = '".intval($row[id])."'";
$result2 = mysql_query($query2) or die(mysql_error());
$row2 = mysql_fetch_assoc($result2);
echo $row2[title];
}
Problem solved - did a join statement.

How to display MySQL Select statement results in PHP

I have the following code and it should return just one value (id) from mysql table. The following code doesnt work. How can I output it without creating arrays and all this stuff, just a simple output of one value.
$query = "SELECT id FROM users_entity WHERE username = 'Admin' ";
$result = map_query($query);
echo $result;
I do something like this:
<?php
$data = mysql_fetch_object($result);
echo $data->foo();
?>
You have to do some form of object creation. There's no real way around that.
You can try:
$query = "SELECT id FROM users_entity WHERE username = 'Admin' ";
//$result = map_query($query);
//echo $result;
$result = mysql_query($query); // run the query and get the result object.
if (!$result) { // check for errors.
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result); // get the single row.
echo $row['id']; // display the value.
all you have is a resource, you would still have to make it construct a result array if you want the output.
Check out ADO if you want to write less.
Not sure I exactly understood, what you want, but you could just do
$result = mysql_query('SELECT id FROM table WHERE area = "foo" LIMIT 1');
list($data) = mysql_fetch_assoc($result);
if you wish to execute only one row you can do like this.
$query = "SELECT id FROM users_entity WHERE username = 'Admin' ";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo $row[0];
there have been many ways as answered above and this is just my simple example. it will echo the first row that have been executed, you can also use another option like limit clause to do the same result as answered by others above.

Categories