LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-15-2008, 12:10 PM   #1
s0l1dsnak3123
LQ Newbie
 
Registered: Jan 2008
Distribution: Ubuntu Hardy Heron
Posts: 18

Rep: Reputation: 0
[PHP] [mySQL] Finding all records with with a certain field.


hey there

I am having some problems with a PHP script I am developing.

The aim of the portion of the script I'm having problems with is to connect to a mySQL database, find a list of tables to search through, and then search for a piece of text under the field title of "Peripheral Type", with all the tables.

this is the portion I suspect is causing the problems:

PHP Code:
if ($_POST["product_type"]){ #search for product type, regardless of distro
    
$sql = @mysql_connect ($database_server$database_user$database_pass)
    or die (
$sql_conn_err_msg); #Connect to SQL Database
    
if (!@mysql_select_db($database_prefix $database_name$sql)) {   // leave the database                            
        
echo $sql_conn_err_msg;
    } 
    
$result mysql_query("SELECT * FROM dist_list"$sql);  #find all distros
    
while($row mysql_fetch_array($resultMYSQL_ASSOC))
    {
        if (
$distro == ""){ #make $distro a string with all the distros, seperated by whitespaces
            
$distro $row['Distro'];
        } else {
            
$distro $distro "####" $row['Distro'];
        }
    }
    
$distro explode("####",$distro);
    
mysql_free_result($result);
    
    
    
$per_type $_POST["product_type"]; #get product type
        
if (!@mysql_select_db($database_prefix $database_name$sql)) {   // leave the database                            
            
echo $sql_conn_err_msg;
        } 
    echo 
"<table border=\"1\" color=\"black\">"#create the start of the table
    
echo "<tr>";
    echo 
"<td><strong>Distribution</strong></td>";
    echo 
"<td><strong>Peripheral Type</strong></td>";
    echo 
"<td><strong>Peripheral Name</strong></td>";
    echo 
"<td><strong>Bugs/Limitations</strong></td>";
    echo 
"<td><strong>How to install</strong></td>";
    echo 
"<td><strong>Overall Happiness</strong></td>";
    echo 
"<td><strong>Stars</strong></td>";
    echo 
"<td><strong>Date</strong></td>";
    echo 
"</tr>";
    
$i=0;
    foreach (
$distro as $name){ #for each distro in the list
        
$result mysql_query("SELECT " $per_type " FROM " $distro[$i], $sql);  #query the product type on that record of distro
        
if (!$result) {
            echo 
$sql_conn_err_msg;
            exit;
        }
            while (
$row mysql_fetch_array($resultMYSQL_ASSOC)) { 
        echo 
"<tr>";
        echo(
"<td> " $distro[$i] . "</td>"); #print the distro
        
echo("<td> " $row["Peripheral Type"] . "</td>"); 
        echo(
"<td> " $row["Peripheral Name"] . "</td>"); 
        echo(
"<td> " $row["Bugs/Limitations"] . "</td>"); 
        echo(
"<td> " $row["How to install"] . "</td>"); 
        echo(
"<td> " $row["Overall Happiness"] . "</td>"); 
        if (
$row["stars"] == 1){
            echo(
"<td>");
            echo(
"<img src=\"Images/star.png\">");
            echo (
"</td>");
        } elseif (
$row["stars"] == 2){
            echo(
"<td>");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"/portal/Images/star.png\">");
            echo (
"</td>");
        } elseif (
$row["stars"] == 3){
            echo(
"<td>");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo (
"</td>");
        } elseif (
$row["stars"] == 4){
            echo(
"<td>");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo (
"</td>");
        } elseif (
$row["stars"] == 5){
            echo(
"<td>");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo (
"</td>");
            }
        echo(
"<td> " $row["Date"] . "</td>"); 
        echo 
"</tr>";
    }
        
$i++;
    }
    
$i=0;
    echo 
"</table>";
    
mysql_free_result($result);
    

for good measure, here is the source for this script:

PHP Code:

<?php
/* Allows you to view various aspects of the database */
include 'config.php'#include the config file

if ($_POST["distro"]){
    
$sql = @mysql_connect ($database_server$database_user$database_pass)
    or die (
$sql_conn_err_msg); #Connect to SQL Database
    
$distro_name $_POST["distro"];
    
    if (!@
mysql_select_db($database_prefix $database_name$sql)) {   // leave the database                            
        
echo $sql_conn_err_msg;
    } 
    
$result mysql_query("SELECT * FROM " $distro_name$sql); 
    
    if (!
$result) {
        echo 
$sql_conn_err_msg;
        exit;
    }
    
    echo 
"<table border=\"1\" color=\"black\">";
    echo 
"<tr>";
    echo 
"<td><strong>Distribution</strong></td>";
    echo 
"<td><strong>Peripheral Type</strong></td>";
    echo 
"<td><strong>Peripheral Name</strong></td>";
    echo 
"<td><strong>Bugs/Limitations</strong></td>";
    echo 
"<td><strong>How to install</strong></td>";
    echo 
"<td><strong>Overall Happiness</strong></td>";
    echo 
"<td><strong>Stars</strong></td>";
    echo 
"<td><strong>Date</strong></td>";
    echo 
"</tr>";
    while (
$row mysql_fetch_array($resultMYSQL_ASSOC)) { 
        echo 
"<tr>";
        echo(
"<td> " $distro_name "</td>");
        echo(
"<td> " $row["Peripheral Type"] . "</td>"); 
        echo(
"<td> " $row["Peripheral Name"] . "</td>"); 
        echo(
"<td> " $row["Bugs/Limitations"] . "</td>"); 
        echo(
"<td> " $row["How to install"] . "</td>"); 
        echo(
"<td> " $row["Overall Happiness"] . "</td>"); 
        if (
$row["stars"] == 1){
            echo(
"<td>");
            echo(
"<img src=\"Images/star.png\">");
            echo (
"</td>");
        } elseif (
$row["stars"] == 2){
            echo(
"<td>");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"/portal/Images/star.png\">");
            echo (
"</td>");
        } elseif (
$row["stars"] == 3){
            echo(
"<td>");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo (
"</td>");
        } elseif (
$row["stars"] == 4){
            echo(
"<td>");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo (
"</td>");
        } elseif (
$row["stars"] == 5){
            echo(
"<td>");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo (
"</td>");
            }
        echo(
"<td> " $row["Date"] . "</td>"); 
        echo 
"</tr>";
    }
    echo 
