For example I have the code
$query="Select * from table order by id";
$res1=mysqli_query($link,$query);
while($row=mysqli_fetch_array($res1)) {
echo $row['Id'];
}
echo '<br>';
$query="Select * from table order by user";
$res1=mysqli_query($link,$query);
while($row=mysqli_fetch_array($res1)) {
echo $row['Id'];
}
It is possible to do this without second query and without use of array and foreach, to change orders of row in resul1 from first query to get order like in second query.
You can easily order by several fields at one time:
$query = "SELECT * FROM table ORDER BY id, user ";
$res1 = mysqli_query($link,$query);
....
you can achieve this by passing a php variable in the mysql query.
$query="Select * from table order by $order";
and set $order as you want.
$query = "SELECT * FROM table ORDER BY id, user ";
This will order your data first by id and then user.
You can change order of results in PHP.
Get all results into a PHP array.
Write a comparison function.
Use usort http://php.net/manual/en/function.usort.php
Related
I have a scenario where I want to fetch records from my database table using SQL NOT operator and in the condition it doesn't negate two or more conditions but one; e.g
$sql1 = mysql_query("Select * from `".$dbTable17."` where `Dept`='$dept' AND NOT `Remark`='Error' ORDER BY `Date` ASC")or die("Error!~".mysql_error());
I assume you understand that the variables are already declared.
So, it doesn't fetch the records and I need a way to do it inside my while() loop construct.
Try this:
$sql1 = mysql_query("Select * from $dbTable17 where Dept='$dept' AND Remark!='Error' ORDER BY Date ASC")
or die(mysql_error());
I want to get the maximum id of row data. In my table first column is id then firstname and etc.
this is the sql command I used to get the max(id) of row data.
<?PHP $maxid=$this->db->query("SELECT MAX(id) FROM `emplyee_personal_details`");
print_r( $maxid) ;?>
but it prints bunch of data as it is a array. But I need only the maximam id of row data for validation before data inserting and updating.
How to get the maxid. I use codeigniter framework.
Try this:
$maxid = $this->db->query('SELECT MAX(id) AS `maxid` FROM `emplyee_personal_details`')->row()->maxid;
UPDATE
This will work even if your table is empty (unlike my example above):
$maxid = 0;
$row = $this->db->query('SELECT MAX(id) AS `maxid` FROM `emplyee_personal_details`')->row();
if ($row) {
$maxid = $row->maxid;
}
The problem with using a raw query like "SELECT MAX(id) ..." is it is not abstract and may not work for every SQL engine. I think the active record way to do it is like this:
$this->db->select_max('id');
$query = $this->db->get('emplyee_personal_details');
// Produces: SELECT MAX(id) as age FROM emplyee_personal_details
See: http://ellislab.com/codeigniter/user-guide/database/active_record.html
SELECT id FROM table ORDER BY id DESC LIMIT 1
That will get you the highest id value, and when I read this, "it prints bunch of data as it is a array" I get the sense that what you really want is a part of that array. DB queries always return complex structures like arrays or objects. So if you wanted just the scalar value (the number as an integer) you might use something like this:
$maxid = (int)$maxid['id'];
or like this (if you have an object):
$maxid = (int)$maxid->id;
HTH, ~Ray
Try this,hope it helps,
return $this->db->select_max('id')
->get('your_table_name')
->row()->id;
public function getMaxCategoryId() {
$query = $this->db->query("SELECT category_id+1 AS maxid FROM " . DB_PREFIX . "category ORDER BY category_id DESC LIMIT 1");
return $query->row['maxid'];
}
error undefined index maxid
<?php`$qry = "select max(ID)+1 As ID from records";`
$result = $con->query($qry);
$row = $result->fetch_assoc();`echo "New ID To Enter = ".$row["ID"];?>
After Connection Just Write This Code It Will Work
I have a very simple database table of posts, I'd like to be able to print the most recent entry first, followed then by the next most previous entry etc etc when I retrieve them all . How do I do this?
$someText = mysql_query("SELECT * FROM text");
while($row = mysql_fetch_array($someText)) {
echo "$row[column]";
}
You have to order them by id or date or something else. (ASC or DESC)
This might do the trick if you've added them in the correct order.
$test = mysql_query("SELECT * FROM posts ORDER BY id DESC");
while($row = mysql_fetch_array($test)) {
echo "$row[post]";
}
In your SQL statement, "SELECT * FROM posts ORDER BY <date_time_field> DESC" If you don't have a date_time_field, you can use the primary key (usually id).
If you are just trying to order them by date, the MySQL would be like:
SELECT * FROM posts ORDER BY date DESC
$result3=mysql_query("select * from $mail");
while($row=mysql_fetch_array($result3)) {
if($row['status']!=NULL) {
echo $row['status'];
echo $row['date'];
echo $row['time'];
}
}
i want that the last field in the database should be displayed first. How to implement this?
$result3 = mysql_query("SELECT * from $mail ORDER BY `date` DESC");
Never do yourself what the database can do for you.
If, for whatever weird reason, you actually want to traverse the result set in reverse order, you'll have to use mysql_data_seek, starting at mysql_num_rows() - 1 and decrementing the pointer after each call to mysql_fetch_array.
Use an ORDER BY clause in your query so that the results come in the order you want to display them.
Without an ORDER BY, there is no guarantee about what order the data will come in, so "reversing" that doesn't make sense.
Either user an ordering in the SQL query itself using ORDER BY or put them into an array in PHP and reverse the array:
$all_rows = array();
while($row=mysql_fetch_array($result3)) {
$all_rows[] = $row;
}
$all_rows = array_reverse($all_rows);
Order your fields with the ORDER BY sql feature. So choose what field you want to order by, and order it by that field..
$result3=mysql_query("select * from $mail ORDER BY date DESC ,time DESC");
or if they have an id field:
$result3=mysql_query("select * from $mail ORDER BY id DESC");
And while you're at it, make sure that you know EXCATLY what is in $mail!
"select * from $mail order by date DESC, time DESC"
MY SQL QUERY:
$q = mysql_query("SELECT * FROM `ads` WHERE keywords LIKE '%$key%' ORDER BY RAND()");
RESULTS: KEYWORD123
This query searches and results in one random row but i want to show 2 random rows.
How to do that?
any solution?
how??
im grabbing it using this
$row = mysql_fetch_array($q); if ($row
<= 0){ echo 'Not found'; }else{ echo
$row['tab']; }
That query (as-is) will return more than one row (assuming more than one row is LIKE %$key%). If you're only seeing one record, it's possible you're not cycling through the result set, but rather pulling the top response off the stack in your PHP code.
To limit the response to 2 records, you would append LIMIT 2 onto the end of the query. Otherwise, you'll get every row that matches the LIKE operator.
//Build Our Query
$sql = sprintf("SELECT tab
FROM ads
WHERE keyword LIKE '%s'
ORDER BY RAND()
LIMIT 2", ('%'.$key.'%'));
// Load results of query up into a variable
$results = mysql_query($sql);
// Cycle through each returned record
while ( $row = mysql_fetch_array($result) ) {
// do something with $row
echo $row['tab'];
}
The while-loop will run once per returned row. Each time it runs, the $row array inside will represent the current record being accessed. The above example will echo the values stored in your tab field within your db-table.
Remove your order by and add a LIMIT 2
That happens after the execution of the SQL.
Right now you must be doing something like
$res = mysql_query($q);
$r = mysql_fetch_array($res);
echo $r['keywords'];
what you need to do
$q = mysql_query("SELECT * FROM ads WHERE keywords LIKE '%$key%' ORDER BY RAND() LIMIT 2");
$res = mysql_query($q);
while($r = mysql_fetch_array($res)){
echo "<br>" . $r['keywords'];
}
Hope that helps
This query will return all rows containing $key; if it returns only one now this is simply by accident.
You want to add a LIMIT clause to your query, cf http://dev.mysql.com/doc/refman/5.0/en/select.html
Btw both LIKE '%... and ORDER BY RAND() are performance killers