displaying data from my database

This is a discussion on "displaying data from my database" within the PHP Forum section. This forum, and the thread "displaying data from my database are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Jun 13th, 2006, 18:00
Junior Member
Join Date: Jun 2006
Location: usa
Age: 29
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation displaying data from my database

I want to display all the info that is associated with categories but for some reason is not going though i need some help please

PHP: Select all

 
<?php
$host 
"localhost";
$username "localhost";
$password "abc123";
$database "contacts";
$db mysql_connect($host$username$password);
mysql_select_db($database);
if(isset(
$_GET['Categories']))
  {
    
$Categories addslashes($_GET['Categories']);
    }else{
    echo(
"No Categories found.");
    exit;
}

  
$query "SELECT *FROM `Categories` WHERE `Categories` LIKE '%".$Categories."%'";
  
$result mysql_query($query) or die (mysql_error());
  
$num_result mysql_num_rows($result);

if(
$num_result <= 0)
  {
    echo(
"No Results found.");
    exit;
}
  echo 
"<p>Number of business found: ".$num_result."</p>";
while(
$row mysql_fetch_array($result))
     {
     echo 
"<p><strong>BusinessName  ";
     echo (
$row["Slogan"]);
     echo 
"</strong><br>Address ";
     echo (
stripslashes($row["Business_Address"]));
     echo 
"<br>ISBN: ";
     echo (
stripslashes($row["Make"]));
     echo 
"<br>State: ";
     echo (
stripslashes($row["type"]));
      echo 
"<br>Price: ";
     echo (
stripslashes($row["Tel"]));
     echo 
"</p>";
}
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jun 13th, 2006, 18:55
Most Reputable Member
Join Date: Aug 2005
Location: North Wales, United Kingdom
Age: 21
Posts: 1,093
Thanks: 0
Thanked 0 Times in 0 Posts
Re: displaying data from my database

SELECT *FROM
U forgot the space


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jun 15th, 2006, 11:39
Aug Aug is offline
New Member
Join Date: Jun 2006
Location: Vegas
Age: 25
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Re: displaying data from my database

The typo sypher mentioned was the only syntax error I found. Is this still not working?

Also, you should consider your database naming convention.

A.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jun 15th, 2006, 12:13
Junior Member
Join Date: Jun 2006
Location: usa
Age: 29
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Angry Re: displaying data from my database

you think i should rename the columms in my database...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jun 15th, 2006, 12:34
Aug Aug is offline
New Member
Join Date: Jun 2006
Location: Vegas
Age: 25
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Re: displaying data from my database

Not because of programming but because you are printing a column named slogan with the heading BusinessName, Make as ISBN, Type as State, Price as Tel, it just seems very odd.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Jun 15th, 2006, 12:37
Junior Member
Join Date: Jun 2006
Location: usa
Age: 29
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Re: displaying data from my database

ok, i made changes to that already but it is still not workin,

PHP: Select all

 
<?php
$host 
"localhost";
$username "localhost";
$password "abc123";
$database "contacts";

trim($Categories,$make,$type);
if(!
$Categories||!$make||$type)
$Categories addslashes ($Categories);
$make addslashes ($make);
$type addslashes ($type);
{
  echo 
"You have not enter all fields";
  exit;
  }

$db mysql_connect($host$username$password);
mysql_select_db($database);
if(isset(
$_GET['Categories']))
  {
    
$Categories addslashes($_GET['Categories']);
    }
    else
    {
    echo(
"No Categories found.");
    exit;
}

  
$query "SELECT * FROM `Categories` WHERE `Categories` LIKE '%".$Categories."%'";
  
$result mysql_query($query) or die (mysql_error());
  
$num_result mysql_num_rows($result);

if(
$num_result <= 0)
  {
    echo(
"No Results found.");
    exit;
}
  echo 
"<p>Number of business found: ".$num_result."</p>";
while(
$row mysql_fetch_array($result))
     {
     echo 
"<p><strong>BusinessName  ";
     echo (
stripslahes($row["<strong>BusinessNama"]));
     echo (
$row["Slogan"]);
     echo 
"</strong><br>Address ";
     echo (
stripslashes($row["Business_Address"]));
     echo 
"<br>State ";
     echo (
stripslashes($row["Make"]));
     echo 
"<br>City ";
     echo (
stripslashes($row["type"]));
      echo 
"<br>Tel ";
     echo (
stripslashes($row["Tel"]));
     echo 
"</p>";
}
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Jun 15th, 2006, 13:09
Aug Aug is offline
New Member
Join Date: Jun 2006
Location: Vegas
Age: 25
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Re: displaying data from my database

Code: Select all

if(!$Categories||!$make||$type)
$Categories = addslashes ($Categories);
$make = addslashes ($make);
$type = addslashes ($type);
{
  echo "You have not enter all fields";
  exit;
  }
