Jump to content

help with mysql vars and querys :(


jamesxg1

Recommended Posts

<?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...

 

Link to comment
Share on other sites

$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.

 

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :( ?

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.