I really need help

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
costyman
Forum Newbie
Posts: 4
Joined: Fri Feb 19, 2010 6:28 am

I really need help

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: I really need help

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
costyman
Forum Newbie
Posts: 4
Joined: Fri Feb 19, 2010 6:28 am

Re: I really need help

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: I really need help

Post 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";
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
costyman
Forum Newbie
Posts: 4
Joined: Fri Feb 19, 2010 6:28 am

Re: I really need help

Post by costyman »

Well, now it's worse :P

Code: Select all

 
<playlist>
</playlist>
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: I really need help

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: I really need help

Post 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().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
costyman
Forum Newbie
Posts: 4
Joined: Fri Feb 19, 2010 6:28 am

Re: I really need help

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: I really need help

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply