Jump to content


Photo

Very simple search script not working. I'm Baffled. **[SOLVED]**


  • Please log in to reply
13 replies to this topic

#1 Lyricsride

Lyricsride
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationQuebec

Posted 01 August 2006 - 03:48 AM

Hey guys, so i'm using a VTC to learn some php http://www.vtc.com/products/php.htm but the demonstration he gave in the video, doesn't work on my machine. I'm thinking maybe there's a problem with my php.ini settings, or the application in the video is changed in the new versions of php. Here's the code that doesn't seem to run right.

"searchbyname.html"
---------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<title>A Simple Search Script</title>
</head>
<body>
<h2> Search for a user</h2>
<br />
<form action="resultsbyname.php" method="post">
  Please enter the name, or part of the name, of the user you are seeking: <br />
  <input name="name" type="text" />
  <br />
  <input type="submit" value="search" />
</form>
</body>
</html>
---------------------------------------------------------------------------

"resultsbyname.php"
---------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Results of your search SIR</title>
</head>

<body>
<h2>Search Results</h2>
<br />

<?php
echo $name;
?>

<br />
<a href="searchbyname.html">Go back to the search page</a>
</body>
</html>
---------------------------------------------------------------------------

The two pages of code about seem to be exactly what he has. Any ideas? Thanks.

Lyricsride
Lyricsride

#2 Lyricsride

Lyricsride
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationQuebec

Posted 01 August 2006 - 03:49 AM

sorry, i guess i should have said what it meant to happen. The first page should pass the inputed user text to the second page, and display it ...
Lyricsride

#3 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 01 August 2006 - 04:02 AM

unless your register globals is set to on (usually set off by default on most servers), you'll need to put this (in php) before you use the variable names:

foreach($_POST as $key=>$value) {
$$key = $value;
}


#4 Lyricsride

Lyricsride
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationQuebec

Posted 01 August 2006 - 04:21 AM

Thanks for the quick reply! Worked great. I know using globals is not reccomended so I will certainly want to use this method then. Would you mind maybe explaining quickly how the code exactly works? Either way thanks for the quick reply, appreciate it.
Lyricsride

#5 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 01 August 2006 - 04:57 AM

Well I'm still a bit of a newb myself, and the reason I knew the answer to that is because I've scratched my head over the same problem before :P

But basically when you use a post method like that, in the following page, you can just type
$_POST[$variable_name]  to call each variable.

This foreach loop just goes through every item posted and assigns it to the appropriate variable.

Glad I could help! :)

#6 Lyricsride

Lyricsride
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationQuebec

Posted 01 August 2006 - 06:03 AM

Ah ok, that seems to make sense right enough.
For a "GET" form method, (instead of post) would you simply modify it to say
$_GET[$variable_YOURVARIABLE] i suppose?

Also, the
$_POST[$variable_name] part of the code is what calls the specific variable being passed right? Well, thanks for the help bud!  ;)
Lyricsride

#7 Lyricsride

Lyricsride
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationQuebec

Posted 01 August 2006 - 06:34 AM

Arg. Update:

I thought the problem i was having before, was the cause of another one in a slightly more complicated situation. Turns out it's not. The 'second' problem i'm having is this.

"searchbyname.html" [still same page as before]
---------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<title>A Simple Search Script</title>
</head>
<body>
<h2> Search for a user</h2>


<form action="resultsbyname.php" method="post">
  Please enter the name, or part of the name, of the user you are seeking:

  <input name="name" type="text" />


  <input type="submit" value="search" />
</form>
</body>
</html>
---------------------------------------------------------------------------


"resultsbyname.php"
---------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Results of your search SIR</title>
</head>

<body>
<h2>Search Results</h2>



<?php
foreach($_POST as $key=>$value) {
$$key = $value;
}

echo $name;
$db =      mysql_connect("localhost",root,314314);
  mysql_select_db("prototyping",$db);
$query =      "SELECT users.usr_fname
          FROM users
  WHERE name LIKE '%".$name."%'";
$result = mysql_query($query);

while($record = mysql_fetch_assoc($result)){
while(list($fieldname, $fieldvalue) = each($record)){
echo $fieldname.": <b>".$fieldvalue."</b><br />";
}
echo"<br />";
}
?>



<a href="searchbyname.html">Go back to the search page[/url] 
</body>
</html>
---------------------------------------------------------------------------

When i type in a search of some sort, the "echo $name;" works fine and repeats what i typed, but the search throws this error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in c:\server\Apache2.2\htdocs\learn_php_test\resultsbyname.php on line 22

Line 22 is the bolded red code. At first, I thought that this error was becasue $query failed to get $name which i though might casue $result to screw up, thus causing line 22 to fail since it used $result. But there's another problem apparently...

