$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die ("Fail".mysqli_error($conn));
$record2 = mysqli_fetch_array($rslt2);
$nRows = mysqli_num_rows($rslt2);
for ($i=0; $i<$nRows; $i++) {
$record2 = mysqli_fetch_array($rslt2);
echo $record2["title"];
}
the echo result is skipping the first row of the query, what am I doing wrong?
You are loosing the first row of your result set because you read the first row and throw it ways/ignore it
$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die ("Fail".mysqli_error($conn));
// this line gets the first row of your result set
// and you just ignore it therefore throw it away
$record2 = mysqli_fetch_array($rslt2);
$nRows = mysqli_num_rows($rslt2);
for ($i=0; $i<$nRows; $i++) {
$record2 = mysqli_fetch_array($rslt2);
echo $record2["title"];
}
As #Anant says, you are better advised to use a while as if the result set is empty, it just does nothing. Admittedly not always an advantage.
$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die ("Fail".mysqli_error($conn));
while ( $record2 = mysqli_fetch_array($rslt2) ) {
echo $record2["title"];
}
remove the line $record2 = mysqli_fetch_array($rslt2);, which is placed before the for loop.
new code would be:
$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die("Fail".mysqli_error($conn));
$nRows = mysqli_num_rows($rslt2);
for ($i=0; $i<$nRows; $i++) {
$record2 = mysqli_fetch_array($rslt2);
echo $record2["title"];
}
Related
Based on my codes, i need to restrict the insertion of the data by 3, i mean is like after the insertion of 3 data row, it will be restricted from inserting in data. Is that possible? For more information, is like the borrow inserting 3 times, then it cannot be inserted anymore. Is there anyway to do so? I am still learning php by the way, thank you.
if(isset($_POST['selector']))
$id=$_POST['selector'];
else
$id = '';
$member_id = $_POST['member_id'];
$due_date = $_POST['due_date'];
$isbn = $_POST['due_date'];
if ($id == '' ){
//header("location: borrow.php");
if(isset($_POST['isbn'])){
$isbn = $_POST['isbn'];
$query = mysql_query("select book_id from book WHERE isbn = '$isbn'")or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 0){
$row = mysql_fetch_array($query);
$bookid = $row['book_id'];
$date = date('Y-m-d');
}
mysql_query("insert into borrow (member_id,book_id,date_borrow,due_date) values ('$member_id','$bookid','$date','$due_date')")or die(mysql_error());
}
else{
header("location: borrow.php");
}
}else{
mysql_query("insert into borrow (member_id,date_borrow,due_date) values ('$member_id',NOW(),'$due_date')")or die(mysql_error());
$query = mysql_query("select * from borrow order by borrow_id DESC")or die(mysql_error());
$row = mysql_fetch_array($query);
$borrow_id = $row['borrow_id'];
}else{
mysql_query("insert into borrow (member_id,date_borrow,due_date) values ('$member_id',NOW(),'$due_date')")or die(mysql_error());
$query = mysql_query("select * from borrow order by borrow_id DESC")or die(mysql_error());
$row = mysql_fetch_array($query);
$borrow_id = $row['borrow_id'];
$N = count($id);
for($i=0; $i < $N; $i++)
{
mysql_query("insert borrowdetails (book_id,borrow_id,borrow_status)
values('$id[$i]','$borrow_id','pending')")or die(mysql_error());
}
header("location: borrow.php");
}
You just have to count number of user row before to make a new insert :
$query = mysql_query("SELECT COUNT(*) AS count FROM borrow WHERE member_id = '".$member_id."'");
$row = mysql_fetch_assoc($query);
if ( $row['count'] >= 3 )
echo('Max insert');
Also, check this : Why shouldn't I use mysql_* functions in PHP?
I'm not sure I understand you correctly.
You can restrict the number of rows returned by SELECT query using the LIMIT clause.
Make sure you either put an ORDER BY clause in there or determine that you don't care 'which' 3 rows will get inserted.
See here:
http://dev.mysql.com/doc/refman/5.0/en/select.html
I have a table and i want to echo the 2 last rows of my tabel, I used the below code but just the last one showed, what is the problem.
$result1 =(mysql_fetch_array(mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2")));
Print $result1['time'];
mysql_fetch_array = 1 fetch.
do it again for fetching 2nd result.
Also, use mysqli eh.
You're doing mysql_fetch_array only one time, so it gets the first element. If you want to get all the elements, then do it again, or use a loop.
Something like:
$query = "SELECT * FROM $table ORDER BY id DESC LIMIT 2";
while($row = mysql_fetch_array(mysql_query($query) )
{
echo $row['time'].'<br>';
}
For 2 Or more rows you need to loop it
$sql = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2")
while($row=mysql_fetch_array($sql))
{
echo $row['time']."<br>";
}
$query = mysqli_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2");
while ($result = mysqli_fetch_array($query)) {
echo $result['time'];
}
Gives out every return of your database (2 in this case). You should use mysqli_-functions.
Maybe you should try like this, since mysql_fetch_array returns only one row
while ($row = mysql_fetch_array($yourQuery)) {
echo $row["yourAlias"];
}
Further details here : http://fr2.php.net/manual/en/function.mysql-fetch-array.php
My solution:
$limit = 2;
$sql = "SELECT * FROM $table ORDER BY id DESC LIMIT $limit";
$result = mysql_query($sql);
$array = array(); $i = 0;
while($row = mysqli_fetch_array($result))
{
$array[$i] = $row;
$i++;
}
var_dump($array[0]);
var_dump($array[1]);
I would like to know how can I do a LIMIT 0,10 in this case (in order to do a pagination later) because when I do this I obtain a total of 30 results (10 results for each result of the first loop) which is logical, any idea ?
$select = "SELECT X FROM Y"
$result = mysql_query($select,$link);
$total = mysql_num_rows($result);
while($row = mysql_fetch_array($result)) {
$Name = $row['X'];
$select2 = "SELECT id FROM `".$Name."` LIMIT 0,30 ";
$result2 = mysql_query($select2,$link) or die ('Erreur : '.mysql_error() );
$total2 = mysql_num_rows($result2);
while($row2 = mysql_fetch_array($result2)) {
echo $row2['id']
}
}
Thanks in advance for your help !
I'm trying to update an older MySQL PHP method call but I end up calling the same SELECT call twice to achieve the same result. Is there a better way to do this?
Here is the original:
function updateProbabilities()
{
// first update the word count of each category
$rs = $this->con->select("SELECT category_id, SUM(count) AS total FROM nb_wordfreqs WHERE 1 GROUP BY category_id");
$total_words = 0;
while (!$rs->EOF()) {
$total_words += $rs->f('total');
$rs->moveNext();
}
$rs->moveStart();
if ($total_words == 0) {
$this->con->execute("UPDATE nb_categories SET word_count=0, probability=0 WHERE 1");
return true;
}
while (!$rs->EOF()) {
$proba = $rs->f('total')/$total_words;
$this->con->execute("UPDATE nb_categories SET word_count=".(int)$rs->f('total').",
probability=".$proba."
WHERE category_id = '".$rs->f('category_id')."'");
$rs->moveNext();
}
return true;
}
Here is my version:
function updateProbabilities() {
$sql = "SELECT category_id, SUM(count) AS total FROM nb_wordfreqs WHERE 1 GROUP BY category_id";
$res = mysql_query($sql) or die (mysql_error());
$total_words = 0;
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
$total_words += $row['total'];
}
if ($total_words >= 1) {
$sql = "SELECT category_id, SUM(count) AS total FROM nb_wordfreqs WHERE 1 GROUP BY category_id";
$res = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
$proba = $row['total']/$total_words;
$count = (int)$row['total'];
$sql = "UPDATE nb_categories SET word_count=".$count.",
probability=".$proba."
WHERE category_id = '".$row['category_id']."'";
mysql_query($sql) or die (mysql_error());
}
}
else {
$sql = "UPDATE nb_categories SET word_count=0, probability=0 WHERE 1";
mysql_query($sql) or die (mysql_error());
}
return true;
}
Do you see any way I can avoid this second duplicate SELECT call?
The first time you run the query, add this to the while loop to store the results:
$rowarray[] = $row; // added to prevent running it twice
Then the second time it comes up, replace this:
$sql = "SELECT category_id, SUM(count) AS total FROM nb_wordfreqs WHERE 1 GROUP BY category_id";
$res = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
with this:
foreach ($rowarray as $row) {
ALSO: move away from mysql_ functions, as they are deprecated and will soon be removed from PHP. Change to a PDO or MySQLi. You're already upating the code, may as well make that change too.
The mysql equivalent of $rs->moveStart() is:
mysql_data_seek($res, 0);
P.S. If you're rewriting into a new API, why are you using the deprecated mysql extension? You should use mysqli or PDO? They have similar methods for rewinding the cursor.
You can set the first query to a unique variable, and re-use the results of that query.
function updateProbabilities() {
$sql = "SELECT category_id, SUM(count) AS total FROM nb_wordfreqs WHERE 1 GROUP BY category_id";
$res = mysql_query($sql) or die (mysql_error());
$total_words = 0;
$results = array();
while ($row1 = mysql_fetch_array($res, MYSQL_ASSOC)) {
$total_words += $row1['total'];
$results[] = $row1;
}
if ($total_words >= 1) {
foreach ($results as $row2) {
$proba = $row2['total']/$total_words;
$count = (int)$row2['total'];
$sql = "UPDATE nb_categories SET word_count=".$count.",
probability=".$proba."
WHERE category_id = '".$row2['category_id']."'";
mysql_query($sql) or die (mysql_error());
}
}
else {
$sql = "UPDATE nb_categories SET word_count=0, probability=0 WHERE 1";
mysql_query($sql) or die (mysql_error());
}
return true;
}
I am trying to get the qunt with are int and add them all up and show a total
$qunt = 0;
$result = mysql_query("SELECT *
FROM properties_items
WHERE user_propid='$view'
ORDER BY id DESC") or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
$itemid = $row['itemid'];
$qunt = $row['qunt'];
$qunt++;
}
echo $qunt;
$qunt = 0;
$result = mysql_query("SELECT *
FROM properties_items
WHERE user_propid='$view'
ORDER BY id DESC") or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
$itemid = $row['itemid'];
$qunt += $row['qunt'];
}
echo $qunt;
Dare I say that you probably aren't using the itemid, in which case there's no point in looping through a result set in code. Instead, try something like this:
$qunt = mysql_query "SELECT SUM(qunt) FROM properties_items WHERE user_propid='$view'";
$qunt += $row['qunt'];
And get rid of the ++ line.