Jump to content


Photo

mysql_fetch_array():


  • Please log in to reply
6 replies to this topic

#1 grrwood

grrwood
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 26 December 2005 - 11:54 AM

ok lets see if i can explain this well.

using a tutorial that can be found [a href=\"http://forum.overhauledpc.com/showthread.php?t=637\" target=\"_blank\"]here[/a] i compleated the 2 other tutorials no problem just added a line here and there but this one has got me.

the error is as follow

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\easyphp\www\testplatform\profile.php on line 10

using a link like this
[a href=\"http://localhost/testplatform/profile.php?u=1\" target=\"_blank\"]http://localhost/testplatform/profile.php?u=1[/a]

the script is

<?php

// Connect to the database
require("config.php");
// select database
// Get users information

$result = mysql_query("SELECT * FROM `users` WHERE `userid` = $u LIMIT 1");

[b]while($r=mysql_fetch_array($result)){[/b]

// Turn breaks into new lines for biography field

$r[biography] = nl2br($r[biography]);

// Output users information

echo "<b>Viewing $r[username]'s profile</b><br>";

echo "Biography: $r[biography]";

}

?>

any help in this would be greatly appreatiated. as i have looked thourgh several sites tring to find the answer.
then going through php databases with member listing to see how they pull up the members info.

apache 1.3.33
php 4.3.10
mysql 4.1.9

#2 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 26 December 2005 - 02:03 PM

try this:
<?php

// Connect to the database
require("config.php");
// select database
// Get users information

$result = mysql_query("SELECT * FROM `users` WHERE `userid` = $u LIMIT 1");

while($row = mysql_fetch_array($result)) {

// Output users information

echo "<b>Viewing {$row['username']}'s profile</b><br />";

echo nl2br("Biography: {$row['biography']}");

}

?>


#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 26 December 2005 - 03:47 PM

The only thing I can see is that you're not explicitly passing mysql_query() a link identifier, and since I don't see any code above line 10 that makes a DB call, it's possible that you're not connecting to the server properly. I bet if you check the value of $result after the query it will be FALSE, leading the error that you are reporting. Otherwise, $result should be a valid resource, and there should be no issues with the code above.

Hope that helps.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#4 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 26 December 2005 - 06:32 PM

Have you tried $u = $_GET['u'] before passing it to the query?
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#5 grrwood

grrwood
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 26 December 2005 - 07:23 PM

thanks for the help i really appretiate it

added the line
  if (!$result) {
    echo("<P>Error performing query: " .
         mysql_error() . "</P>");
    exit();
  }


Error performing query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1

so im going to keep reading hopefully something come up

#6 LazyJones

LazyJones
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 26 December 2005 - 07:29 PM

[!--quoteo(post=330466:date=Dec 26 2005, 02:23 PM:name=grrwood)--][div class=\'quotetop\']QUOTE(grrwood @ Dec 26 2005, 02:23 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
thanks for the help i really appretiate it

added the line
  if (!$result) {
    echo("<P>Error performing query: " .
         mysql_error() . "</P>");
    exit();
  }
Error performing query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1

so im going to keep reading hopefully something come up
[/quote]

I'm sure the problem is that variable $u is not set. Try it out by signing some value to it before query


#7 grrwood

grrwood
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 26 December 2005 - 08:42 PM

you guys here in the forums are gods thank you.
this works but like i butt head i could get it to work till i remembered the ;
duh, oh well noob mistake
$u = $_GET['u'];

ended up leaving the code like this

<?php
// Connect to the database
require("config.php");
// select database
// Get users information
$u = $HTTP_GET_VARS['u'];
$result = mysql_query("SELECT * FROM `users` WHERE `userid` = $u LIMIT 1");
  if (!$result) {
    echo("<P>Error performing query: " .
         mysql_error() . "</P>");
    exit();
  }
while($r=mysql_fetch_array($result)){
// Turn breaks into new lines for biography field
$r[biography] = nl2br($r[biography]);
// Output users information
echo "<b>Viewing $r[username]'s profile</b><br>";
echo "Biography: $r[biography]";
}
?>

thanks again for the help you guys rock.
and i will be sure to read more on _GET




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users