GB_001 Posted December 22, 2007 Share Posted December 22, 2007 I keep getting an odd error with this code. <?php @mysql_connect("mysql.x10hosting.com", "gb_GB", "********") or die(mysql_error()); @mysql_select_db("gb_USERInfo") or die(mysql_error()); session_start(); //specify how many results to display per page $limit = 20; // Get the search variable from URL $var = @$_GET['q'] ; //trim whitespace from the stored variable $trimmed = trim($var); //separate key-phrases into keywords $trimmed_array = explode(" ",$trimmed); // check for an empty string and display a message. if ($trimmed == "") { $resultmsg = "<p>Search Error</p><p>Please enter a search...</p>" ; } // check for a search parameter if (!isset($var)){ $resultmsg = "<p>Search Error</p><p>We don't seem to have a search parameter! </p>" ; } // Build SQL Query for each keyword entered foreach ($trimmed_array as $trimm){ // EDIT HERE and specify your table and field names for the SQL query $query = "SELECT * FROM Ysers WHERE School/Office LIKE \"%$trimm%\" OR Location LIKE \"%$trimm%\" OR email LIKE \"%$trimm%\" ORDER BY field1 DESC" ; // Execute the query to get number of rows that contain search kewords $results= mysql_query($query); $row_num = mysql_numrows ($results); // next determine if 's' has been passed to script, if not use 0. // 's' is a variable that gets set as we navigate the search result pages. if (empty($s)) { $s=0; } // now let's get results. $query .= " LIMIT $s,$limit" ; $results = mysql_query ($query) or die ( "Couldn't execute query" ); $row= mysql_fetch_array ($results); } if($row_num_links_main == 0 && $row_set_num == 0){ $resultmsg = "<p>Search results for:" . $trimmed ."</p><p>Sorry, your search returned zero results</p>" ; } else{ // Make a MySQL Connection while($row = mysql_fetch_array($result)){ echo $row['firstname']; echo $row['School/Office']; echo $row['Location']; echo "<br />"; } } ?> Error: Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/gb/public_html/Friendsearch.php on line 32 Couldn't execute query Thankyou for your help, this is a great community. Quote Link to comment https://forums.phpfreaks.com/topic/82792-solved-mysql_num_rows-problem/ Share on other sites More sharing options...
phpSensei Posted December 22, 2007 Share Posted December 22, 2007 What error does this spit out ? <?php @mysql_connect("mysql.x10hosting.com", "gb_GB", "redman12345") or die(mysql_error()); @mysql_select_db("gb_USERInfo") or die(mysql_error()); session_start(); //specify how many results to display per page $limit = 20; // Get the search variable from URL $var = @$_GET['q'] ; //trim whitespace from the stored variable $trimmed = trim($var); //separate key-phrases into keywords $trimmed_array = explode(" ",$trimmed); // check for an empty string and display a message. if ($trimmed == "") { $resultmsg = "<p>Search Error</p><p>Please enter a search...</p>" ; } // check for a search parameter if (!isset($var)){ $resultmsg = "<p>Search Error</p><p>We don't seem to have a search parameter! </p>" ; } // Build SQL Query for each keyword entered foreach ($trimmed_array as $trimm){ // EDIT HERE and specify your table and field names for the SQL query $query = "SELECT * FROM Ysers WHERE School/Office LIKE \"%$trimm%\" OR Location LIKE \"%$trimm%\" OR email LIKE \"%$trimm%\" ORDER BY field1 DESC" ; // Execute the query to get number of rows that contain search kewords $results= mysql_query($query)or die (mysql_error()); $row_num = mysql_numrows ($results); // next determine if 's' has been passed to script, if not use 0. // 's' is a variable that gets set as we navigate the search result pages. if (empty($s)) { $s=0; } // now let's get results. $query .= " LIMIT $s,$limit" ; $results = mysql_query ($query) or die (mysql_error()); $row= mysql_fetch_array ($results); } if($row_num_links_main == 0 && $row_set_num == 0){ $resultmsg = "<p>Search results for:" . $trimmed ."</p><p>Sorry, your search returned zero results</p>" ; } else{ // Make a MySQL Connection while($row = mysql_fetch_array($result)){ echo $row['firstname']; echo $row['School/Office']; echo $row['Location']; echo "<br />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/82792-solved-mysql_num_rows-problem/#findComment-421043 Share on other sites More sharing options...
Dane Posted December 22, 2007 Share Posted December 22, 2007 $row_num = mysql_numrows ($results); should be $row_num = mysql_num_rows ($results); at line 32 Quote Link to comment https://forums.phpfreaks.com/topic/82792-solved-mysql_num_rows-problem/#findComment-421129 Share on other sites More sharing options...
GB_001 Posted December 22, 2007 Author Share Posted December 22, 2007 Okay, so I redid the code and I still get the same error. <?php @mysql_connect("mysql.x10hosting.com", "gb_GB", "***********") or die(mysql_error()); @mysql_select_db("gb_USERInfo") or die(mysql_error()); session_start(); //specify how many results to display per page // Get the search variable from URL $var = @$_GET['Email'] ; $trim = trim($var); $array = explode(" ",$trim); foreach ($array as $array){ if ($trim == "") { $resultmsg = "<p>Search Error</p><p>Please enter an email...</p>" ; } $query = "SELECT * FROM Ysers WHERE email LIKE \"%$array%\" ORDER BY email DESC" ; $results=mysql_query ($query) or die (mysql_error()); $row_num =mysql_num_rows ($numresults) or die (mysql_error());; $numresults = mysql_query ($query) or die ( "Couldn't execute query" ); $row= mysql_fetch_array ($numresults); do{ //EDIT HERE and specify your field name that is primary key $adid_array[] = $row[ 'id' ]; }while( $row= mysql_fetch_array($numresults)); } //end foreach $tmparr = array_unique($adid_array); $i=0; foreach ($tmparr as $v) { $newarr[$i] = $v; $i++; } while($row = mysql_fetch_array( $numresult ) or die (mysql_error())) { // Print out the contents of each row echo $row['name']." - ".$row['email']. "<br />"; } Error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/gb/public_html/Friendsearch.php on line 24 Quote Link to comment https://forums.phpfreaks.com/topic/82792-solved-mysql_num_rows-problem/#findComment-421352 Share on other sites More sharing options...
GingerRobot Posted December 22, 2007 Share Posted December 22, 2007 You're passing the wrong variable to the mysql_num_rows() function. It should be be $results, not $numresults. Same goes for call to mysql_fetch_array(): $query = "SELECT * FROM Ysers WHERE email LIKE \"%$array%\" ORDER BY email DESC" ; $results=mysql_query ($query) or die (mysql_error()); $row_num =mysql_num_rows ($results); $row= mysql_fetch_array ($results); Dane: FYI, mysql_numrows() is an alias for mysql_num_rows(), though it is depreciated. Quote Link to comment https://forums.phpfreaks.com/topic/82792-solved-mysql_num_rows-problem/#findComment-421354 Share on other sites More sharing options...
GB_001 Posted December 22, 2007 Author Share Posted December 22, 2007 Thankyou, I have one more error though. I get an unexpected end error on line 41. Quote Link to comment https://forums.phpfreaks.com/topic/82792-solved-mysql_num_rows-problem/#findComment-421368 Share on other sites More sharing options...
darkfreaks Posted December 22, 2007 Share Posted December 22, 2007 please comment line 41 so we can find the error thank you Quote Link to comment https://forums.phpfreaks.com/topic/82792-solved-mysql_num_rows-problem/#findComment-421371 Share on other sites More sharing options...
GB_001 Posted December 22, 2007 Author Share Posted December 22, 2007 [code][code]<?php @mysql_connect("mysql.x10hosting.com", "gb_GB", "********") or die(mysql_error()); @mysql_select_db("gb_USERInfo") or die(mysql_error()); session_start(); $var = @$_GET['Email'] ; $trim = trim($var); $array = explode(" ",$trim); foreach ($array as $array){ if ($trim == "") { $resultmsg = "<p>Search Error</p><p>Please enter an email...</p>" ; } $query = "SELECT * FROM Ysers WHERE email LIKE \"%$array%\" ORDER BY email DESC" ; $results=mysql_query ($query) or die (mysql_error()); $row_num =mysql_num_rows ($results); $row= mysql_fetch_array ($results); $numresults = mysql_query ($query) or die ( "Couldn't execute query" ); do{ $adid_array[] = $row[ 'id' ]; }while( $row= mysql_fetch_array($numresults)); $tmparr = array_unique($adid_array); $i=0; foreach ($tmparr as $v) { $newarr[$i] = $v; $i++; } while($row = mysql_fetch_array( $numresult ) or die (mysql_error())) { echo $row['name']." - ".$row['email']. "<br />"; } ?> //Line 41 [/code][/code] Quote Link to comment https://forums.phpfreaks.com/topic/82792-solved-mysql_num_rows-problem/#findComment-421375 Share on other sites More sharing options...
darkfreaks Posted December 22, 2007 Share Posted December 22, 2007 there was no ending bracket i added it goodluck <?php @mysql_connect("mysql.x10hosting.com", "gb_GB", "********") or die(mysql_error()); @mysql_select_db("gb_USERInfo") or die(mysql_error()); session_start(); $var = @$_GET['Email'] ; $trim = trim($var); $array = explode(" ",$trim); foreach ($array as $array){ if ($trim == "") { $resultmsg = "<p>Search Error</p><p>Please enter an email...</p>" ; } $query = "SELECT * FROM Ysers WHERE email LIKE \"%$array%\" ORDER BY email DESC" ; $results=mysql_query ($query) or die (mysql_error()); $row_num =mysql_num_rows ($results); $row= mysql_fetch_array ($results); $numresults = mysql_query ($query) or die ( "Couldn't execute query" ); do{ $adid_array[] = $row[ 'id' ]; }while( $row= mysql_fetch_array($numresults)); $tmparr = array_unique($adid_array); $i=0; foreach ($tmparr as $v) { $newarr[$i] = $v; $i++; } while($row = mysql_fetch_array( $numresult ) or die (mysql_error())) { echo $row['name']." - ".$row['email']. " "; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/82792-solved-mysql_num_rows-problem/#findComment-421379 Share on other sites More sharing options...
GB_001 Posted December 23, 2007 Author Share Posted December 23, 2007 Thankyou so much. My script has no more errors, but due to cruel fate stopped working. So I decided to start over with something simple and that didn't work. <?php @mysql_connect("mysql.x10hosting.com", "gb_GB", "************") or die(mysql_error()); @mysql_select_db("gb_USERInfo") or die(mysql_error()); session_start(); $var = $_POST['email'] ; $trim = trim($var); $earray = explode(" ",$trim); $query = "SELECT * FROM Ysers WHERE email LIKE '%$earray%'"; $result = mysql_query($query); echo $trim; while($row = mysql_fetch_array( $result )) { echo $row['email']; echo"<br/>"; } ?> It displays no errors. Quote Link to comment https://forums.phpfreaks.com/topic/82792-solved-mysql_num_rows-problem/#findComment-421448 Share on other sites More sharing options...
papaface Posted December 23, 2007 Share Posted December 23, 2007 Remove the @ signs then it WILL display errors! Quote Link to comment https://forums.phpfreaks.com/topic/82792-solved-mysql_num_rows-problem/#findComment-421449 Share on other sites More sharing options...
GB_001 Posted December 23, 2007 Author Share Posted December 23, 2007 Sorry, that did not seem to work. Quote Link to comment https://forums.phpfreaks.com/topic/82792-solved-mysql_num_rows-problem/#findComment-421451 Share on other sites More sharing options...
papaface Posted December 23, 2007 Share Posted December 23, 2007 Add: error_reporting(E_ALL); to the top and try again. Quote Link to comment https://forums.phpfreaks.com/topic/82792-solved-mysql_num_rows-problem/#findComment-421452 Share on other sites More sharing options...
papaface Posted December 23, 2007 Share Posted December 23, 2007 The problem is that you're putting an array into a WHERE ??? You need to specify which element it is in the array: e.g $query = "SELECT * FROM Ysers WHERE email LIKE '%{$earray[0]}%'"; Quote Link to comment https://forums.phpfreaks.com/topic/82792-solved-mysql_num_rows-problem/#findComment-421454 Share on other sites More sharing options...
GB_001 Posted December 23, 2007 Author Share Posted December 23, 2007 Thankyou for all your help, but when I search it brings up every email in the database. Quote Link to comment https://forums.phpfreaks.com/topic/82792-solved-mysql_num_rows-problem/#findComment-421496 Share on other sites More sharing options...
GB_001 Posted December 24, 2007 Author Share Posted December 24, 2007 Bumped. Quote Link to comment https://forums.phpfreaks.com/topic/82792-solved-mysql_num_rows-problem/#findComment-422140 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.