WanknessHD Posted October 23, 2011 Share Posted October 23, 2011 I get this error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\user\user.php on line 5 code: user.php: <?php $get = (isset($_GET['id'])); //this means that user.php?id=1 would mean $get = 1. Note: This is not SQL Inject protected. $users = mysql_query("SELECT * FROM users WHERE id='".$get."'"); while ($row = mysql_fetch_array($users)) { echo ' Id = '.$row['id'].' Name = '.$row['name'].' Username = '.$row['username'].' Password = '.$row['password'].' Reg. on = '.$row['date'].' '; } ?> <html> <body> <form action='user.php' method='GET'> Username: <input type='text' value=''> <input type='submit' value='submit'> </form> <?php //what goes here? ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/249648-error-on-admin-look-up/ Share on other sites More sharing options...
Pikachu2000 Posted October 23, 2011 Share Posted October 23, 2011 mysql_fetch_array() takes the value of $users as its parameter, which comes from mysql_query(). mysql_query returns either a result resource on success, or a boolean FALSE on failure. The query is failing. Echo it and see if it contains the values you'd expect it to contain. Quote Link to comment https://forums.phpfreaks.com/topic/249648-error-on-admin-look-up/#findComment-1281559 Share on other sites More sharing options...
WanknessHD Posted October 23, 2011 Author Share Posted October 23, 2011 Sooo? echo while ($row = mysql_fetch_array($users)) Like that? im a noob. Quote Link to comment https://forums.phpfreaks.com/topic/249648-error-on-admin-look-up/#findComment-1281561 Share on other sites More sharing options...
WanknessHD Posted October 23, 2011 Author Share Posted October 23, 2011 SOrry for double post but i put a ";" after: while ($row = mysql_fetch_array($users)) and it showed: Id = Name = Username = Password = Reg. on = Quote Link to comment https://forums.phpfreaks.com/topic/249648-error-on-admin-look-up/#findComment-1281564 Share on other sites More sharing options...
Pikachu2000 Posted October 23, 2011 Share Posted October 23, 2011 No, remove the query string from the query execution. Form the query string in a variable so you can echo it along with any error returned by MySQL, like this. $query = "SELECT field FROM table"; $result = mysql_query($query) or die( "<br>Query: $query<br>Returned error: " . mysql_error() ) ; // continue with code . . . A couple other things I noticed are that there is no logic to prevent the query from being executed if $_GET['id'] has no value, and the assignment of the value to $get can only result in $get holding a boolean TRUE/FALSE value. So to address those 2 issues, and to validate that $_GET['id'] contains only numeric characters (as I assume it should) . . . $get = (isset($_GET['id'])); // Wrong way; this assigns the result of isset(), not the value of $_GET['id'] to $get // should be written as if( isset($_GET['id']) && ctype_digit($_GET['id']) ) { $get = $_GET['id']; // continue with DB query here } Quote Link to comment https://forums.phpfreaks.com/topic/249648-error-on-admin-look-up/#findComment-1281565 Share on other sites More sharing options...
WanknessHD Posted October 23, 2011 Author Share Posted October 23, 2011 I did what you said, and here is my code: <?php if( isset($_GET['id']) && ctype_digit($_GET['id']) ) { $get = $_GET['id']; $connect = mysql_connect("localhost","root","") or die("Couldnt Connect!"); mysql_select_db("phplogin") or die("Couldnt find database!"); $query = "SELECT * FROM users"; $result = mysql_query($query) or die( "<br>Query: $query<br>Returned error: " . mysql_error() ) ; while ($row = mysql_fetch_array($users)); { echo ' Id = '.$row['id'].' Name = '.$row['name'].' Username = '.$row['username'].' Password = '.$row['password'].' Reg. on = '.$row['date'].' '; } } ?> <html> <body> <form action='user.php' method='GET'> Username: <input type='text' value=''> <input type='submit' value='Get Info'> </form> <?php //what goes here? ?> </body> </html> and i still get error such as: error1: Notice: Undefined variable: users in C:\xampp\htdocs\user\user.php on line 9 error2: Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\user\user.php on line 9 Quote Link to comment https://forums.phpfreaks.com/topic/249648-error-on-admin-look-up/#findComment-1281567 Share on other sites More sharing options...
Pikachu2000 Posted October 23, 2011 Share Posted October 23, 2011 Your query result resource will be in $result instead of $users, so you'd need to change mysql_fetch_array($users) to mysql_fetch_array($result). Quote Link to comment https://forums.phpfreaks.com/topic/249648-error-on-admin-look-up/#findComment-1281569 Share on other sites More sharing options...
WanknessHD Posted October 23, 2011 Author Share Posted October 23, 2011 Now i just need to know how to get it to show the information... hah Quote Link to comment https://forums.phpfreaks.com/topic/249648-error-on-admin-look-up/#findComment-1281585 Share on other sites More sharing options...
xyph Posted October 23, 2011 Share Posted October 23, 2011 We really need to get this solution up as a sticky. fetch: Not a valid resource is like 50% of MySQL issues. It's so basic that I've got a copy/paste response in my /phpfreaks/ project folder Quote Link to comment https://forums.phpfreaks.com/topic/249648-error-on-admin-look-up/#findComment-1281589 Share on other sites More sharing options...
WanknessHD Posted October 23, 2011 Author Share Posted October 23, 2011 Can you help me then, ive got it with no errors, but when i type in "Austin" it dosent do anything, and the id to "Austin" is "4" so i go ahead and type in http://localhost/user/user.php?id=4 and it shows: Id = Name = Username = Password = Reg. on = But now i need it to accually show the information.. can you help with that? user.php: <?php if( isset($_GET['id']) && ctype_digit($_GET['id']) ) { $get = $_GET['id']; $connect = mysql_connect("localhost","root","") or die("Couldnt Connect!"); mysql_select_db("phplogin") or die("Couldnt find database!"); $query = "SELECT * FROM users"; $result = mysql_query($query) or die( "<br>Query: $query<br>Returned error: " . mysql_error() ) ; } ?> <html> <body> <form action='user.php' method='GET'> Username: <input type='text' value=''> <input type='submit' value='Get Info'> </form> <?php while ($row = mysql_fetch_array($result)); { echo ' Id = '.$row['id'].'<br> Name = '.$row['name'].'<br> Username = '.$row['username'].'<br> Password = '.$row['password'].'<br> Reg. on = '.$row['date'].'<br> '; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/249648-error-on-admin-look-up/#findComment-1281592 Share on other sites More sharing options...
xyph Posted October 23, 2011 Share Posted October 23, 2011 You have $result defined in an IF statement, and you later use it outside of that IF statement. What happens when that IF statement doesn't get executed? $result won't exist. Also, you haven't specified a WHERE clause in your query. This is something you should be able to find on your own. Quote Link to comment https://forums.phpfreaks.com/topic/249648-error-on-admin-look-up/#findComment-1281599 Share on other sites More sharing options...
PFMaBiSmAd Posted October 23, 2011 Share Posted October 23, 2011 You also have a terminating semi-colon ; on the end of your while() statement that does not belong there. Quote Link to comment https://forums.phpfreaks.com/topic/249648-error-on-admin-look-up/#findComment-1281630 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.