/    Sign up×
Community /Pin to ProfileBookmark

Search MySQL and display results

Using XAMPP – (latest version – basic – installer) on my computer running XPHome

I want to be able to test php scripts locally before going live on Web Server.

My Database – david
My Table – contacts
My Fields – id, first, last, phone, cell, fax, email, web

I want a search box on an html page or php page that I can type in the last (name) of a person and if there is a match in the “contacts” table then it will display all the fields for that person.

I have this set up already:
Indexes: last FULLTEXT None last

I have tried seven or eight tutorials so far and none of them have worked for me. I get all kind of error messages from T-string to unexpected this or that errors. I think most of the script tutorials were written for earlier versions of mysql and php, which may not be compatible with what I have. I don’t want to buy a search program for my database, but it’s kind of looking that way.

I tried using this but it displays all of the database:

This on a seperate html page that calls the script below


*******************************************

<form name=”form” action=”search.php” method=”get”>
<input type=”text” name=”$keyword” size=”45″ />
<input type=”submit” name=”Submit” value=”Search” />
</form>


*******************************************

The above calls this script

//search.php
<?
$username=”root”;
$password=”passwd”;
$database=”david”;
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);
$query = (“SELECT * FROM contacts WHERE last LIKE ‘$keyword%'”);
$result = mysql_query($query) or die(mysql_error());
$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,”first”);
$last=mysql_result($result,$i,”last”);
$phone=mysql_result($result,$i,”phone”);
$cell=mysql_result($result,$i,”cell”);
$fax=mysql_result($result,$i,”fax”);
$email=mysql_result($result,$i,”email”);
$web=mysql_result($result,$i,”web”);
?>

<center>
<table width=”90%” cellspacing=”2″ cellpadding=”0″>
<!–<tr><td colspan=”2″><font face=”Arial” size=”2″><? echo “$id”; ?></font></td></tr>–>
<tr>
<td align=”right” width=”10%><font face=”Arial” size=”3″>Name:</font></td>
<td width=”90%><font face=”Arial” size=”3″>&nbsp;&nbsp;<? echo “<b>$first $last</b>”; ?></font></td>
</tr>

<tr>
<td align=”right” width=”10%><font face=”Arial” size=”3″>Phone:</font></td>
<td width=”90%><font face=”Arial” size=”3″>&nbsp;&nbsp;<? echo “$phone”; ?></font></td>
</tr>

<tr>
<td align=”right” width=”10%><font face=”Arial” size=”3″>Cell:</font></td>
<td width=”90%><font face=”Arial” size=”3″>&nbsp;&nbsp;<? echo “$cell”; ?></font></td>
</tr>

<tr>
<td align=”right” width=”10%><font face=”Arial” size=”3″>Fax:</font></td>
<td width=”90%><font face=”Arial” size=”3″>&nbsp;&nbsp;<? echo “$fax”; ?></font></td>
</tr>

<tr>
<td align=”right” width=”10%><font face=”Arial” size=”3″>Email:</font></td>
<td width=”90%><font face=”Arial” size=”3″>&nbsp;&nbsp;<? echo “$email”; ?></font></td>
</tr>

<tr>
<td align=”right” width=”10%><font face=”Arial” size=”3″>Website:</font></td>
<td width=”90%><font face=”Arial” size=”3″>&nbsp;&nbsp;<? echo “$web”; ?></font></td>
</tr>

<tr><td style=”border-top:1px solid #666″ colspan=”2″>&nbsp;</td></tr>

</table>
</center>

<?
$i++;
}

?>

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@chazzyMay 23.2006 — you shouldn't use mysql_result like that.

without you posting the errors you get, can't really tell you what's going on.
Copy linkTweet thisAlerts:
@DavidAPauthorMay 23.2006 — With this particular html and script I don't get an error, I just get the entire table printed out. It acts like this:

$query="SELECT * FROM contacts";

I just want to pull up the rows where the last name matches what I put into the search box, like "Smith" for instance. If I only have one table entry with the last name of Smith, then only one record is returned and displayed. If five "Smiths", then five records displayed, etc.

The errors I was receiving was from the scripts in the tutorials.

Parse error: syntax error, unexpected T_STRING in C:Program Filesxampphtdocstestsearch.php on line 17

Line 17: echo("<p>You did not enter any search criteria, <a href="search.php">

In another:

Parse error: syntax error, unexpected $end in C:Program Filesxampphtdocstestsearchagain.php on line 71

Line 3: ?>

You delete this and it just moves up to the next thing above it and all the way up.

Many, many more errors.



***************

What's wrong with using mysql_result?
Copy linkTweet thisAlerts:
@chazzyMay 23.2006 — mysql_result is designed in you only want to deal with a small data set. since you don't know how large it's going to be, it's easier to use mysql_fetch_array, mysql_fetch_assoc, etc.

For one your query should be something like

<i>
</i>SELECT * FROM contacts WHERE last LIKE '%".$keyword."%';


But I'm a little curious to see if keyword is even being set.
[code=php]echo("The keyword is: ".$keyword);[/code]
Copy linkTweet thisAlerts:
@DavidAPauthorMay 28.2006 — [B]echo("The keyword is: ".$keyword);[/B]

I don't understand, what I'm suppose to do here.

Does this set the keyword? Where do I put it?
Copy linkTweet thisAlerts:
@DavidAPauthorMay 28.2006 — [B]you shouldn't use mysql_result like that[/B]

Do you mean I shouldn't use this here?

$first=mysql_result($result,$i,"first");

$last=mysql_result($result,$i,"last");

If a use [B]mysql_fetch_assoc[/B] would I change the above to this?

$first=mysql_fetch_assoc($assoc,$i,"first");

$last=mysql_fetch_assoc($assoc,$i,"last");

Would I have to change this as well?

$num=mysql_numrows($result); - to this $num=mysql_numrows($assoc);
Copy linkTweet thisAlerts:
@DavidAPauthorMay 28.2006 — When I use this

$first=mysql_fetch_array($result,$i,"first");

$last=mysql_fetch_array($result,$i,"last");

$phone=mysql_fetch_array($result,$i,"phone");

$cell=mysql_fetch_array($result,$i,"cell");

$fax=mysql_fetch_array($result,$i,"fax");

$email=mysql_fetch_array($result,$i,"email");

$web=mysql_fetch_array($result,$i,"web");

I get this warning:

Warning: Wrong parameter count for mysql_fetch_array() on line 16 - line 22.
Copy linkTweet thisAlerts:
@NogDogMay 29.2006 — mysql_fetch_*() functions return an entire result row as an array, and move the result row pointer to the next row. So to read one result row:
[code=php]
$row = mysql_fetch_assoc($result);
[/code]

In this example, $row would now be an associative array where the array keys are the column names in the query and the values are the corresponding values from the database:
[code=php]
echo "This row's last name value is: " . $row['last'];
[/code]

Since the mysql_fetch_assoc() function moves the result row pointer to the next row, and since it returns false if it has reached the end of the result set, you can loop through a mutliple-row query result with this pattern:
[code=php]
echo "<table>n";
while($row = mysql_fetch_assoc($result))
{
$first = $row['first'];
$last = $row['last'];
// etc....
echo "<tr><td>$first</td><td>$last</td></tr>n";
}
echo "</table>n";
[/code]
×

Success!

Help @DavidAP spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...