Jump to content

newbie question for php and mysql to return and DISPLAY info


Luvac Zantor

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!

Link to comment
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]

Link to comment
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*

Link to comment
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?

Link to comment
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 :)

Link to comment
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.

Link to comment
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

Link to comment
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!!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.