I have the following inside a foreach loop (displaying my various videos), I'm trying to display some alternate text for the top three voted videos. What on earth am I doing wrong (a lot clearly)...
$sql = "SELECT video_id FROM videos WHERE displayable='y' ORDER BY votes desc LIMIT 0,3";
$result = mysql_query($sql);
$row = #mysql_fetch_array($result);
if(in_array($video->getProperty('video_id')) == $row['video_id']) {
do this...
} else {
do this..
}
Firstly replace your code with some error preventing techniques like so!
$sql = "SELECT video_id FROM videos WHERE displayable='y' ORDER BY votes desc LIMIT 0,3";
if(false != ($result = mysql_query($sql))
{
$row = mysql_fetch_assoc($result); //Dont need the # restraint as the result is not false above.
//Also to get associate keys you need to use mysql_fetch_assoc
if($video->getProperty('video_id') == $row['video_id'])) //Remove the in array as your directly comparing the to entities with ==
{
//Match
}else
{
//Video does not match
}
}
Your main problem was the mysql_fetch_array(), Please research the differences with mysql_fetch_array() and mysql_fetch_assoc();
--
Edit: The way i would go
//Change the query and the loop way.
$sql = "SELECT video_id FROM videos WHERE displayable='y' AND video_id != '".(int)$video->getProperty('video_id')."' ORDER BY votes desc LIMIT 0,3";
if(false != ($result = mysql_query($sql))
//Use the # restraint if you have E_NOTICE on within E_Reporting
{
while($row = mysql_fetch_assoc($result))
{
//Print the $row here how you wish for it to be displayed
}
}else
{
//We have an error?
echo '<strong>Unable to list top rated videos, please check back later.</strong>'
}
}
mysql_fetch_array only returns a single row, you need to loop through your results to build an array containing the top three ids.
$sql = "SELECT video_id FROM videos WHERE displayable='y' ORDER BY votes desc LIMIT 0,3";
$result = mysql_query($sql);
while($row = #mysql_fetch_array($result)) {
$topthree[] = $row["video_id"];
}
Then you can use in_array but with the correct syntax:
if(in_array($video->getProperty('video_id'), $topthree)) {
do this...
} else {
do this..
}
Related
Maybe I am overthinking this, but I have narrowed my query to find a row down to 1 result that I need, and it will not display. Wondering if someone could tell me what I am doing wrong.
$result = mysqli_query($link, "SELECT pageid FROM article ORDER BY id DESC LIMIT 1");
$row = mysqli_use_result($result);
echo $row;
I have it selecting the last row and supplying me with the stored data from the pageid of the last row.
I had to adapt my code. I believe it was because I use mysql. However, this code will work if you use mysqli
$pageid = "SELECT pageid FROM articles ORDER BY id DESC LIMIT 1";
$resultpageid = $link->query($pageid);
if ($resultpageid->num_rows > 0) {
while ($row = $resultpageid->fetch_assoc()) {
$pagenumber = $row["pageid"];
}
} else {
echo "0 results";
}
mysqli doesn't have any function to get a single column from a single row. You need to use one of the fetch methods e.g. fetch_array(). You don't need any loop if you use LIMIT 1.
Just fetch a single row and get the column from the returned array:
$pageid = "SELECT pageid FROM articles ORDER BY id DESC LIMIT 1";
$resultpageid = $link->query($pageid);
$row = $resultpageid->fetch_assoc();
// or
$row = $resultpageid->fetch_array();
if ($row) {
echo $row["pageid"];
} else {
echo "No record found!";
}
I am trying to show some text depending upon what a status is set to in a db table.
See my code below:
$result=mysql_query("SELECT * FROM hr_recruitment_stages where vacancy_ref='$vacancyref' order by added_on DESC limit 0,1")or die('ERROR 315' );
$row = mysql_fetch_array($result);
$stage_name = $row ['stage_name'];
if($stage_name['stage_name'] == 'Shortlisting') { echo"Shortlisting"; } else { echo"Not Shortlisting"; } ?>
However this doesnt seem to be working properly as it is showing as Not Shortlisting even when stage_name equals Shortlisting.
Any ideas why?
Its variable type mistake. Check your assigned variable, you assigned the Array Element not the entire array. so try like below.
<?php
$result = mysql_query("SELECT * FROM hr_recruitment_stages where vacancy_ref='$vacancyref' order by added_on DESC limit 0,1") or die('ERROR 315' );
$row = mysql_fetch_array($result);
$stage_name = $row['stage_name'];
if($stage_name == 'Shortlisting') {
echo"Shortlisting";
} else {
echo"Not Shortlisting";
}
?>
Refer this Article for PHP Array understanding.
http://php.net/manual/en/language.types.array.php
<?php
mysql_select_db($database_XXX, $XXX);
$result= mysql_query("SELECT COUNT(*) FROM news");
$total = mysql_result($result, 0, 0);
// create a random number
mt_srand((double)microtime()*1000000);
$number = mt_rand()%$total;
// get a random entry
$result= mysql_query("SELECT * FROM news LIMIT $number, 5");
$row = mysql_fetch_array($result);
?>
This is the PHP code I'm using and it pulls up random data from the table I want but I can't seem to figure out how to have it not show the current post it's on. Will I need to throw in an if statement in? If I do where would it go and how to impletment it. The only thing I can thing of is using if statements to check if the post_id on page matches the post_id posted. But I'm new to this and the only thing I can think of is.
if(!$row['post_id'] == $_GET['id']) {
}
I don't know what to make it do after. Also if anyone knows how or can help point me in the right direction that would be great. Thanks.
Here is the update of the total thing here. It still shows post it's already on. hope this helps. This is the php code for the page.
<?php
mysql_select_db($database_xxx, $xxx);
$result= mysql_query("SELECT COUNT(*) FROM news");
$total = mysql_result($result, 0, 0);
// create a random number
mt_srand((double)microtime()*1000000);
$number = mt_rand()%$total;
// get a random entry
$result= mysql_query(sprintf("SELECT * FROM news WHERE post_id <> %d LIMIT %d, 3", $post->post_id, $number));
$row = mysql_fetch_array($result);
?>
<?php
if (! isset($_GET['id']) || (int) $_GET['id'] === 0 ) {
echo "Incorrect input, aborting";
exit;
}
mysql_select_db($database_xxx, $xxx);
$sql = "SELECT * FROM news WHERE post_id = " . $_GET['id'];
// a line of debug to make sure things are as expected
$query = MYSQL_QUERY($sql);
// query your table for a match with post_id
if (mysql_num_rows($query) == "1")
// if a record is found, show the info
{
$fetch = mysql_fetch_array($query); // set $fetch to have the values from the table
} else {
echo "No match in database found."; // if no match is found, display this error
}
?>
Assuming that code is on the page where you view the main product:
$result= mysql_query(sprintf("SELECT * FROM news WHERE id <> %d LIMIT %d, 5", $post->id, $number));
Also, you should not be using mysql_* functions anymore as they are deprecated; checkout PDO
//get the current member count
$sql = ("SELECT count(member_id) as total_members from exp_members");
$result = mysql_query($sql) or die(mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows != 0) {
while($row = mysql_fetch_array($result)) {
$total_members = $row['total_members'];
}
}
//get list of products
$sql = ("SELECT m_field_id, m_field_label from exp_member_fields where m_field_name like 'cf_member_ap_%' order by m_field_id asc");
$result = mysql_query($sql) or die(mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows != 0) {
while($row = mysql_fetch_array($result)) {
$m_field_id = $row['m_field_id'];
$m_field_label = $row['m_field_label'];
$sql2 = ("SELECT count(m_field_id_".$m_field_id.") as count from exp_member_data where m_field_id_".$m_field_id." = 'y'");
$result2 = mysql_query($sql2) or die(mysql_error());
$num_rows2 = mysql_num_rows($result2);
if ($num_rows2 != 0) {
while($row2 = mysql_fetch_array($result2)) {
$p = ($row2['count']/$total_members)*100;
$n = $row2['count'];
$out .= '<tr><td>'.$m_field_label.'</td><td>'.number_format($p,1).'%</td><td>'.$n.'</td></tr>';
}
}
}
}
It's easier to help if you can describe in non-code terms what you're trying to accomplish. But one indicator of a problem is seeing a php loop on rows from one query with another query executing for each row.
There are ways to query for subtotals. But it would be easier to explain if you can explain the goal a bit.
count query would always return 1 row, so you don't need the loop
$sql = ("SELECT count(member_id) as total_members from exp_members");
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
$total_members = $row['total_members'];
Other than that i am not sure how you can make it better. You can do the same for both of your count queries.
As these are straight forward queries, any bottleneck i guess now would be on the MySQL end
The first COUNT query ("get the current member count") should execute almost instantaneously.
The second query ("get list of products") may be slow depending on your indexes. You are querying on m_field_name and then ordering on m_field_id so you may need a combined index of the two.
The third query, which is executed repeatedly (once for each product), is querying on m_field_id_* (i.e. any of a number of possible fields), so you should probably make sure they are indexed.
In summary, you need to a) figure out which query is running slow, b) index things that need to be indexed, and c) combine queries if possible.
I'm working on a php/mySQL/ajax shoutbox and have run into a small snag.
On the page I want the content to be loaded from oldest to newest, with the newest on the bottom. I also want to limit the output to reduce load times once the database starts to get a lot of data in it.
Here is the current code;
<?php
include_once("../includes/db.php");
include_once("../includes/functions.php");
$q="SELECT tM.*, tC.char_name as username, tC.char_id as char_id
FROM shoutbox tM JOIN characters tC
ON tC.char_id=tM.char_id
ORDER BY shout_id DESC LIMIT 25";
db_Connect();
$result=mysql_query($q);
while($row=mysql_fetch_array($result))
{
$classColor = getClassColor($row['char_id']);
echo "<span class='".$classColor."'>".$row['username']."</span>: ",nl2br($row['shout_message'])."<br />";
}
mysql_Close();
?>
I have tried using while($row=array_reverse(mysql_fetch_array($result))) as well as $result = array_reverse(mysql_query($q)) but both return an error that array_reverse needs to be fed an array.
So far anything I have found on the web from the SQL side have all been answered "just use DESC or ASC accordingly."
$res = mysql_query($q);
$shouts = array();
while($row = mysql_fetch_assoc($res))
$shouts[] = $row;
$shouts = array_reverse($shouts);
foreach($shouts as $shout) {
// show them...
}
If you want oldest to newest, and shout_id is auto increment just use
ORDER BY shout_id ASC LIMIT 25
You have DESC/ASC mixed up
$result=mysql_query($q);
unset($temp_array);
while($row = mysql_fetch_array($result))
$temp_array []= $row;
$temp_array = array_reverse($temp_array);
foreach ($temp_array as $row)
{
$classColor = getClassColor($row['char_id']);
echo "<span class='".$classColor."'>".$row['username']."</span>: ",nl2br($row['shout_message'])."<br />";
}
Seems like a lit of work. I just used this:
$result = mysql_query($strQuery) or die(mysql_error());
while ($row = array_reverse(mysql_fetch_array($result))) {
$row['something'];
}