Jump to content

PHP mysql_fetch_array


vytas

Recommended Posts

Because Anders isn't online i can't ask this question to him .But here it goes my code doesn't seem to work

// All my connection code$check = $_POST['check'];$result = mysql_query("SELECT user FROM members WHERE user= '$user'");while ($row = mysql_fetch_array($result)){echo $row['user'];}?>

And here's the site it's on PAGE, i based it on one of Andersmoen codes.So i don't know what's wrong

Link to comment
Share on other sites

I solved the problem Andersmoen changed the Row names .. and i forgot to put them in my query...Oops problem solved

Link to comment
Share on other sites

I got another problem know,My search doesn't show any results :)Ok here's the form and here's the PHP codeFORM

<form action"<?php $_SERVER['self'];?>"method="post"><p align="center"> Search for profile <br /><input type="text"value="PROFILE:"name="check"></p></form>

PHP CODE

$check = $_POST['check'];$result = mysql_query("SELECT bruker FROM Members WHERE bruker= '$user'");if (mysql_num_rows($result)==0){echo "<p align=\"center\">Ur search didn't turn out any results</p>";}while ($row = mysql_fetch_array($result)){echo $row['user'];}?>

Ok hope some1 can help me.

Link to comment
Share on other sites

$check = $_POST['check'];$result = mysql_query("SELECT bruker FROM Members WHERE bruker= '$user'");if (mysql_num_rows($result)==0){echo "<p align=\"center\">Ur search didn't turn out any results</p>";}while ($row = mysql_fetch_array($result)){echo $row['user'];}

You're comparing the column 'bruker' with the value $user, I guess it should be compared with $check instead...And you use the column 'user' in the while-loop but 'bruker' in the SQL.

Link to comment
Share on other sites

I edited my code but it still didn't work FORM

<form action"<?php $_SERVER['self'];?>"method="post"><p align="center"> Search for profile <br /><input type="text"value="PROFILE:" name="check" /> <input type="submit" name="submit" value="Search" /></p></form>

PHP CODE

$check = $_POST['check'];$result = mysql_query("SELECT bruker FROM Members WHERE bruker= '$check'");if (mysql_num_rows($result)==0){echo "<p align=\"center\">Ur search didn't turn out any results</p>";}while ($row = mysql_fetch_array($result)){echo "$row[bruker]";}

Hope u can solve it :) Anders and i can't find it out .

Link to comment
Share on other sites

First, make sure you have spaces in your attribute lists. All tag attributes need to be separated with spaces. You also left out an equal sign in the form tag and an "echo" in the php, and $_SERVER['self'] does not exist. So your form should look like this:

<form action="<?php echo $_SERVER['SCRIPT_FILENAME'];?> "method="post"><p align="center"> Search for profile <br /><input type="text" value="PROFILE:" name="check" /> <input type="submit" name="submit" value="Search" /></p></form>

Now debugging isn't all that difficult, but people seem to have problems with it. If a piece of code is not doing what you expect, then put in debugging statements to find out exactly what it's doing!

$check = $_POST['check'];echo "check: {$check}<br>";$result = mysql_query("SELECT bruker FROM Members WHERE bruker= '$check'");echo "sql: SELECT bruker FROM Members WHERE bruker= '{$check}'<br>";echo "number of rows: " . mysql_num_rows($result) . "<br>";if (mysql_num_rows($result)==0){echo "<p align=\"center\">Ur search didn't turn out any results</p>";}while ($row = mysql_fetch_array($result)){echo $row['bruker'];}

Adding debugging statements like these are the only way to know what your code is doing, so if you have a piece of code and it's not doing what you think it should be doing, adding statements like these are the #1 way I use to check what is going on. It's always the first step, and 99% of problems can be solved when you see what is actually happening.

Link to comment
Share on other sites

Click on Search then we get redirected to the index kinda weird.

Link to comment
Share on other sites

IfI type in Anders and press Search I get this

You searched for: Andersand you got 1 result(s).Results:Anders
Isn't that what it should show?
Link to comment
Share on other sites

Some of the most usefull inventions has be discovered by accident.Example: Rubber (on wheels etc.), pencilin, super-glue etc.:?)

Link to comment
Share on other sites

Just one more question...do you know how we can make it not so sensitive? Because if I search for anders or Anders, I get a result, but if I search for ander or Ander I get 0 results. I mean...I want it to be like, if it had been in a news site and you searched for "hello" and you'd get all the results that contain the word "hello", and if you search for hel* (had to take one letter away, or else it would be swearing, though I don't mean to swear, it was just to shorten down a word). It would still get textes that contain the word hello and not just hel*.

Link to comment
Share on other sites

