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.



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

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
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>";
}
?>
Reply With Quote

  #2 (permalink)  
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
Send a message via MSN to sypher
Re: displaying data from my database

SELECT *FROM
U forgot the space


Reply With Quote
  #3 (permalink)  
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.
Reply With Quote
  #4 (permalink)  
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...
Reply With Quote
  #5 (permalink)  
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.

Reply With Quote
  #6 (permalink)  
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>";
}
?>
Reply With Quote
  #7 (permalink)  
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)
Reply With Quote
  #8 (permalink)  
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
Reply With Quote
  #9 (permalink)  
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
Reply With Quote
  #10 (permalink)  
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.
Reply With Quote
  #11 (permalink)  
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?
Reply With Quote
  #12 (permalink)  
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
Send a message via MSN to sypher
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.
Reply With Quote
  #13 (permalink)  
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.
Reply With Quote
  #14 (permalink)  
Old Jun 15th, 2006, 16:01
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: displaying data from my database

I've looked through the various bits of code you have provided and noted the following problems:

Code: Select all
<input name=testfield" type"Categories" size="12"/>
The name attribute has a missing opening " and there is no such type as 'Categories'. As has already been pointed out maybe you meant that to be the name and the type should be 'text'
Code: Select all
<input name="textfield" type="Keywords" size="20" />
Similarly, no such type as 'Keywords'.

In case you didn't pick up from previous posts;
You have a method in your form of 'POST' but in your processing you are trying to pick up passed variables using $_GET.

With regard to your database naming convention, it's a very bad idea to vae a table named the same as the database and a column the same as the table. Also modify your variable names. It's very confusing code when you have something like:
Code: Select all
SELECT * FROM Categories WHERE Categories LIKE ‘%categories%’
Reply With Quote
  #15 (permalink)  
Old Jun 15th, 2006, 17:04
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

well, well, i made some changes asnf added the Post and this is what i am getting now

Array ( [make] => Massachusetts [type] => suv [Categories] => restaurant [textfield] => [submit] => Search ) No Category Passed

PHP: Select all

 
print_r
($_POST);
if(isset(
$_POST['submit']))

$host "localhost";
$username "localhost";
$password "abc123";
$database "contacts";
if(isset(
$_GET['Categories']))
{
    
$Categories trim(addslashes($_GET['Categories']));
}
else
{
    echo 
'No Category Passed';
    exit;
}
$db mysql_connect($host$username$password);
mysql_select_db($database);
$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['make']) . ')<br />';
     echo 
'City (' stripslashes($row['type']) . ')<br />';
     echo 
'Tel (' stripslashes($row['tel']) . ')<br /></p>';

any idea of what is really happening there
thank you
Reply With Quote
  #16 (permalink)  
Old Jun 15th, 2006, 17:35
Junior Member
Join Date: Jun 2006
Location: usa
Age: 29
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up Re: displaying data from my database

Ok, just for future reference the only thing that was wrong was that i have a GET instead of a POST, I changed it to Post and it works. I was just wondering what is the diffrent and when should use the GET
Reply With Quote
  #17 (permalink)  
Old Jun 15th, 2006, 17:57
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
Send a message via MSN to sypher
Re: displaying data from my database

GET uses the url string to fetch information from it (example www.domain.com?id=5)

POST passes the info through forms.
Reply With Quote
  #