Jump to content

Archived

This topic is now archived and is closed to further replies.

Luvac Zantor

newbie question for php and mysql to return and DISPLAY info

Recommended Posts

This is the code, pretty straight forward.

 


$server = mysql_connect($host, $username, $password) or die(mysql_error()); 



$connection = mysql_select_db($database, $server); 





$sql = \'SELECT PASSING.Player, PASSING.Cmp, PASSING.Att, PASSING.Yds, PASSING.TD, PASSING.Ints\';

$sql .= \'FROM PASSING\';

$sql .= \'ORDER BY PASSING.Yds DESC LIMIT 0, 3\'; 



$result = mysql_query($sql);



echo $result





?>

 

I\'ve tried many variations, but obviously I\'m new and have no clue. I assume it\'s parsing, because I get a blank page (when I mess around with the query, i\'ll get errors). It\'s the damn blank page.

 

Any suggestions? Thanks!

Share this post


Link to post
Share on other sites

3 comments.

 

Something to keep in mind when your coding, about 99% of the things in php need to end in ;

You executed the query but after that you need to fetch the array you just told mysql to get.

And lastly, when you pull something from mysql, it assigns the results to an array, so you need to tell php what part of the array you want to show.

 

So you made a good effort and got real close :)

 

give this a shot

 

[php:1:602bbf1d27]<?php

$server = mysql_connect($host, $username, $password) or die(mysql_error());

 

$connection = mysql_select_db($database, $server);

 

$sql = \'SELECT PASSING.Player, PASSING.Cmp, PASSING.Att, PASSING.Yds, PASSING.TD, PASSING.Ints\';

$sql .= \'FROM PASSING\';

$sql .= \'ORDER BY PASSING.Yds DESC LIMIT 0, 3\';

 

$query = mysql_query($sql);

 

$result = mysql_fetch_array(query);

 

echo \"Player is \" . $result[Player] . \"<BR>\";

 

echo \"Players yards are \" . $result[Yds] . \"<BR>\";

 

 

 

?>[/php:1:602bbf1d27]

Share this post


Link to post
Share on other sites

this is the error I get

 

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/Zantor/public_html/af2/test.php on line 24

 

 

line 24 is

 

$result = mysql_fetch_array(query);

 

 

I noticed,, did you forget the $ before query? going to try that.. *shrugs*

Share this post


Link to post
Share on other sites

looks like my query is wrong.. too many lines maybe?

 

 

regardless it works now as this....

 


$server = mysql_connect($host, $username, $password) or die(mysql_error()); 



$connection = mysql_select_db($database, $server); 



$sql = \'SELECT * FROM PASSING\';



$query = mysql_query($sql); 



$result = mysql_fetch_array($query); 



echo "Player is " . $result[Player] . "<BR>"; 



echo "Players yards are " . $result[Yds] . "<BR>"; 

 

It displays now, but how do I get it to display just the top 3 in yards?

Share this post


Link to post
Share on other sites

ah okay, what was probably happening was your query was returning nothing. If you have phpmyadmin you can paste the query in there and see what you get back. The query might of worked but if nothing gets returned then you\'d get that error on the fetch array.

 

And to move through the records you want to do a for or while loop, I prefer for\'s.

 

[php:1:bb865c6431]

$server = mysql_connect($host, $username, $password) or die(mysql_error());

 

$connection = mysql_select_db($database, $server);

 

$query = mysql_query(\"SELECT Player, Yds FROM PASSING ORDER BY Yds DESC LIMIT 3\");

 

for ($i = 0; $i < mysql_num_rows($query); $i++)

{

$result = mysql_fetch_row($query);

echo \"Player is \" . $result[Player] . \"<BR>\";

 

echo \"Players yards are \" . $result[Yds] . \"<BR>\";

}

[/php:1:bb865c6431]

 

That should work. If that works then just start adding stuff to your query, like the TD and other fields. Let me know if it doesn\'t work and I\'ll crank on it some more :)

Share this post


Link to post
Share on other sites

btw, the query doesn\'t have to be on one line. The way you were building the query was right.

Share this post


Link to post
Share on other sites

trying that now.

 

I agree that it should work, you\'d think, but if my query is multiple lines,, doesn\'t work. I put it in one line, it works

 

*Shrugs*

 

Also the query works in phpmyadmin, which is why I was initially confused.

 

btw, thanks for the help!!! :)

Share this post


Link to post
Share on other sites

the only reason I could see it not working is because of the spacing you had. You\'d run into the problem of it looking like

 

SELECT PASSING.Player, PASSING.Cmp, PASSING.Att, PASSING.Yds, PASSING.TD, PASSING.IntsFROM PASSINGORDER BY PASSING.Yds DESC LIMIT 0, 3\';

 

with no space between the Ints and From and Passing and order.

Share this post


Link to post
Share on other sites

hmm, okay, try two different things.

 

Maybe someone that knows more about mysql can explain it but this is what I get twisted on all the time heh

 

[php:1:1c734f0c85]

for ($i = 0; $i < mysql_num_rows($query); $i++)

{

$result = mysql_fetch_row($query);

echo \"Player is \" . $result[0] . \"<BR>\";

 

echo \"Players yards are \" . $result[1] . \"<BR>\";

}

[/php:1:1c734f0c85]

 

or

 

[php:1:1c734f0c85]

 

for ($i = 0; $i < mysql_num_rows($query); $i++)

{

$result = mysql_fetch_array($query);

echo \"Player is \" . $result[Player] . \"<BR>\";

 

echo \"Players yards are \" . $result[Yds] . \"<BR>\";

}

[/php:1:1c734f0c85]

 

let me know if either of those work

Share this post


Link to post
Share on other sites

got it... I modified what you had slightly and it\'s working now

 

 


$connection = mysql_select_db($database, $server); 





$sql = \'SELECT * FROM PASSING ORDER BY PASSING.Yds DESC LIMIT 0, 3 \';



$query = mysql_query($sql); 



$i = 0;

while ($i < mysql_num_rows($query)) 

{ 

$result = mysql_fetch_array($query); 

echo "Player is " . $result[Player] . "<BR>"; 



echo "Players yards are " . $result[Yds] . "<BR>"; 

$i++;

} 

 

some changes probably not needed, but changed the internal num_row to fetch_array and it\'s doing what it should!

 

 

THANKS FOR THE INSIGHT, it utterly helped me fix the problem. THANKS!!!

Share this post


Link to post
Share on other sites

dang you were quick to respond.... :)

 

I just guessed at using the fetch_array, which apparently is what you replied with.

 

again, thanks for the help!!!!!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.