It works but something looks weird now. Go to our site and search for "a". *link is here*Here's the code:

<form action="" method="post">Search for profile<br /><input type="text" name="check" /><input type="submit" name="submit" value="Search" /></form></div><?php$con = mysql_connect("localhost","username","password");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("db_name", $con);if (isset($_POST[submit])) {$check = $_POST['check'];echo "You searched for: {$check}";echo "<br />";$result = mysql_query("SELECT * FROM Members WHERE bruker LIKE '%$check%'");while ($row = mysql_fetch_array($result)) {echo "and you got " . mysql_num_rows($result) . " result(s).";echo "<br />";echo "<br />";echo "<b>Results:</b>";echo "<br />";echo "<a href=\"users/$row[id].php\"><i>{$check}</i></a>";}}else {echo "<div style='text-align: center;'>Search after other members in the search engine above</div>";}?>

Link to comment
Share on other sites

Well yeah, you have all of this in a loop:

while ($row = mysql_fetch_array($result)) {echo "and you got " . mysql_num_rows($result) . " result(s).";echo "<br />";echo "<br />";echo "<b>Results:</b>";echo "<br />";echo "<a href=\"users/$row[id].php\"><i>{$check}</i></a>";}

Link to comment
Share on other sites

You print the "and you ..." in the while-loop:

if (isset($_POST[submit])) {$check = $_POST['check'];echo "You searched for: {$check}";echo "<br />";$result = mysql_query("SELECT * FROM Members WHERE bruker LIKE '%$check%'");while ($row = mysql_fetch_array($result)) {echo "and you got " . mysql_num_rows($result) . " result(s).";echo "<br />";echo "<br />";echo "<b>Results:</b>";echo "<br />";echo "<a href=\"users/$row[id].php\"><i>{$check}</i></a>";}}

put it outside the loop instead:

if (isset($_POST[submit])) {$check = $_POST['check'];echo "You searched for: {$check} <br />";echo "<br /><br />";echo "<b>Results:</b>";echo "and you got " . mysql_num_rows($result) . " result(s).";$result = mysql_query("SELECT * FROM Members WHERE bruker LIKE '%$check%'");while ($row = mysql_fetch_array($result)) {echo "<br />";echo "<a href=\"users/$row[id].php\"><i>{$check}</i></a>";}}

Link to comment
Share on other sites

You print the "and you ..." in the while-loop:
if (isset($_POST[submit])) {$check = $_POST['check'];echo "You searched for: {$check}";echo "<br />";$result = mysql_query("SELECT * FROM Members WHERE bruker LIKE '%$check%'");while ($row = mysql_fetch_array($result)) {echo "and you got " . mysql_num_rows($result) . " result(s).";echo "<br />";echo "<br />";echo "<b>Results:</b>";echo "<br />";echo "<a href=\"users/$row[id].php\"><i>{$check}</i></a>";}}

put it outside the loop instead:

if (isset($_POST[submit])) {$check = $_POST['check'];echo "You searched for: {$check} <br />";echo "<br /><br />";echo "<b>Results:</b>";echo "and you got " . mysql_num_rows($result) . " result(s).";$result = mysql_query("SELECT * FROM Members WHERE bruker LIKE '%$check%'");while ($row = mysql_fetch_array($result)) {echo "<br />";echo "<a href=\"users/$row[id].php\"><i>{$check}</i></a>";}}

Now it looks like this when I search for "a":
You searched for: A Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/hotserv.dk/users/mafioza/profile.php on line 48and you got result(s).Results:AAA
Here's line 48:echo "and you got " . mysql_num_rows($result) . " result(s).";link again
Link to comment
Share on other sites

Sorry, missed that part.the value of $check is what the user typed in, to search for, not the found value.change

echo "<a href=\"users/$row[id].php\"><i>{$check}</i></a>";

to

echo "<a href=\"users/$row[id].php\"><i>".$row['bruker']."</i></a>";

and missed this too, guess I'm tired.

echo "and you got " . mysql_num_rows($result) . " result(s).";$result = mysql_query("SELECT * FROM Members WHERE bruker LIKE '%$check%'");

should be

$result = mysql_query("SELECT * FROM Members WHERE bruker LIKE '%$check%'");echo "and you got " . mysql_num_rows($result) . " result(s).";

Link to comment
Share on other sites

Eh, that's not the thing. Look here: Error message:"Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/hotserv.dk/users/mafioza/profile.php on line 50"and line 50 is this line:echo "and you got " . mysql_num_rows($result) . " result(s).";

Link to comment
Share on other sites

Ah, you were updating it :)I'll update this post when I've tried the codeNow it works perfect :)This is something I've been trying to make for ages just with HTML before I had PHP host and knew PHP :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...