Page 1 of 1

Help with PHP search function (Array won't take data?)

Posted: Fri May 15, 2009 12:04 pm
by TheBrandon
Hello all,

I'm sure this has a simple answer, but I'm kinda stumped.

Right now I'm just making a simple search form to look for a member name, email or number.

Here is an example of my SQL:

Code: Select all

 
-- phpMyAdmin SQL Dump
-- version 2.9.1.1
-- http://www.phpmyadmin.net
-- 
-- Host: localhost
-- Generation Time: May 15, 2009 at 11:54 AM
-- Server version: 5.0.27
-- PHP Version: 5.2.0
-- 
-- Database: `login`
-- 
 
-- --------------------------------------------------------
 
-- 
-- Table structure for table `users`
-- 
 
CREATE TABLE `users` (
  `id` smallint(255) NOT NULL AUTO_INCREMENT,
  `username` varchar(30) NOT NULL,
  `password` varchar(32) DEFAULT NULL,
  `userid` varchar(32) DEFAULT NULL,
  `userlevel` tinyint(1) UNSIGNED NOT NULL,
  `memberlevel` tinyint(10) NOT NULL,
  `email` varchar(50) DEFAULT NULL,
  `timestamp` int(11) UNSIGNED NOT NULL,
  `current` tinyint(2) NOT NULL DEFAULT '0',
  `member_num` varchar(5000) character SET utf8 collate utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY  (`id`),
  KEY `username` (`username`,`password`,`userid`,`userlevel`,`memberlevel`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=14 ;
 
-- 
-- Dumping data for table `users`
-- 
 
INSERT INTO `users` (`id`, `username`, `password`, `userid`, `userlevel`, `memberlevel`, `email`, `timestamp`, `current`, `member_num`) VALUES 
(1, 'admin', '21232f297a57a5a743894a0e4a801fc3', '47c33449153e7357cf2a1bdee8eb8d79', 11, 8, 'BrandonPence@Gmail.com', 1242406345, 1, '655321'),
(3, 'thebrandon', '1ffc9f9cc376341aa08bb5973c511ac3', '77d98ba8d64c8f41ede3259608a7da78', 1, 1, 'BrandonPence@Gmail.com', 1238697180, 1, '658'),
(4, 'brandon', '3b2dd09db3d4442052fe6b920658979e', '9153dfe3ae871f32f70ddfe9082a9874', 6, 4, 'BrandonPence@Gmail.com', 1242400116, 0, NULL),
(5, 'callcenter', '3b2dd09db3d4442052fe6b920658979e', '8a5d3690973d7521f935c6ec32a1b279', 10, 1, 'BrandonPence@Gmail.com', 1242055196, 1, '1315'),
(6, 'reznor', '3b2dd09db3d4442052fe6b920658979e', 'ebf2404918e6a2c415f7857d3f032eed', 1, 1, 'brandonpence@gmail.com', 1242400795, 0, NULL),
(11, 'ihateartists', '3b2dd09db3d4442052fe6b920658979e', 'ca3a6283ac1ea34b4cad2bd8b6ebe7b2', 0, 2, 'Brandon@ReviveMediaServices.com', 1241113423, 1, '666'),
(12, 'chairperson', '3b2dd09db3d4442052fe6b920658979e', '41c3da59ea2888eee7906d9131a0c203', 12, 1, 'Brandon@ReviveMediaServices.com', 1242052951, 1, '327'),
(13, 'trentreznor', '3b2dd09db3d4442052fe6b920658979e', NULL, 0, 1, 'trentreznor@reznor.com', 0, 0, '');
 
And here is my PHP:

Code: Select all

 
<?php 
include("session.php");
?>
 
<html>
<head>
<title>Login Script</title>
<?php  include("style.php");?>
</head>
<body>
 
<table border="2" width="100%">
<?php 
/*are we logged in?*/
if($session->logged_in){
 
    include("include/sitemenu.php");
    
    $string = $_POST['terms'];
    
    echo $string;
    echo '<hr/>';
    $sql = "SELECT * FROM users WHERE username LIKE '%$string%' OR email LIKE '%$string%' OR member_num LIKE '%$string%' ORDER BY id DESC";
    $query = mysql_query($sql) or die(mysql_error());
    $row_sql = mysql_fetch_assoc($query);
    $total = mysql_num_rows($query);
 
    echo $total;
    echo '<hr/>';
    $i=0;
    
    if($total){
        
        echo 'Total is good! (Line 39)<hr/>';
        
        if($total>$i){echo 'Total>i<hr/>';}
        
        echo '<table border="2">';
        
        while ($total > $i) {
            
            $searcharray = array();
            
            $searcharray = mysql_fetch_assoc($query);  
            $name = $searcharray['username'];
            $email = $searcharray["email"];
            $num = $searcharray["member_num"];
            
            print_r($searcharray);
            
            echo '<tr><td>User Name: ';
            echo $name;
            echo '</td><td>User Email: ';
            echo $searchemail;
            echo '</td><td>Search Number: ';
            echo $searchnum;
            echo '</td></tr>';
            
            echo 'Loop completed! (line 57)<hr/>';
        
            $i++;
            
        }
    echo '</table>';
    } else
        {
        echo "No results to display";
    }
?>
<?php 
}else{
?>
</table>
<table border="2">
    <tr><td>Login</td></tr>
<?php 
/**
 * User not logged in, display the login form.
 * If user has already tried to login, but errors were
 * found, display the total number of errors.
 * If errors occurred, they will be displayed.
 */
if($form->num_errors > 0){
   echo "<font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font>";
}
?>
<form action="process.php" method="POST">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<?php  echo $form->value("user"); ?>"></td><td><?php  echo $form->error("user"); ?></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<?php  echo $form->value("pass"); ?>"></td><td><?php  echo $form->error("pass"); ?></td></tr>
<tr><td colspan="2" align="left"><input type="checkbox" name="remember" <?php  if($form->value("remember") != ""){ echo "checked"; } ?>>
<font size="2">Remember me next time &nbsp;&nbsp;&nbsp;&nbsp;
<input type="hidden" name="sublogin" value="1">
<input type="submit" value="Login"></td></tr>
<tr><td colspan="2" align="left"><br><font size="2">[<a href="forgotpass.php">Forgot Password?</a>]</font></td><td align="right"></td></tr>
<tr><td colspan="2" align="left"><br>Not registered? <a href="register.php">Sign-Up!</a></td></tr>
</table>
</form>
 
<?php 
}
include("include/footer.php");?>
 
 
</td></tr>
</table>
 
 
</body>
</html>
 
Now here is what happens. If I search for "brandon" I will get a bunch of results.

If I search for "reznor" I will get 1 result.

If I search for "admin" I will get none, but the loop completes. It still says 1 result, and the "Loop completed! (line 57)" line executes. So... what am I missing? Why isn't it grabbing the data?

Re: Help with PHP search function (Array won't take data?)

Posted: Fri May 15, 2009 12:11 pm
by TheBrandon
Okay apparantly this line:
$row_sql = mysql_fetch_assoc($query);

Was causing the error.

But... why?