users.usr_fname is a varchar(50) cell, it currently only contains one record. When i ran a search "%jas%" in myPHPadmin this was the Query it used:

SELECT *
FROM `prototyping`.`users`
WHERE `usr_id` LIKE CONVERT( _utf8 '%%jas%%'
USING latin1 )
COLLATE latin1_swedish_ci
OR `usr_alias` LIKE CONVERT( _utf8 '%%jas%%'
USING latin1 )
COLLATE latin1_swedish_ci
OR `usr_fname` LIKE CONVERT( _utf8 '%%jas%%'
USING latin1 )
COLLATE latin1_swedish_ci
OR `usr_lname` LIKE CONVERT( _utf8 '%%jas%%'
USING latin1 )
COLLATE latin1_swedish_ci
OR `usr_joindate` LIKE '%%jas%%'
OR `usr_email` LIKE CONVERT( _utf8 '%%jas%%'
USING latin1 )
COLLATE latin1_swedish_ci
OR `usr_bio` LIKE CONVERT( _utf8 '%%jas%%'
USING latin1 )
COLLATE latin1_swedish_ci
OR `usr_location` LIKE CONVERT( _utf8 '%%jas%%'
USING latin1 )
COLLATE latin1_swedish_ci
LIMIT 0 , 30

---------

So that's about all the info i can think of giving... hopefully i'm not testing your knowledge -too- much =0
Lyricsride

#8 Lyricsride

Lyricsride
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationQuebec

Posted 02 August 2006 - 04:23 PM

bump. Still stumped..  ???
Lyricsride

#9 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 02 August 2006 - 04:30 PM

Change
$result = mysql_query($query);

to

$result = mysql_query($query) or die(mysql_error());

and tell us what the error is.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#10 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 02 August 2006 - 04:39 PM

:D, haha you beat me to the punch!

;)

#11 Lyricsride

Lyricsride
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationQuebec

Posted 03 August 2006 - 01:11 AM

:D Thanks for the reply -both- of you haha.
Ok, so this is what it said when i search using "searchbyname.php" page (source code above):

Unknown column 'name' in 'where clause'

Here is the page source again. I think the error is that my SQL syntax is searching my database for a column titled "name" instead of using the search string passed from the search in the previous page. That's the only thing that seems to make sense based on the error message...  ???
------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Results of your search SIR</title>
</head>

<body>
<h2>Search Results</h2>
<br />

<?php
$hostname_logon = "localhost"; 
$database_logon = "prototyping"; 
$username_logon = "root"; 
$password_logon = "314314";

//open database connection
$connections = mysql_connect($hostname_logon, $username_logon, $password_logon) or die("Unabale to connect to the database");

//select database
mysql_select_db($database_logon,$connections) or die ("Unable to select database!");

//specify how many results to display per page, loaded into a variable
$limit = 10;

//allows passing of variables from page-to-page (without using Global Variables On in PHP.ini)
foreach($_POST as $key=>$value) {
$$key = $value;
}

//tests to see if directly-above code is working right, it should output the search string typed in the previous page
echo $name;

$query = "SELECT users.usr_fname
FROM users
WHERE name LIKE '%".$name."%'";
$result = mysql_query($query) or die(mysql_error());
while($record = mysql_fetch_assoc($result)){
while(list($fieldname, $fieldvalue) = each($record)){
echo $fieldname.": <b>".$fieldvalue."</b><br />";
}
echo"<br />";
}
?>

<br />
<a href="searchbyname.html">Go back to the search page</a>
</body>
</html>
---------------------------------
Lyricsride

#12 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 03 August 2006 - 01:18 AM

Look at your query:

SELECT users.usr_fname FROM users WHERE name LIKE '%".$name."%'[code]

Do you have a column named "name"?  Apparently you don't (from the error), so you are probably wanting that to be:

[code]SELECT users.usr_fname FROM users WHERE usr_fname LIKE '%".$name."%'[code][/code][/code][/code]


#13 Lyricsride

Lyricsride
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationQuebec

Posted 03 August 2006 - 02:56 AM

Hitman... wow. Well first off. lol, you were spot on. I'm just shocked becasue i could of sworn i went over that query a million times, and was sure it was perfect. When you pointed out the error i immediatly thought to myself "OH CHRIST OF COURSE?!@$!%R FASD FAFASDA GFA >:(>:(>:(>:("

haha... well thanks -a lot- Hitman and the rest of you who posted replys. I think i learnt something today about paying attention more carefully to my code....  :-\
Lyricsride

#14 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 03 August 2006 - 05:17 AM

what dvd auther are you using for your course cheers.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users