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'];

?>

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);

$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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.