Jump to content

need help: warning mysql_fetch_array() expects parameter 1 to be resource, Boolean given


Recommended Posts

Hi everyone,

 

I was hoping I could get a little help or a push in the right direction, I am new to php, and still learning.

 

Currently am trying to develop a site that can add, edit, delete and search from my MYSQL database. with the add, edit, delete part I feel pretty confident and should not have a issue. I am just having trouble with the search function. I can manually search my database and display everything within that table, but now I am trying to make it so that I can enter a first name and all the data about that person in the table will display.

 

currently I have 2 php files (search.php): with a "input type: textbox" and submit button ( so I can type a first name and hit search) and I also have a (searchresult.php): which I want to search for the first name I entered and display the cust_id, first name, last name and email (which are populated in my customers table within my database).

 

currently I am getting an error of: " warning mysql_fetch_array() expects parameter 1 to be resource, Boolean given" , iv been reading up about the issue but still not sure what to do from here, and was hoping for some help with my code, and maybe a better ( dumbed down ) explanation of the error.

 

Below ill show my code for both pages:

 

Below is my "search.php" code: just a simple / basic html.

<!DOCTYPE HTML>

<html>
<head>
<title>Conxbiz - Search Customer</title>
<link rel="stylesheet" type="text/css" href="../conxbiz/css/main.css">
<link href='http://fonts.googleapis.com/css?family=Englebert|Nova+Square' rel='stylesheet' type='text/css'>
</head>

<body>

<form name="form" action="searchresult.php" method="get"> 
<input name="firstname" type="text"> 
<input type="submit" name="search" value="Search"> 
</form> 

<?php



?>


</body>
</html>


and below is my "searchresult.php" code:

<!DOCTYPE HTML>

<html>
<head>
<title>Conxbiz - Home</title>
<link rel="stylesheet" type="text/css" href="../conxbiz/css/main.css">
<link href='http://fonts.googleapis.com/css?family=Englebert|Nova+Square' rel='stylesheet' type='text/css'>
</head>

<body>

<?php

$firstname = $_GET['firstname'];

echo "<table>";
echo "<tr>";
echo "<th>cust_id</th>";
echo "<th>First Name</th>";
echo "<th>Last Name</th>";
echo "<th>Email</th>";	
echo "</tr>";


mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());
mysql_select_db("conxbiz");

$query = mysql_query("SELECT * FROM customers WHERE firstname '$firstname'");

while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) 
{
$cust_id = $row['cust_id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$email = $row['email'];

echo "<tr>";
echo "<td>" . $row['cust_id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}



echo "</table>";
?>	

</body>
</html>

so to re-cap, my aim is that I want to enter say "adam" into a textbox, hit search and have the php code pull up "cust_id, firstname,lastname and email" and display this to the screen.

 

I am guessing my coding is totally wrong :(  but hope someone could help me!

 

thank you for any help / guidance in advance!

 

 

mysql_query() will return either a resource or false (a boolean value). It will return false if, for some reason, the database server does not like the way you have expressed the SQL statement.

 

Please examine closely the SQL statement you are sendng the database server.

There's an error in your sql query, so $query = false because there was an error.

 

also i think you should use post instead of get.

 

also you are using insecure method of communication with the database. code is wide open to sql injection. my suggestion would be to read up on mysqli or pdo and learn to use them. they are a bit more difficult to get comfortable with but alot better. I myself prefer mysqli but most people use PDO

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.