Jump to content

Error on Admin Look-up


WanknessHD

Recommended Posts

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>

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.