Jump to content


Photo

php mysql


  • Please log in to reply
3 replies to this topic

#1 ferellie

ferellie
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 03 March 2006 - 10:28 AM

I am using table with several fields which I display in a div no problem. I need to display all fields on a seperate page where the categories field contains the string web as an example. This is my code..

<?php

include "config.php";

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

// read data from database
$result = mysql_query("select * from $table WHERE categories LIKE %web% order by id desc limit $rows", $link)
or die ("Could not read data because ".mysql_error());
?>

AND TO DISPLAY IN A DIV

<?php
// print the data in a div
if (mysql_num_rows($result)) {
while ($qry = mysql_fetch_array($result)) {
?>

<div id="business">

<?php
print "$qry[business]<br>";
?>
</div>

<div id="businessdescription">
Business description:</div>
<?php
print "$qry[description]<br>";
?>

<div id="contactname">
Contact person:</div>

<?php
print "$qry[name]<br>";
?>

<div id="location">
Location:</div>

<?php
print "$qry[town]";
?>
&nbsp;
<?php
print "$qry[postcode]<br>";
?>

<div id="telephone">
Telephone:</div>

<?php
print "$qry[phone]<br>";
?>

<div id="website">
<a href="

<?php
print "$qry[website]";
?>

">Visit Company Website</a></div>

<div id="endseperator"></div>

<?php
}
}
mysql_close();
?>

MY CONFIG.PHP CONTENTS IS

<?php
$server = "localhost"; // server to connect to.
$database = "pages"; // the name of the database.
$db_user = "pages"; // mysql username to access the database with.
$db_pass = "pass"; // mysql password to access the database with.
$table = "denb"; // database table
$rows = 500; // the number of table rows to display
?>

I would really appreciate any help,

Thank you.



#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 03 March 2006 - 02:06 PM

since web is a string you have to encase it in quotes
$result = mysql_query("select * from $table WHERE categories LIKE '%web%' order by id desc limit $rows", $link)
or die ("Could not read data because ".mysql_error());

Ray




#3 ferellie

ferellie
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 03 March 2006 - 02:19 PM

[!--quoteo(post=351329:date=Mar 3 2006, 02:06 PM:name=craygo)--][div class=\'quotetop\']QUOTE(craygo @ Mar 3 2006, 02:06 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
since web is a string you have to encase it in quotes
$result = mysql_query("select * from $table WHERE categories LIKE '%web%' order by id desc limit $rows", $link)
or die ("Could not read data because ".mysql_error());

Ray
[/quote]


[!--quoteo(post=351329:date=Mar 3 2006, 02:06 PM:name=craygo)--][div class=\'quotetop\']QUOTE(craygo @ Mar 3 2006, 02:06 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
since web is a string you have to encase it in quotes
$result = mysql_query("select * from $table WHERE categories LIKE '%web%' order by id desc limit $rows", $link)
or die ("Could not read data because ".mysql_error());

Ray
[/quote]


thank you but how do I get the text from the input box to replace the text 'web' in my code?

Thanks

#4 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 03 March 2006 - 06:37 PM

If I am understanding you correctly. you have a search field that user fill in and when they select it you want to use that as your filter?!?!

if it is then use this
$web = $_POST['web'];

$result = mysql_query("select * from $table WHERE categories LIKE "%$web%" order by id desc limit $rows", $link)
or die ("Could not read data because ".mysql_error());

If you only have specefic catagories I would suggest making the catagories form field a pulldown menu so people will get the right info. Then instead of using LIKE you can use "="

Ray






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users