More PHP & MySQL help

Discuss Programming
Post Reply
byrdman
administrator
administrator
Posts: 225
Joined: Thu May 08, 2003 1:59 pm
Location: In the cloud

More PHP & MySQL help

Post by byrdman » Fri Jul 16, 2010 8:07 am

I have a question about using the mysql_real_escape_string in php with a mysql query.

my page that outputs stock quotes looks like:

Code: Select all


$symbol = mysql_real_escape_string($_GET['symbol']);
$query="SELECT * FROM table WHERE `symbol`='$symbol'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"name");
$symbol=mysql_result($result,$i,"symbol");
$price=mysql_result($result,$i,"price");
$change=mysql_result($result,$i,"change");
$pchange=mysql_result($result,$i,"pchange");

The rest of the above just outputs the values to XML...

in the browser if you type http://myserver.com/getstocks.php?symbol=T
I would get AT&T's latest stocks.

how could I use the above to get multiple stocks if I put in my browser:
http://myserver.com/getstocks.php?symbo ... BBY+IBM+KO

Thanks for reading!!

byrdman
administrator
administrator
Posts: 225
Joined: Thu May 08, 2003 1:59 pm
Location: In the cloud

Post by byrdman » Tue Sep 21, 2010 12:03 pm

This issue came back up and I was wondering if I could prod anyone's brain to a solution for this. I am thinking array, but usually my thinking leads to things breaking :)

User avatar
Void Main
Site Admin
Site Admin
Posts: 5712
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Tue Sep 21, 2010 5:10 pm

It sounds like an array/hash is the way to go to me. I would put a little thought into the best way to pass the data. You could pass as parameters as in your example or you might consider using session variables. Once you get it into an array it should be fairly simple to wrap your existing code into a foreach loop.

byrdman
administrator
administrator
Posts: 225
Joined: Thu May 08, 2003 1:59 pm
Location: In the cloud

Post by byrdman » Wed Sep 22, 2010 10:44 am

I initially started with taking the data in the url:
?symbol=T+GOOG+IBM...
and trying to throw the symbols into a for each loop after a regex got rid of the "+" symbols.

That's where I got messed up...wasnt sure if the foreach loop would work or if I just don't know what I am doing! :)

Post Reply