Demont Posted November 30, 2007 Share Posted November 30, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/ Share on other sites More sharing options...
pocobueno1388 Posted November 30, 2007 Share Posted November 30, 2007 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()); Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402521 Share on other sites More sharing options...
helraizer Posted November 30, 2007 Share Posted November 30, 2007 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()); Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402524 Share on other sites More sharing options...
Demont Posted November 30, 2007 Author Share Posted November 30, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402526 Share on other sites More sharing options...
helraizer Posted November 30, 2007 Share Posted November 30, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402530 Share on other sites More sharing options...
Demont Posted November 30, 2007 Author Share Posted November 30, 2007 I get a unexpectedT_VARIABLE Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402531 Share on other sites More sharing options...
helraizer Posted November 30, 2007 Share Posted November 30, 2007 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)) { } Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402535 Share on other sites More sharing options...
pocobueno1388 Posted November 30, 2007 Share Posted November 30, 2007 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402536 Share on other sites More sharing options...
helraizer Posted November 30, 2007 Share Posted November 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402540 Share on other sites More sharing options...
pocobueno1388 Posted November 30, 2007 Share Posted November 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402543 Share on other sites More sharing options...
Demont Posted November 30, 2007 Author Share Posted November 30, 2007 $sql = "SELECT * FROM `users` ORDER BY `admin` [u]DESC[/u]" What so I do with the DESC...? </noob> Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402546 Share on other sites More sharing options...
pocobueno1388 Posted November 30, 2007 Share Posted November 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402549 Share on other sites More sharing options...
dewey_witt Posted November 30, 2007 Share Posted November 30, 2007 <?php $id=$row["id"]; $gender=$row["gender"]; $race=$row["race"]; ?> change the " character to the ' character in all these elements. Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402553 Share on other sites More sharing options...
Demont Posted November 30, 2007 Author Share Posted November 30, 2007 But what if I just want to display the users who have the value set to '1'? Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402557 Share on other sites More sharing options...
pocobueno1388 Posted November 30, 2007 Share Posted November 30, 2007 But what if I just want to display the users who have the value set to '1'? Then change your query to this $sql = "SELECT * FROM `users` WHERE admin='1'"; Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402559 Share on other sites More sharing options...
br3nn4n Posted November 30, 2007 Share Posted November 30, 2007 ^^^ Select all from the table users where the admin value is 1 In case that wasn't clear Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402567 Share on other sites More sharing options...
helraizer Posted November 30, 2007 Share Posted November 30, 2007 $sql = "SELECT * FROM `users` ORDER BY `admin` [u]DESC[/u]" What so I do with the DESC...? </noob> it'd be $sql = "SELECT * FROM `users` ORDER BY `admin` DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402587 Share on other sites More sharing options...
marcus Posted November 30, 2007 Share Posted November 30, 2007 Ha. $sql = "SELECT * FROM `users` WHERE `admin`='1' ORDER BY `name` DESC"; Select all users with admin equal to one, order by username. Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402591 Share on other sites More sharing options...
Demont Posted November 30, 2007 Author Share Posted November 30, 2007 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."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402604 Share on other sites More sharing options...
teng84 Posted November 30, 2007 Share Posted November 30, 2007 http://www.w3schools.com/php/php_mysql_select.asp read.. Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402606 Share on other sites More sharing options...
Demont Posted November 30, 2007 Author Share Posted November 30, 2007 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) Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402617 Share on other sites More sharing options...
teng84 Posted November 30, 2007 Share Posted November 30, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402623 Share on other sites More sharing options...
Demont Posted November 30, 2007 Author Share Posted November 30, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402625 Share on other sites More sharing options...
pocobueno1388 Posted November 30, 2007 Share Posted November 30, 2007 Is the $id variable coming from the database? If it is, your forgetting to use $row. Change it to this echo "<td><a href='staff.php?cmd=bio&id={$row['id']}'>". $row['username'] . "</a></td>"; Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402626 Share on other sites More sharing options...
Demont Posted November 30, 2007 Author Share Posted November 30, 2007 Tht worked! Thanks Pocobueno Quote Link to comment https://forums.phpfreaks.com/topic/79490-solved-mysql_fetch_array-help/#findComment-402630 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.