Jump to content


Photo

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


  • Please log in to reply
2 replies to this topic

#1 adam_lougheed

adam_lougheed

    Newbie

  • New Members
  • Pip
  • 1 posts

Posted 25 April 2014 - 09:37 PM

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!

 

 



#2 bsmither

bsmither

    Advanced Member

  • Members
  • PipPipPip
  • 126 posts
  • LocationRocky Mountains

Posted 25 April 2014 - 10:01 PM

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.


Just waiting until my question gets answered.


#3 r3wt

r3wt

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts

Posted 25 April 2014 - 10:05 PM

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






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com