This block of code doesn't make sense.
Are you trying to do this:

Code: Select all
$Categories = addslashes ($Categories);
$make = addslashes($make);
$type = addslashes($type);
if(!$Categories || !$make || !$type)
{
    echo 'You have not entered all fields.';
}
You also are setting $Categories a few lines later with:
Code: Select all
if(isset($_GET['Categories']))
{
   $Categories = addslashes($_GET['Categories']);
}
else
{
   echo("No Categories found.");
   exit;
}
I made a database with the rows as the html files attached shows (exported sqlyog).

I wrote this script to do what you wanted (I think) code was kinda disorderly.

Code: Select all
<?php
$hostAddr       = "localhost";
$dbUsername     = "username";
$dbPassword     = "password";
$dbName         = "database name";

if(isset($_GET['Categories']))
{
    $categories = trim(addslashes($_GET['Categories']));
}
else
{
    echo 'No Category Passed';
    exit;
}

$db = mysql_connect($hostAddr, $dbUsername, $dbPassword);
mysql_select_db($dbName);
$query = "SELECT * FROM Categories WHERE Categories LIKE '%$categories%'";
$result = mysql_query($query) or die (mysql_error());
$num_result = mysql_num_rows($result);

if($num_result == 0)
{
    echo("No Results found.");
    exit;
}

echo "<p>Number of business found: $num_result </p>";
while($row = mysql_fetch_assoc($result))
{
     echo '<p><strong>BusinessName (' . stripslashes($row['businessName']) . ')';
     echo '(' . $row['slogan'] . ')</strong><br />';
     echo 'Address (' . stripslashes($row['businessAddress']) . ')<br />';
     echo 'State (' . stripslashes($row['state']) . ')<br />';
     echo 'City (' . stripslashes($row['city']) . ')<br />';
     echo 'Tel (' . stripslashes($row['tel']) . ')<br /></p>';    
}
?>
It produced this output:
Code: Select all
Number of business found: 2 
BusinessName (test)(testttt1)
Address (123 test lane.)
State (nv)
City (las vegas)
Tel (702-555-55)

BusinessName (test2)(testttt)
Address (234 Test Str.)
State (NV)
City (Las Vegas)
Tel (702-555-55)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Jun 15th, 2006, 14:29
Junior Member
Join Date: Jun 2006
Location: usa
Age: 29
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Re: displaying data from my database

it is not going through, here is my html for

PHP: Select all

 
<body onload="initListGroup('vehicles', document.forms[0].make, document.forms[0].type, document.forms[0].model, 'cs')">

<
form action="freedisplay3.phpmethod="post">
<
table width="770" height="121" border="0" align="center" cellpadding="0" cellspacing="0">
 
  <
tr
    <
td height="386" colspan="4" class="buscadorstilo"><table width="770" border="0" cellspacing="0" cellpadding="0">
        <
tr
          <
td height="20" colspan="18" class="buscador-textopequeno"Search TownsFinder&#8482; 
            
with EstateCityCategory or Keyword (ZipArea Code, and more).</td>
        </
tr>
        <
tr
          <
td width="53" valign="middle" class="buscadortxt">State:</td>
          <
td colspan="3" valign="middle"
     <
select name="make" style="width:160px;"></select>
       </
td>
          <
td width="41" class="buscadortxt">City:</td>
          <
td colspan="3">
    
    <
select name="type" style="width:160px;"></select>
       </
td>
          <
td width="71" class="buscadortxt">Category:</td>
          <
td colspan="3"
    <
input name=testfield" type"Categories" size="12"/> </td>
         
    <td width="
68" class="buscadortxt">Keywords:</td>
          
    <td colspan="
4"><input name="textfield" type="Keywords" size="20" /></td>
    
          <td valign="
middle align="center"> <input type="submit" value="Search"></a
          </
td>
        </
tr>
        <
tr
          <
td height="10" colspan="18">&nbsp;</td>
        </
tr>
        <
tr
          <
td colspan="18" class="texto01Copy">Other Popular Cities: <a href="#">UK</a>, 
            <
a href="#">Germany</a>, <a href="#">Spain</a>, <a href="#">Latvia</a>, 
            <
a href="#">Brazil</a>, <a href="#">Australia</a>, <a href="#">Canada</a>, 
            <
a href="#">and more...</a></td>
        </
tr>
      </
table></td>
  </
tr>
  
  
 
  
  
</
table>
</
form
please help, is maybe is not grabbing the info i am typing in
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Jun 15th, 2006, 14:41
Junior Member
Join Date: Jun 2006
Location: usa
Age: 29
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Re: displaying data from my database

The message that i am getting is

No Category Passed, but i have data inside the database and i also have restaurant in the which is the category that i am looking for

