[SOLVED] Help returning data from MySQL database...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Mark Bowen
Forum Newbie
Posts: 13
Joined: Thu Sep 30, 2004 4:38 am
Location: United Kingdom | Birmingham

Update on my needs :-)

Post by Mark Bowen »

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi there,

Just a quick update. It was me being silly! I managed to find a way to do partially what I wanted.
The code I am using is this :

Code: Select all

<?
$queryStart = "SELECT * FROM myDatabaseTable WHERE ";
echo $queryStart;
// Open our connection to the database....
$dbh = mysql_connect("localhost", "userName", "password");
mysql_select_db("databaseName");




// Set our query....
$sql = "SELECT * FROM myDatabase";
$result = mysql_query($sql) or die(mysql_error());

// Set up a $count variable....
$count = 0;
// create an array with field names we don't want to output
$excluded_fields = array('id', 'name');

while ($row = mysql_fetch_assoc($result)){
foreach ($row as $key => $value) {
// check that the $key (the field name) isn't listed in the $excluded_fields
// array so we only output fields we want
if (!in_array($key, $excluded_fields)) {


$output = $value . " LIKE " . $value . " AND ";
echo $output;
}
}

// increment $count by 1
$count++;
}

?>
The problems I am having though revolve around to things. The first being the part in the code where it says $value the second time it should use the value from the URL that is being used ie.

Code: Select all

http://www.mySite.com/searchDatabase.php?colour=red
but at the moment it is just using the same value of colour so that would be my first question and then secondly when it is looping through is is adding the " AND " to the end of each criteria but it then obviously adds it to the end of the last criteria aswell like this :

Code: Select all

"SELECT * FROM myDatabaseTable WHERE colour = colour AND type = type AND length = length AND"
so I would like to be able to somehow get rid of the last AND.

Really what I need is to be able to send in a URL in this format :

Code: Select all

http://www.mySite.com/searchData.php?colour=red&amp;type=wooden&amp;length=long
and then be able to create this SELECT query :

Code: Select all

"SELECT * FROM myDatabaseTable WHERE colour LIKE 'red%' AND type LIKE 'wooden%' AND length LIKE '$long%'"
but I just can't seem to get it working.

Any help with this would be massively appreciated.


EDIT
I managed to (I think) to fix the one problem by placing this line of code :

Code: Select all

echo "emptyField IS NULL";
at the end of the PHP file so that it creates a valid SELECT query as I have created a field called emptyField in the database with no data in it.
Could you let me know if this will create any problems seeing as how there isn't a field with that name when it comes to searching the database?
I still have the other problem though of passing in the values to the script.



Regards,

Mark Bowen


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Continues at: viewtopic.php?t=26479

Mac
Locked