Page 1 of 1

I really need help

Posted: Fri Feb 19, 2010 6:32 am
by costyman
hello there. I am having some trouble with the syntax. I will post the code so maybe you can tell me how to do it.

Code: Select all

 
<?php
 
$link = mysql_connect("localhost","root","");
mysql_select_db("proiect");
 
$query1 = 'SELECT path FROM stiri WHERE id=LAST_INSERT_ID() ';
$results1 = mysql_query($query1);
 
$query2 = 'SELECT nume FROM stiri WHERE id=LAST_INSERT_ID()';
$results2 = mysql_query($query2);
 
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<playlist>\n";
 
while($line = mysql_fetch_assoc($results1) & mysql_fetch_assoc($results2) ) {
    echo '<vid src="$query1" desc="$query2"/>\n';
}
 
echo "</playlist>";
 
mysql_close($link)
?>
 
 
And the problem is in the echo in the while statement :)
Thanks a lot.

Re: I really need help

Posted: Fri Feb 19, 2010 7:34 am
by social_experiment
Try this :

Code: Select all

<?php while ($line = mysql_fetch_assoc($results1)) {
 $valueOfSrc = $line['path'];
}
while ($line1 = mysql_fetch_assoc($results2)) {
 $valueOfDesc = $line1['nume'];
}
 
 echo "<vid src=\"$valueOfSrc\" desc=\"$valueOfDesc\" />\n";
?>
Hope this helps.

Re: I really need help

Posted: Fri Feb 19, 2010 10:25 am
by costyman
Well, basically, it's correct now. 10x. The thing it does not do anything. It returns no result. It only shows:

Code: Select all

 
 
<vid src="" desc="" />
 
 
I am thinking that maybe it has something to do with the queries...

What do you think?

Re: I really need help

Posted: Fri Feb 19, 2010 10:38 am
by AbraCadaver
Maybe those queries don't return any results? So then the while loop will never execute and those variables will never be set, but you echo them anyway.

In addition, why do you have 2 queries for the same table with the same condition for two different fields?

Code: Select all

$query1 = 'SELECT path, nume FROM stiri WHERE id=LAST_INSERT_ID()';
$results1 = mysql_query($query1) or print(mysql_error());
 
if(!$results1) {
    echo "No records found!";
}
while($line = mysql_fetch_assoc($results1)) {
    echo "<vid src=\"{$line['path']}\" desc=\"{$line['nume']}\"/>\n";
}

Re: I really need help

Posted: Fri Feb 19, 2010 10:54 am
by costyman
Well, now it's worse :P

Code: Select all

 
<playlist>
</playlist>
 

Re: I really need help

Posted: Fri Feb 19, 2010 10:57 am
by AbraCadaver
costyman wrote:Well, now it's worse :P

Code: Select all

 
<playlist>
</playlist>
 
It should at least say "No records found!" if you did it properly.

Re: I really need help

Posted: Fri Feb 19, 2010 11:06 am
by AbraCadaver
Also, if I remember correctly, you must have had an insert on that same connection in order to get the LAST_INSERT_ID().

Re: I really need help

Posted: Sun Feb 21, 2010 1:12 pm
by costyman
Hey there.

I wrote the code properly. At least I think so.

Maybe I miss-checked something and some of you guys can find it.

Code: Select all

 
 <?php
 
$link = mysql_connect("localhost","root","");
mysql_select_db("proiect");
 
$query1 = 'SELECT path, nume FROM stiri WHERE id=LAST_INSERT_ID()';
$results1 = mysql_query($query1) or print(mysql_error());
 
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<playlist>\n";
 
 
 
 if(!$results1) {
    echo "No records found!";
}
while($line = mysql_fetch_assoc($results1)) {
    echo "<vid src=\"{$line['path']}\" desc=\"{$line['nume']}\"/>\n";
}
 
 
 
echo "</playlist>";
 
mysql_close($link)
?>
 
If what you said is right, how can I retrieve the last added item from the db, without using LAST_INSERT_ID() function?

Re: I really need help

Posted: Sun Feb 21, 2010 10:02 pm
by AbraCadaver
It seems that in your scenario you want the latest addition regardless of what client or connection added it (newest addition). In that case you could use max(id), though I would prefer to add a timestamp field is set when you insert and use that. So you would also use a LIMIT 1 since you know you only want 1 record, and you don't need a loop for the results.