please anymore subjections i dont know what to do anymore
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Jun 15th, 2006, 15:40
Aug Aug is offline
New Member
Join Date: Jun 2006
Location: Vegas
Age: 25
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Re: displaying data from my database

It looks like when you say
Code: Select all
<input name=testfield" type"Categories" size="12"/>
you mean to have
Code: Select all
<input name="Categories" size="12" />
If that is not what you mean you aren't passing Categories to anything.

You also need to make sure you code is reading in Categories as a Post since in your example you were doing a get so did I.
Just change

Code: Select all
$_GET['Categories'] to $_POST['Categories']
A.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Jun 15th, 2006, 15:49
Junior Member
Join Date: Jun 2006
Location: usa
Age: 29
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Re: displaying data from my database

i did that and nothing happend. so it seems it not passing categories..Why is that?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Jun 15th, 2006, 15:53
Most Reputable Member
Join Date: Aug 2005
Location: North Wales, United Kingdom
Age: 21
Posts: 1,093
Thanks: 0
Thanked 0 Times in 0 Posts
Re: displaying data from my database

If i was you i would re write the script. Doing the simple functions first of passing the category var and posting the info.

Ttest if it works, then do the extra functions one at a time, testing each one. Then you will know what the problem is.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Jun 15th, 2006, 15:59
Aug Aug is offline
New Member
Join Date: Jun 2006
Location: Vegas
Age: 25
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Re: displaying data from my database

Because I am lazy and didn't feel like doing anything more I changed the code to look like this:

Code: Select all
<?php

print_r($_POST);

if(isset($_POST['submit']))
{
    $hostAddr       = "localhost";
    $dbUsername     = "username";
    $dbPassword     = "password";
    $dbName         = "dbname";

    if(isset($_POST['Categories']))
    {
        $categories = trim(addslashes($_GET['Categories']));
    }
    else
    {
        echo 'No Category Passed';
        exit;
    }

    $db = mysql_connect($hostAddr, $dbUsername, $dbPassword);
    mysql_select_db($dbName);
    $query = "SELECT * FROM Categories WHERE Categories LIKE '%$categories%'";
    $result = mysql_query($query) or die (mysql_error());
    $num_result = mysql_num_rows($result);

    if($num_result == 0)
    {
        echo("No Results found.");
        exit;
    }

    echo "<p>Number of business found: $num_result </p>";
    while($row = mysql_fetch_assoc($result))
    {
         echo '<p><strong>BusinessName (' . stripslashes($row['businessName']) . ')';
         echo '(' . $row['slogan'] . ')</strong><br />';
         echo 'Address (' . stripslashes($row['businessAddress']) . ')<br />';
         echo 'State (' . stripslashes($row['state']) . ')<br />';
         echo 'City (' . stripslashes($row['city']) . ')<br />';
         echo 'Tel (' . stripslashes($row['tel']) . ')<br /></p>';    
    }
}
else
{
?>
<form action="" method="post">
<table width="770" height="121" border="0" align="center" cellpadding="0" cellspacing="0">
 
  <tr> 
    <td height="386" colspan="4" class="buscadorstilo"><table width="770" border="0" cellspacing="0" cellpadding="0">
        <tr> 
          <td height="20" colspan="18" class="buscador-textopequeno"> Search TownsFinder™ 
            with Estate, City, Category or Keyword (Zip, Area Code, and more).</td>
        </tr>
        <tr> 
          <td width="53" valign="middle" class="buscadortxt">State:</td>
          <td colspan="3" valign="middle"> 
     <select name="make" style="width:160px;"></select>
       </td>
          <td width="41" class="buscadortxt">City:</td>
          <td colspan="3">
    
    <select name="type" style="width:160px;"></select>
       </td>
          <td width="71" class="buscadortxt">Category:</td>
          <td colspan="3"> 
    <input name="Categories" size="12"/> </td>
         
    <td width="68" class="buscadortxt">Keywords:</td>
          
    <td colspan="4"><input name="textfield" type="Keywords" size="20" /></td>
    
          <td valign="middle align="center"> <input type="submit" name="submit" value="Search"></a> 
          </td>
        </tr>
        <tr> 
          <td height="10" colspan="18">&nbsp;</td>
        </tr>
        <tr> 
          <td colspan="18" class="texto01Copy">Other Popular Cities: <a href="#">UK</a>, 
            <a href="#">Germany</a>, <a href="#">Spain</a>, <a href="#">Latvia</a>, 
            <a href="#">Brazil</a>, <a href="#">Australia</a>, <a href="#">Canada</a>, 
            <a href="#">and more...</a></td>
        </tr>
      </table></td>
  </tr>
  
  
 
  
  
</table>
</form>  
<?
}
I added print_r($_POST) so you could see what was actually being sent. All I did was change the name of the input to Categories and give the Search button the name submit and it works for me. To test your script I would recommend putting print_r($_POST) at the top to see why it isn't working.

A.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!