8 Replies - 354 Views - Last Post: 09 February 2014 - 06:18 AM

#1 VB ASRAAF007  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 11-April 12

Fetching the last record inserted

Posted 03 February 2014 - 09:49 PM

Hello i have this small problem of getting the last record inserted in my database... i want to get the last record and echo it on my page... here are my codes



if (isset($_GET['search_offres_button']) == null)
{
		 	 	  $query = "SELECT * FROM `deposer_une_annonce` ORDER BY ID DESC LIMIT 0,1 WHERE  district LIKE '%$search_value%' OR ville LIKE '%$search_value%' OR categorie LIKE '%$search_value%' OR titre_annonce LIKE '%$search_value%' OR prix LIKE '%$search_value%' OR telephone LIKE '%$search_value%' ";
		  	
			

		  $run = mysql_query($query) or die(mysql_error());
		  $grab = mysql_fetch_assoc($result);
		  $max = $row['ID'];
		  mysql_data_seek($result, 0);
		 
 
		  while($row = mysql_fetch_array($run)){
	
	$district = $row['district'];
		  	$categorie = $row['categorie'];
			$prix = $row['prix'];
			$telephone = $row['telephone'];
			$ville = $row['ville'];
			$titre_annonce = $row['titre_annonce'];
			$image_principale_1 = $row['photo_principale'];



			 
			echo "<a style='text-decoration:none;' href='search_offres_display.php'>";
            echo "<div style='text-align:center;' style=width:300px; align='center';>
					<h4><p> Nom du produit: $titre_annonce</h4>
					<strong>Categorie:</strong> $categorie <p>
					<strong>Prix:</strong> $prix <p>
					<strong>Telephone:</strong> $telephone <p>
					<strong>District: </strong> $district </p></p><p>
					<strong>Ville: </strong> $ville </p></div>";
						
     echo "<div style='text-align:center;'>";?><?php echo"<img src=$image_principale_1 <height='150' width='200'>"?> <?php echo "</div>";
				
			echo "<a/>";	
			echo "<hr/>";
		  
		  }





Is This A Good Question/Topic? 0
  • +

Replies To: Fetching the last record inserted

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: Fetching the last record inserted

Posted 03 February 2014 - 11:44 PM

Quote

i want to get the last record and echo it on my page

for that you need MySQLs LAST_INSERT_ID() function.
Was This Post Helpful? 0
  • +
  • -

#3 VB ASRAAF007  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 11-April 12

Re: Fetching the last record inserted

Posted 04 February 2014 - 04:39 AM

and how do i adapt this to my codes up there? please
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: Fetching the last record inserted

Posted 04 February 2014 - 08:02 AM

Id try
SELECT {fields-of-interest} FROM {table} WHERE {PK-field} = LAST_INSERT_ID();


PS. PK => Primary Key
Was This Post Helpful? 0
  • +
  • -

#5 VB ASRAAF007  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 11-April 12

Re: Fetching the last record inserted

Posted 08 February 2014 - 10:00 AM

i already have this query running :

$query = "SELECT * FROM `deposer_test` WHERE  date LIKE '%$search_value%' OR name LIKE '%$search_value%'  ";



now how do i adapt this:
SELECT {fields-of-interest} FROM {table} WHERE {PK-field} = LAST_INSERT_ID();
to mine?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: Fetching the last record inserted

Posted 08 February 2014 - 12:04 PM

those two have nothing to do with each other. the first is looking for some data based upon a search value, the second takes the last entry put into the DB.

if you instead want the latest entry of the first query, then you will either need a DATETIME field that tells you what the latest entry of that selection is or, if you have an auto_increment field, the entry of that with the highest ID.
Was This Post Helpful? 0
  • +
  • -

#7 VB ASRAAF007  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 11-April 12

Re: Fetching the last record inserted

Posted 09 February 2014 - 01:53 AM

ok i've used the
 order by id desc 
to get the lastest record inserted ... Thanks for you replies :D really helped

This post has been edited by VB ASRAAF007: 09 February 2014 - 02:41 AM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: Fetching the last record inserted

Posted 09 February 2014 - 03:07 AM

View PostVB ASRAAF007, on 09 February 2014 - 09:53 AM, said:

ok i've used the
 order by id desc 
to get the lastest record inserted ...

the latest of your query (and that is not even guaranteed). that is a small, but very important difference. the absolutely latest inserted (with regards to the DB table) is still from LAST_INSERT_ID().
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5903
  • View blog
  • Posts: 12,808
  • Joined: 16-October 07

Re: Fetching the last record inserted

Posted 09 February 2014 - 06:18 AM

So, this returns all rows that meet your like criteria:
SELECT * 
  FROM `deposer_test`
  WHERE  date LIKE '%$search_value%' 
    OR name LIKE '%$search_value%' 



I'm going to assume that your pk is "id" and it's auto generated sequential. If this is true, then this gets the id of the the last inserted row:
SELECT max(id) FROM `deposer_test`



Putting the two together, this is the id of the last inserted row that meets the where:
SELECT max(id)
  FROM `deposer_test`
  WHERE  date LIKE '%$search_value%' 
    OR name LIKE '%$search_value%' 



Further, assuming that actually gives you a hit, this would be the record with that id:
SELECT a.*
  FROM `deposer_test` a
    INNER JOIN (
      SELECT max(id) as id
        FROM `deposer_test`
        WHERE  date LIKE '%$search_value%' 
          OR name LIKE '%$search_value%' 
      ) b
        ON a.id=b.id



The above query should return 0 or 1 rows.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1