Fetching Most Recent Record

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
highflight1985
Forum Newbie
Posts: 4
Joined: Tue Aug 30, 2011 10:32 am

Fetching Most Recent Record

Post by highflight1985 »

Yep, it's been asked before. But I'm running into problems...

Goal: Add a record then fetch the auto-incremented primary key column value to then query the database on the data just entered and do some math on it.

Flow: User enters data. Data inserted into database. Find "loadid" (auto-incremented primary key column name) of the record just entered. Call a function to do some math on this data. Return an array with final values.

The problem: I can get the "loadid" number just fine. But actually getting the row's data is failing for some reason. I'm probably just having a bad Tuesday...

Code:

Code: Select all

function load_analysis($type,$start,$end,$loadid)
{

echo "<br>Just entered analysis.<br>";
echo "<br>Type: " . $type . ".<br>";
echo "<br>Start: " . $start . "<br>";
echo "<br>End: " . $end . "<br>";
echo "<br>LoadID: " . $loadid . "<br>";
echo "<br>Checking type...<br>";

if ($type == "single") {
echo "<br>Type is single<br>";

$query = "SELECT * FROM loads WHERE 'loadid' LIKE '" . $loadid . "'";

echo "<br>Query: " . $query;

$result = mysql_query($query);

echo "<br>Result: " . $result . "<br>";

while($row = mysql_fetch_assoc($result)) {

echo "<br>Row: " . $row . "<br>";
echo "<br>BasicPayMethod:" . $row['basicpaymethod'] . "<br>";

.... Function continues but rest of it is irrelevant....

$result prints as Resource ID #11. The stuff inside my while statement just doesn't happen.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Fetching Most Recent Record

Post by Celauran »

highflight1985 wrote:$result prints as Resource ID #11. The stuff inside my while statement just doesn't happen.
Have you checked the number of rows being returned?
highflight1985
Forum Newbie
Posts: 4
Joined: Tue Aug 30, 2011 10:32 am

Re: Fetching Most Recent Record

Post by highflight1985 »

What an obvious idea! I didn't think of that. I guess I should have but I just assumed the number of rows would be at least 1 and only 1, so I didn't bother...

Added echo "<br>Number of Rows" . mysql_num_rows($result) . "<br>";

Prints: 0

That's a step in the right (and wrong) direction.

Here's the code that inserts the new record and finds out what the new record is:

Code: Select all

$sqlInsertIntoLoads = "INSERT INTO loads (loadid, userid, userloadid, carrier) VALUES (null, '$uid', '$userloadid', '$carrier')";
if ($debug) {
 echo $sqlInsertIntoLoads;
}

 if (!mysql_query($sqlInsertIntoLoads,$conn))
  {
  die('Error! Was unable to add the load: ' . mysql_error());
  }
 $loadid = mysql_insert_id();

"New load inserted with LoadID: " . $loadid . "<br>";

//Analyse the load:

echo "About to get into Analysis";
$analysis = load_analysis("single",null,null,$loadid);
Each time I refresh the page, it prints:
About to get into Analysis
Just entered analysis.

Type: single.

Start:

End:

LoadID: 17

Checking type...

Type is single

Query: SELECT * FROM loads WHERE 'loadid' LIKE '17'
Result: Resource id #11

Number of rows: 0
The number for loadid increments each time as it should.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Fetching Most Recent Record

Post by Celauran »

Looks like you're using single quotes around the column name. Does it work if you change that to backticks?

Code: Select all

SELECT * FROM tablename WHERE `column` LIKE 'foo'
highflight1985
Forum Newbie
Posts: 4
Joined: Tue Aug 30, 2011 10:32 am

Re: Fetching Most Recent Record

Post by highflight1985 »

Wow. That was it! It's working now. But that brings up another question:

Why would it "work" with the single quotes and not give me an error nor function 100%? Why do "back ticks" work where single quotes do not?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Fetching Most Recent Record

Post by Celauran »

Why single quotes ever worked, I have no idea. They're not meant to and give me an error if I try.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Fetching Most Recent Record

Post by McInfo »

The quotes produced a valid query because LIKE also operates on strings, not just fields.

Code: Select all

'column' LIKE 'foo'
attempts to find "foo" in the literal string "column".
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Fetching Most Recent Record

Post by Celauran »

McInfo wrote:The quotes produced a valid query because LIKE also operates on strings, not just fields.

Code: Select all

'column' LIKE 'foo'
attempts to find "foo" in the literal string "column".
Good to know. Thanks for the tip.
Post Reply