"</table>";
    
mysql_free_result($result);
    
mysql_close ($sql); #Close Connection with SQL Database
}

if (
$_POST["product_type"]){ #search for product type, regardless of distro
    
$sql = @mysql_connect ($database_server$database_user$database_pass)
    or die (
$sql_conn_err_msg); #Connect to SQL Database
    
if (!@mysql_select_db($database_prefix $database_name$sql)) {   // leave the database                            
        
echo $sql_conn_err_msg;
    } 
    
$result mysql_query("SELECT * FROM dist_list"$sql);  #find all distros
    
while($row mysql_fetch_array($resultMYSQL_ASSOC))
    {
        if (
$distro == ""){ #make $distro a string with all the distros, seperated by whitespaces
            
$distro $row['Distro'];
        } else {
            
$distro $distro "####" $row['Distro'];
        }
    }
    
$distro explode("####",$distro);
    
mysql_free_result($result);
    
    
    
$per_type $_POST["product_type"]; #get product type
        
if (!@mysql_select_db($database_prefix $database_name$sql)) {   // leave the database                            
            
echo $sql_conn_err_msg;
        } 
    echo 
"<table border=\"1\" color=\"black\">"#create the start of the table
    
echo "<tr>";
    echo 
"<td><strong>Distribution</strong></td>";
    echo 
"<td><strong>Peripheral Type</strong></td>";
    echo 
"<td><strong>Peripheral Name</strong></td>";
    echo 
"<td><strong>Bugs/Limitations</strong></td>";
    echo 
"<td><strong>How to install</strong></td>";
    echo 
"<td><strong>Overall Happiness</strong></td>";
    echo 
"<td><strong>Stars</strong></td>";
    echo 
"<td><strong>Date</strong></td>";
    echo 
"</tr>";
    
$i=0;
    foreach (
$distro as $name){ #for each distro in the list
        
$result mysql_query("SELECT " $per_type " FROM " $distro[$i], $sql);  #query the product type on that record of distro
        
if (!$result) {
            echo 
$sql_conn_err_msg;
            exit;
        }
            while (
$row mysql_fetch_array($resultMYSQL_ASSOC)) { 
        echo 
"<tr>";
        echo(
"<td> " $distro[$i] . "</td>"); #print the distro
        
echo("<td> " $row["Peripheral Type"] . "</td>"); 
        echo(
"<td> " $row["Peripheral Name"] . "</td>"); 
        echo(
"<td> " $row["Bugs/Limitations"] . "</td>"); 
        echo(
"<td> " $row["How to install"] . "</td>"); 
        echo(
"<td> " $row["Overall Happiness"] . "</td>"); 
        if (
$row["stars"] == 1){
            echo(
"<td>");
            echo(
"<img src=\"Images/star.png\">");
            echo (
"</td>");
        } elseif (
$row["stars"] == 2){
            echo(
"<td>");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"/portal/Images/star.png\">");
            echo (
"</td>");
        } elseif (
$row["stars"] == 3){
            echo(
"<td>");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo (
"</td>");
        } elseif (
$row["stars"] == 4){
            echo(
"<td>");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo (
"</td>");
        } elseif (
$row["stars"] == 5){
            echo(
"<td>");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo(
"<img src=\"Images/star.png\">");
            echo (
"</td>");
            }
        echo(
"<td> " $row["Date"] . "</td>"); 
        echo 
"</tr>";
    }
        
$i++;
    }
    
$i=0;
    echo 
"</table>";
    
mysql_free_result($result);
    
}

if (
$_POST["product_name"]){
    
    
}

if (
$_POST["product_stars"]){
    
    
}

if (
$_POST["product_date"]){
    
    
}
?>
Please help me as I really cannot tell what I am doing wrong.

Thanks in advance,
s0l1dsnak3123
 
Old 06-15-2008, 06:09 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,246

Rep: Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025
It would help if you told us what erorr msgs you are getting.
 
Old 06-15-2008, 08:26 PM   #3
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 31
My recommendation is to crank up your error reporting by adding error_reporting(E_ALL) at the top of the script so that you see any errors or warnings that might be generated. And as chrism01 said, you might also explain your problem. What's not working? What are you expecting to happen and what are you actually getting?
 
  


Reply

Tags
mysql, php


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql and purge old records Ammad Linux - Software 3 10-22-2012 05:59 AM
PHP not finding mysql tehfatal Linux - Server 1 04-02-2007 02:21 AM
PHP script to retrieve records from MYSQL db and pass them to an external program Julianus Programming 3 08-03-2006 02:09 PM
How to insert same text in one field for 10,000 records table edhan Linux - Newbie 6 06-13-2005 03:48 AM
Mysql/PHP query problem with datetime field. Pcghost Programming 2 11-11-2003 12:24 PM


All times are GMT -5. The time now is 06:41 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration