jamesxg1 Posted March 14, 2009 Share Posted March 14, 2009 <?php error_reporting (E_ALL | E_NOTICE); require("include/database.php"); $s = @$_POST['search']; $search = trim($s); $sql = "SELECT * FROM `users` WHERE username, email LIKE '%$search%'"; $query = mysql_query($sql); $numrows = mysql_num_rows($numresults); while ($row = mysql_fetch_assoc($query)) { $username = $row['username']; $email = $row['email']; $count++ ; } if ($search == "") { echo "<p>Please enter a search...</p>"; exit; } if (!isset($var)) { echo "<p>We dont seem to have a search parameter.</p>"; exit; } if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: ' . $search . ' returned with no results.</p>"; } if (empty($s)) { $s=0; } if($numrows > 1){ $return = "results";} else{ $return = "result"; } echo "<p>Your search for ' . $var . ' returned $numrows $return.</p>"; $count = 1 + $s ; ?> Notice: Undefined variable: numresults in C:\xampp\htdocs\swap\search.php on line 15 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\swap\search.php on line 15 Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\swap\search.php on line 16 Please enter a search... Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/ Share on other sites More sharing options...
lonewolf217 Posted March 14, 2009 Share Posted March 14, 2009 $numrows = mysql_num_rows($numresults); should be $numrows = mysql_num_rows($query); not entirely sure about the other two Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784275 Share on other sites More sharing options...
jamesxg1 Posted March 14, 2009 Author Share Posted March 14, 2009 $numrows = mysql_num_rows($numresults); should be $numrows = mysql_num_rows($query); not entirely sure about the other two Sorry yes im so stupid lol i used another code i had already made and forgot to change that lol. . . Changed! lol, i have changed the var ($s) into $_POST and i now get . . . Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\swap\search.php on line 15 Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\swap\search.php on line 17 We dont seem to have a search parameter. Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784279 Share on other sites More sharing options...
lonewolf217 Posted March 14, 2009 Share Posted March 14, 2009 from a simple google search it seems you may not be connecting to your database properly. do you have something similar to mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect: " . mysql_error()); so you can see if there is an error connecting to the DB ? Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784281 Share on other sites More sharing options...
jamesxg1 Posted March 14, 2009 Author Share Posted March 14, 2009 from a simple google search it seems you may not be connecting to your database properly. do you have something similar to mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect: " . mysql_error()); so you can see if there is an error connecting to the DB ? <?php error_reporting (E_ALL | E_NOTICE); mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error()); $s = @$_POST['search']; $search = trim($s); $sql = "SELECT * FROM `users` WHERE username, email LIKE '%$search%'"; $query = mysql_query($sql); $numrows = mysql_num_rows($query); while ($row = mysql_fetch_assoc($query)) { $username = $row['username']; $email = $row['email']; $count++ ; } if ($search == "") { echo "<p>Please enter a search...</p>"; exit; } if (!isset($var)) { echo "<p>We dont seem to have a search parameter.</p>"; exit; } if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: ' . $search . ' returned with no results.</p>"; } if (empty($s)) { $s=0; } if($numrows > 1){ $return = "results";} else{ $return = "result"; } echo "<p>Your search for ' . $var . ' returned $numrows $return.</p>"; $count = 1 + $s ; ?> Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\swap\search.php on line 17 Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\swap\search.php on line 19 We dont seem to have a search parameter. Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784283 Share on other sites More sharing options...
lonewolf217 Posted March 14, 2009 Share Posted March 14, 2009 you connected to the server, but where are you opening the DB you want to work on ? for example: $connection = mysql_connect($myServer,$myUser,$myPass); mysql_select_db($myDB,$connection); $query = mysql_query("SELECT * from table"); Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784284 Share on other sites More sharing options...
jamesxg1 Posted March 14, 2009 Author Share Posted March 14, 2009 you connected to the server, but where are you opening the DB you want to work on ? for example: $connection = mysql_connect($myServer,$myUser,$myPass); mysql_select_db($myDB,$connection); $query = mysql_query("SELECT * from table"); <?php error_reporting (E_ALL | E_NOTICE); $server = "localhost"; $user = "root"; $pass = ""; $db = "swap"; $connection = mysql_connect($server,$user,$pass); mysql_select_db($db,$connection); $s = @$_POST['search']; $search = trim($s); $sql = "SELECT * FROM `users` WHERE username, email LIKE '%$search%'"; $query = mysql_query($sql); $numrows = mysql_num_rows($query); while ($row = mysql_fetch_assoc($query)) { $username = $row['username']; $email = $row['email']; $count++ ; } if ($search == "") { echo "<p>Please enter a search...</p>"; exit; } if (!isset($var)) { echo "<p>We dont seem to have a search parameter.</p>"; exit; } if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: ' . $search . ' returned with no results.</p>"; } if (empty($q)) { $q=0; } if($numrows > 1){ $return = "results";} else{ $return = "result"; } echo "<p>Your search for ' . $var . ' returned $numrows $return.</p>"; $count = 1 + $q ; ?> Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\swap\search.php on line 15 Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\swap\search.php on line 17 Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784290 Share on other sites More sharing options...
lonewolf217 Posted March 14, 2009 Share Posted March 14, 2009 another possibility is that your SQL query itself is failing. put an "or die() ..." after that as well, or just echo the SQL string and throw it into SQL manually to see if it is correct or, since $search is your only variable in the string, try to echo it prior to doing the query to see if it contains anything Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784291 Share on other sites More sharing options...
kittrellbj Posted March 14, 2009 Share Posted March 14, 2009 Edit: Also, I am not sure what your form looks like, or if you're PHP_SELF'ing it or calling it from another file. My solution is more helpful if you are using a PHP_SELF form. If this error is displaying and stopping the page from executing entirely, a check before querying the database might help. Database errors will often end the page even if they are not necessary for page execution. Try adding: if ($_POST['search'] { $s = @$_POST['search']; $search = trim($s); $sql = "SELECT * FROM `users` WHERE username, email LIKE '%$search%'"; $query = mysql_query($sql); $numrows = mysql_num_rows($query); while ($row = mysql_fetch_assoc($query)) { $username = $row['username']; $email = $row['email']; $count++ ; } } That will keep the query from executing unless it is called by a POST request from a form. Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784299 Share on other sites More sharing options...
Philip Posted March 14, 2009 Share Posted March 14, 2009 WHERE username, email LIKE '%$search%' is incorrect syntax (I believe) Changing $query = mysql_query($sql); to $query = mysql_query($sql) or die(mysql_error()); would have helped Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784300 Share on other sites More sharing options...
jamesxg1 Posted March 14, 2009 Author Share Posted March 14, 2009 WHERE username, email LIKE '%$search%' is incorrect syntax (I believe) Changing $query = mysql_query($sql); to $query = mysql_query($sql) or die(mysql_error()); would have helped <?php error_reporting (E_ALL | E_NOTICE); error_reporting(E_ALL | E_STRICT); error_reporting(E_ALL ^ E_NOTICE); require("include/database.php"); if ($_POST['search']) { $s = @$_POST['search']; $search = trim($s); $sql = "SELECT username, email FROM `users` WHERE username LIKE '%$search%'"; $query = mysql_query($sql) or die(mysql_error()); $numrows = mysql_num_rows($query) or die(mysql_error()); while ($row = mysql_fetch_array($query)) { $username = $row['username']; $email = $row['email']; $count++ ; } } if ($search == "") { echo "<p>Please enter a search...</p>"; exit; } if (!isset($var)) { echo "<p>We dont seem to have a search parameter.</p>"; exit; } if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: ' . $search . ' returned with no results.</p>"; } if (empty($q)) { $q=0; } if($numrows > 1){ $return = "results";} else{ $return = "result"; } echo "<p>Your search for ' . $var . ' returned $numrows $return.</p>"; $count = 1 + $q ; ?> it worked i have no errors but now all that is being displayed is "We dont seem to have a search parameter. " any ideas why it wont show any results ? Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784305 Share on other sites More sharing options...
kittrellbj Posted March 14, 2009 Share Posted March 14, 2009 Can you post your HTML form you are using to access this script? Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784307 Share on other sites More sharing options...
lonewolf217 Posted March 14, 2009 Share Posted March 14, 2009 if (!isset($var)) { echo "<p>We dont seem to have a search parameter.</p>"; exit; } I dont see $var set anywhere on the page so this is always going to be true. can you put the same query through SQL and see if it returns any results ? Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784309 Share on other sites More sharing options...
kittrellbj Posted March 14, 2009 Share Posted March 14, 2009 Above is good advise, also. $var is probably meant to be $search. Also, it's not going to return based on that (if $var is not declared, it is considered unset) and there is an exit command during that if statement. So, it goes into that if and ends because it is true. Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784312 Share on other sites More sharing options...
jamesxg1 Posted March 14, 2009 Author Share Posted March 14, 2009 Above is good advise, also. $var is probably meant to be $search. Also, it's not going to return based on that (if $var is not declared, it is considered unset) and there is an exit command during that if statement. So, it goes into that if and ends because it is true. <?php require("include/database.php"); if ($_POST['search']) { $s = @$_POST['search']; $search = trim($s); $sql = "SELECT username, email FROM `users` WHERE username LIKE '%$search%'"; $query = mysql_query($sql) or die(mysql_error()); $numrows = mysql_num_rows($query) or die(mysql_error()); while ($row = mysql_fetch_array($query)) { $username = $row['username']; $email = $row['email']; $count++ ; } } if($numrows > 1){ $return = "results";} else{ $return = "result"; } print "<p>Your search for $search returned $numrows $return.</p>"; print "$email<br>$username"; if ($search == "") { print "<p>Please enter a search...</p>"; exit; } if ($numrows == 0) { print "<h4>Results</h4>"; print "<p>Sorry, your search: $search returned with no results.</p>"; } ?> its 98% working now i just need it to show all the rows and not just one any help ? Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784313 Share on other sites More sharing options...
Philip Posted March 14, 2009 Share Posted March 14, 2009 while ($row = mysql_fetch_array($query)) { $username = $row['username']; $email = $row['email']; $count++ ; } if($numrows > 1){ $return = "results";} else{ $return = "result"; } print "<p>Your search for $search returned $numrows $return.</p>"; print "$email<br>$username"; That loop will only set the last row found. You need to either create an array, each array item being an array of the results, or print directly from the loop. $array = array(); while($row = mysql_fetch_array($query) $array[] = $row; $return = (($numrows>1) ? "results":"result"); // if there should be an s or not print "<p>Your search for $search returned $numrows $return.</p>"; // show string foreach($row as $k => $v) { // run loop through each array element echo 'Email: ',$v['email']; echo 'Name: ',$v['username'],"\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784319 Share on other sites More sharing options...
lonewolf217 Posted March 14, 2009 Share Posted March 14, 2009 like what kingphilip said .. right after your query, use the mysql_num_rows to find out if you have results or not ( > 0 ). If you have results, do your mysql_fetch_array loop and inside the loop print each of your results Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784321 Share on other sites More sharing options...
jamesxg1 Posted March 14, 2009 Author Share Posted March 14, 2009 while ($row = mysql_fetch_array($query)) { $username = $row['username']; $email = $row['email']; $count++ ; } if($numrows > 1){ $return = "results";} else{ $return = "result"; } print "<p>Your search for $search returned $numrows $return.</p>"; print "$email<br>$username"; That loop will only set the last row found. You need to either create an array, each array item being an array of the results, or print directly from the loop. $array = array(); while($row = mysql_fetch_array($query) $array[] = $row; $return = (($numrows>1) ? "results":"result"); // if there should be an s or not print "<p>Your search for $search returned $numrows $return.</p>"; // show string foreach($row as $k => $v) { // run loop through each array element echo 'Email: ',$v['email']; echo 'Name: ',$v['username'],"\n"; } <?php require("include/database.php"); if ($_POST['search']) { $s = @$_POST['search']; $search = trim($s); $sql = "SELECT username, email FROM `users` WHERE username LIKE '%$search%'"; $query = mysql_query($sql) or die(mysql_error()); $numrows = mysql_num_rows($query) or die(mysql_error()); $array = array(); while($row = mysql_fetch_array($query)) $array[] = $row; $return = (($numrows>1) ? "results":"result"); print "<p>Your search for $search returned $numrows $return.</p>"; foreach($row as $k => $v) { echo 'Email: ',$v['email']; echo 'Name: ',$v['username'],"\n"; } $username = $row['username']; $email = $row['email']; $count++ ; } ?> Your search for james returned 2 results. Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\swap\search.php on line 18 Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784322 Share on other sites More sharing options...
Philip Posted March 14, 2009 Share Posted March 14, 2009 Oops, my bad, change foreach($row as $k => $v) { to foreach($array as $k => $v) { Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784330 Share on other sites More sharing options...
WolfRage Posted March 14, 2009 Share Posted March 14, 2009 KingPhilip I like your use of the if-else block I just learned about that shortened format. <?php $return = (($numrows>1) ? "results":"result"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784355 Share on other sites More sharing options...
Philip Posted March 14, 2009 Share Posted March 14, 2009 I typically don't use it - I prefer using full syntax with braces. However, this application is a bit smaller than what I'm used to working on, so I don't think it will matter too much Quote Link to comment https://forums.phpfreaks.com/topic/149333-help-with-mysql-vars-and-querys/#findComment-784356 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.