Jump to content


Photo

newbie question for php and mysql to return and DISPLAY info


  • Please log in to reply
18 replies to this topic

#1 Luvac Zantor

Luvac Zantor
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 02 November 2003 - 06:13 AM

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!

#2 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 02 November 2003 - 07:19 AM

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]

#3 Luvac Zantor

Luvac Zantor
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 02 November 2003 - 07:35 AM

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*

#4 Luvac Zantor

Luvac Zantor
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 02 November 2003 - 07:36 AM

nope same error..... :(

#5 Luvac Zantor

Luvac Zantor
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 02 November 2003 - 07:38 AM

btw, if I use a query analizer.....

the query DOES return information, so it\'s there....

#6 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 02 November 2003 - 08:01 AM

look at this tutorial,

http://www.phpfreaks...orials/33/0.php

Once you query the database you need to fetch the data into an array. Yes the database was queried but the info is not really in a variable that\'s usable until you fetch it.

#7 Luvac Zantor

Luvac Zantor
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 02 November 2003 - 08:39 PM

thanks for the link

looking now.

#8 Luvac Zantor

Luvac Zantor
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 02 November 2003 - 08:46 PM

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?

#9 Luvac Zantor

Luvac Zantor
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 02 November 2003 - 08:58 PM

got it,


my query has to be one line. I just put all the query info into ONE line, and it appears to be working now.

:)

#10 Luvac Zantor

Luvac Zantor
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 02 November 2003 - 09:03 PM

ok, how do I query the \"next\" row in an array?

I tried mysql_fetch_row() but it failed....

#11 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 02 November 2003 - 09:49 PM

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 :)

#12 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 02 November 2003 - 09:51 PM

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

#13 Luvac Zantor

Luvac Zantor
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 02 November 2003 - 10:01 PM

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!!! :)

#14 Luvac Zantor

Luvac Zantor
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 02 November 2003 - 10:02 PM

the page is displaying multiple lines with NO stats showing.

#15 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 02 November 2003 - 10:04 PM

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.

#16 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 02 November 2003 - 10:06 PM

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

#17 Luvac Zantor

Luvac Zantor
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 02 November 2003 - 10:10 PM

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!!!

#18 Luvac Zantor

Luvac Zantor
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 02 November 2003 - 10:11 PM

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!!!!!

#19 DylanBlitz

DylanBlitz
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts
  • LocationOC Baby!

Posted 02 November 2003 - 10:12 PM

hehe glad you got it working.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users