Jump to content

( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource


tammana

Recommended Posts

Hi I am very new to php. i am just learning php.

I am trying to go to profile page from login page so i created a page process to fetch username and password.

but it is giving me an error ( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\project\process.php on line 12

i don't understand what does this error mean?

 

here is my code:

 

<?php

mysql_connect("localhost", "root", "");

mysql_select_db("project");

$result = mysql_query("SELECT id From users where username='".$_POST['username']."'" and "password='".$_POST['password']."'");

$row = mysql_fetch_array($result);

$_SESSION['userid']=$row['id'];

?>

Link to comment
Share on other sites

Your query is failing and returning a boolean value of FALSE to mysql_fetch_array(). Edit your code so the query string is in a variable, then you can echo both the query string and any errors returned from MySQL.

 

$query = "SELECT id FROM users WHERE username = '{$_POST['username']}' AND password = '{$_POST['password']}";
$result = mysql_query($query) or die( "<br>Query string: $query<br>Produced error: " . mysql_error() );
$row = mysql_fetch_array($result);

Link to comment
Share on other sites

$result = mysql_query("SELECT id From users where username='".$_POST['username']."'" and "password='".$_POST['password']."'");

If you look at the quotation marks you seem to cut the query off at this point: ...username']."'" and...

It should work if you write it like $result = mysql_query("SELECT id From users where username='".$_POST['username']."' AND password='".$_POST['password']."'");

 

If you plan to keep coding it's better to do like Pikachu said, assign the values from the form to variables and put the query in another variable.

Then you can echo all variables and see if they look right AND echo the query itself.

Also, after the query you can put a die clause with the sql error so you get a message saying what went wrong.

 

So like this:

$username = $_POST['username'];

$password = $_POST['password'];

$query = "SELECT id From users where username='$username' AND password='$password'";

$result = mysql_query($query) or die("Query failed".mysql_error());

 

If I messed that up it will tell you why.

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.