Forum Moderators: coopster

Message Too Old, No Replies

Searching a Mysql database using php

         

onoff

4:32 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



Hi. I'm setting up a site where bands can add their own listings and I'm just looking put together a script so people can search the database of events (for a band)using php and display the results. Here's the script I put together but not working- any help would be much appreciated! Cheers

<? $dbcnx = mysql_connect('localhost', 'onoff', 'passwrd');//connect to server
if ($dbcnx){
echo('<p>connected successfully</p>');
}
if (!@mysql_select_db('blast_db')) {//connect to database
exit('<p>Unable to locate the event ' .
'database at this time.</p>');
}
if (mysql_select_db('blast_db')) {//connect to database
echo('<p>Database OK</p>');
}

$form_res = @$_GET['q'] ;
$trimmed = trim($var);
$result = mysql_query('SELECT * FROM leinster WHERE event LIKE "%$form_res%"');
while ($row = mysql_fetch_array($result)) {
echo '<b>' . date( 'jS F Y', $row['timestamp'] ) .'</b>';
echo '<p>' . $row['event'] . '</p>';
echo '<p>' . $row['details'] .'</p>';
}
?>

[edited by: onoff at 4:36 pm (utc) on Aug. 11, 2005]

jatar_k

4:35 pm on Aug 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what exactly is it doing? are there errors, not returning the proper data, or just doing nothing?

you could try some of the PHP library threads about PHP and MySQL [webmasterworld.com]

onoff

4:38 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



Basicly just does nothing! It connects to the database fine but doesn't display any results when searched

onoff

4:54 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



Had a look at the library and didn't find anything there in relation to my problem. I'm pretty much a total beginner at php so I'm not sure if I've left out something big in the code!

jatar_k

6:05 pm on Aug 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I was thinking of
Basics of extracting data from MySQL using PHP [webmasterworld.com]
Help developing MySQL search query [webmasterworld.com]

for your specific case I would add some troubleshooting [webmasterworld.com]

construct the query on a line by itself
echo your query to see if it is constructed correctly
you're using the wrong var in your query
get the error from mysql if there is one

$form_res = $_GET['q'];
$trimmed = trim($var);
$sql = "SELECT * FROM leinster WHERE event LIKE '%" . $trimmed . "%'";
echo '<p>query is: ',$sql;
$result = mysql_query($sql) or die ("<p>the query died: " . mysql_error());
while ($row = mysql_fetch_array($result)) {

try that

onoff

6:37 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



Still no luck with it I'm afraid!

I'm not sure if the problem is in getting the results from the database or just displaying them..

jatar_k

7:38 pm on Aug 11, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well those echo lines should output something

are you getting any output at all?

onoff

3:24 pm on Aug 13, 2005 (gmt 0)

10+ Year Member



It's displaying the query.

whoisgregg

4:24 pm on Aug 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What result do you get when you copy and paste that query into PHPMyAdmin or (if you have shell access) directly into MySQL? Does it throw any errors or does it return the expected result?

jatar_k

4:21 am on Aug 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sounds like it returns an empty set, if there was an error it would die and give the error message.

are you sure that it should be returing a row(s) for the search you are using?