More PHP & MySQL help

Discuss Programming

More PHP & MySQL help

Postby 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

Postby 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 :)
byrdman
administrator
administrator
 
Posts: 225
Joined: Thu May 08, 2003 1:59 pm
Location: In the cloud

Postby 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.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby 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! :)
byrdman
administrator
administrator
 
Posts: 225
Joined: Thu May 08, 2003 1:59 pm
Location: In the cloud


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron