6 Replies - 3749 Views - Last Post: 10 October 2010 - 07:24 AM Rate Topic: -----

#1 flippin_insane  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-October 10

search form results php code error

Posted 09 October 2010 - 08:36 AM

Hi everyone. I'd like to ask some help as I am getting this error using my php code.

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/lis 160 database/order.php on line 68

this is the php code im currently working on and I'm trying to print results from a the comment column, in the same row as the query entered in the search bar. Can anyone please help me find my error?


<?php

				$connection = mysql_connect("localhost","root","") or die("Couldn't connect");

				mysql_select_db("saver") or die("Could not open database");

				$xquery = $_REQUEST['query'];
				
				$result = mysql_query("SELECT comment * FROM  shirt WHERE (name LIKE '$xquery') ");

				
				while($row = mysql_fetch_array($result))
				
					{
				 		echo $row['comment'] ;
					}

				?>


Is This A Good Question/Topic? 0
  • +

Replies To: search form results php code error

#2 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: search form results php code error

Posted 09 October 2010 - 09:29 AM

Try the script from here:
http://www.designpla...?page=1&c_id=25
Was This Post Helpful? 0
  • +
  • -

#3 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: search form results php code error

Posted 09 October 2010 - 10:44 AM

You are getting that error because there is something wrong with your query, most likely a syntax error. It is often helpful in these situations to store the query itself as a string so you can echo it out if you get an error :


$sql = "SELECT * from table";
$result = mysql_query($sql);




Your query is pretty simple, maybe remove the * so it is just
 "SELECT comment FROM shirt WHERE name LIKE '$xquery'"



If that doesn't work it could be something like trying to reference a field in the table that doesn't exist, try echoing out the $sql variable and running the query directly and see why kind of error mysql gives you.
Was This Post Helpful? 0
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: search form results php code error

Posted 09 October 2010 - 02:13 PM

That's exactly what it is. SELECT comment * is invalid syntax. The or die() thing suggested is something you will hear parroted on these forums constantly. It is gospel. Never simply assume your query is infallible.

Are you trying to select just the comment field, or all fields? Something worth thinking about when building your query.
Was This Post Helpful? 2
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: search form results php code error

Posted 10 October 2010 - 05:14 AM

There should be some Javascript added to this board (and every board that does PHP) which when detecting "expects parameter 1 to be a resource" in the text pops up a "HEY! DON'T ASSUME YOUR QUERY ALWAYS SUCCEEDS!!!" message with a complete explanation. Add the same functionality for "headers already sent by" messages and like 50% of PHP questions in forums would likely be fixed without wasting DB space on them.

Of course, that's also the function of the traditional FAQ, but who actually READS FAQs anymore? ;)
Was This Post Helpful? 1
  • +
  • -

#6 flippin_insane  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-October 10

Re: search form results php code error

Posted 10 October 2010 - 06:54 AM

Thanks a lot guys! I just had to remove the * and it worked!
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: search form results php code error

Posted 10 October 2010 - 07:24 AM

Cool! Now take all the information you received here regarding debugging SQL queries and use it in the future! There's also another problem here, and that's that you are taking the input and using it directly in your query which likely leaves you wide open for SQL injection attacks.

Here's the way I would do this:

<?php
$connection = mysql_connect("localhost","root","") or die("Couldn't connect");

mysql_select_db("saver") or die("Could not open database");

// $_REQUEST should probably be changed to be specific to
// either $_GET or $_POST, depending on its origin.
$xquery = mysql_real_escape_string($_REQUEST['query']);
               
$query = "SELECT comment from shirt WHERE (name LIKE '$xquery')"; 
$result = mysql_query($query);
if (!$result)
{
    // This should be logged to an error log rather than
    // using die() in a live, non-development environment
    die("Query $query failed: " . mysql_error());
}
               
while($row = mysql_fetch_array($result))
{
    echo $row['comment'] ;
}
?>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1