Jump to content

Recommended Posts

I'm having trouble with this. I created a page and I keep getting Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/www/loria.freehostia.com/test/staff.php on line 19

 

This is my code...

9   //If cmd has not been initialized
10 if(!isset($cmd)) 
11 {
12  //display only admin users
13  $result = mysql_query("select * from users and order by admin"); 
14
15 echo "<center><table border='1'>
16 <tr>
17 <th>Staff</th>
18 </tr>";
19 while($row=mysql_fetch_array($result)) 
20 { 
21    //grab the user stats
22    $username=$row["username"];
23    $id=$row["id"];
24    $gender=$row["gender"];
25    $race=$row["race"];
26    $hair=$row["hair"];
27    $skin=$row["skin"];
28    $eyes=$row["eyes"];
29     
30	 //make the title a link
31	  echo "<td><a href='staff.php?cmd=show&id=$id'>".$row['$username']." </a></td>";
32	  echo "</tr></center>";
33      
34    }
35  }
36  if($_GET["cmd"]=="show")
37  {
38      $result = mysql_query($sql); 
39      echo "$username is $gender 'and' a $race, with $hair, $skin 'and' $eyes.";
40  }
41  ?>
42  <center><a href="staff.php">Back to staff</a></center>

Link to comment
https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/
Share on other sites

That means there is a problem with your query, and I do see an error in it.

 

Change it to

$result = mysql_query("select * from users order by admin")or die(mysql_error());

 

//to make it easier to view 
$result = mysql_query("SELECT * FROM `users` ORDER BY `admin`") or die("Could not run query because: ".mysql_error());

That got rid of the error, but the row I am calling won't show. ( http://loria.freehostia.com/test/staff.php )

 

Also, instead of ordering by 'admin' since I have it set to either 0, 1 (Zero not being an admin, 1 being an admin) so that it will only list the names by the value from the users table?

That got rid of the error, but the row I am calling won't show. ( http://loria.freehostia.com/test/staff.php )

 

Break it down.

 

$sql = "SELECT * FROM `users` ORDER BY `admin`"

$result = mysql_query($sql) or die("Could not run query because: ".mysql_error());

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

 

Try that, it should work.

 

Sam

I get a unexpectedT_VARIABLE

 

My bad:

 

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

 

I left out a closing bracket.

 

Hmm. Ohh!! We missed of the semicolon on $sql

 

so it should be

 

$sql = "SELECT * FROM `users` ORDER BY `admin`";

$result = mysql_query($sql) or die("Could not run query because: ".mysql_error());

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

Try

 

<?php

$sql = "SELECT * FROM `users` ORDER BY `admin` DESC"
$result = mysql_query($sql) or die("ERROR: ".mysql_error().'<br>With Query: '.$sql);

while ($row = mysql_fetch_array($result)) {
   echo $row['col'].'<br>';
}

?>

Try

 

<?php

$sql = "SELECT * FROM `users` ORDER BY `admin` DESC"
$result = mysql_query($sql) or die("ERROR: ".mysql_error().'<br>With Query: '.$sql);

while ($row = mysql_fetch_array($result)) {
   echo $row['col'].'<br>';
}

?>

 

Need a semi-colon on the end of the $SQL line.

 

Try

 

<?php

$sql = "SELECT * FROM `users` ORDER BY `admin` DESC"
$result = mysql_query($sql) or die("ERROR: ".mysql_error().'<br>With Query: '.$sql);

while ($row = mysql_fetch_array($result)) {
   echo $row['col'].'<br>';
}

?>

 

Need a semi-colon on the end of the $SQL line.

 

 

Oops, I copied and pasted that line from your code to start with ;) You started it, haha.

DESC goes along with the ORDER BY clause. You have the choice between DESC (going from highest value to lowest) or ASC (lowest to highest). Since you have 1's for admins and 0's for normal members, then you want to order by DESC so it will display all the 1's first, then the normal members.

Okay, I got that, but to go along with this script...I am trying to link the administrator names to thier I.D...But it isn't working...Here's my code.

 

 

//Display administrator biography
if($_GET['cmd']=='bio')
{
   $sql = "SELECT FROM users WHERE id=$id";
$result = mysql_query($sql);
   echo "$username has $eyes eyes, $hair hair, and $skin skin.";
}
?>

 

I've got the query all situated. What I am asking now, is why won't the script I just posted work?

 

(http://loria.freehostia.com/test/staff.php)

//Display administrator biography
if($_GET['cmd']=='bio')
{
    $sql = "SELECT FROM users WHERE id=$id"; // initialize you query but you dont have fields selected
$result = mysql_query($sql); // get the resource data and do the db query
// now you should fetc your query so you can out put that
    echo "$username has $eyes eyes, $hair hair, and $skin skin.";
}
?>

 

the answer you dont fetch it

@DEMONT i gave you a good link if you only look that link you soudnt be asking this issue

again go back to that link and you might know more about this

My fault...I posted the wrong section of the code...

 

<?php
while($row = mysql_fetch_array( $result ))
  {
  echo "<tr>";
  echo "<td><a href='staff.php?cmd=bio&id=$id'>". $row['username'] . "</a></td>";
  echo "<td>" . $row['race'] . "</td>";
  echo "<td>" . $row['class'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
?>

 

In there, you can see i have the username made into a link, which is supposed to have the $id of the clicked on name from the users table I specified, but the link(s) do not display the $id value.

 

I  have another script that I use to delete items with the same thing and it displayes the $id, this one however won't.

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.