1

I am a php newbie and I am trying to get this form to work but I get an "error getting data" when I run my query. Any help on this would be fantastic!

Here is the html form code:

<html>
<head>

<title>Where to go play basketball?</title>
<style type="test/css">
table {
background-color: #FCF;
}
th {
width:150px;
text-align:left;
}
</style>

</head>

<body>
<h1>Venue Search</h1>
<form method="post" action="search.php">
<label> Search Criteria: <input type="text" name="term" /></label>
<input type="submit" name="submit" value="Submit" />
</form>
</body>

</html>

And here is search.php:

<?php
mysql_connect ("localhost", "root","*****", "baskt_venues")  or die (mysql_error());

$term = $_POST['term'];

$sql = mysql_query("SELECT * FROM complete_table WHERE country LIKE $term")or die('error       getting data');

while ($row = mysql_fetch_array($sql, MYSQLI_ASSOC)){
echo '<br/> Country: '.$row['country'];
echo '<br/> Date: '.$row['date'];
echo '<br/> Time: '.$row['time'];
echo '<br/><br/>';
}

?>

Thanks to everyone take the time to look at this.

2 Answers 2

1

You need to add quotes around $term in the SQL.

$sql = mysql_query("SELECT * FROM complete_table WHERE country LIKE '$term'") ...

You might also want to add % signs around it so the search returns countries containg the search criteria instead of exact matches.

$sql = mysql_query("SELECT * FROM complete_table WHERE country LIKE '%$term%'")

Edit:

Just realized that the fourth paramater to mysql_connect is not name of the database. You will need a call to mysql_select_db after connecting:

mysql_connect("localhost", "root","*****") or die(mysql_error()) mysql_select_db("baskt_venues") or die(mysql_error())

Sign up to request clarification or add additional context in comments.

2 Comments

The SQL seems fine now, but we need more info on the error. Try replacing or die('error getting data') with or die(mysql_error()) to se what the actual error is.
jlundqvist , @zukanta, thank you so much for the help, it works now. Thank you so much for the help on this! I really appreciate it!
0

Yes you need to add the % sign and the quotes but you also need to concatenate with the contents of $term so you don't compare with '$term'. So looks like you need something in the lines of :

"SELECT * FROM complete_table WHERE country LIKE '%" + $term + "'"

4 Comments

so now I have this ' <?php mysql_connect ("localhost", "root","password", "wcs_venues") or die (mysql_error()); $term = $_POST['term']; $sql = mysql_query("SELECT * FROM complete_table WHERE country LIKE '%" + $term + "'")or die('error getting data'); while ($row = mysql_fetch_array($sql, MYSQLI_ASSOC)){ echo '<br/> Country: '.$row['country']; echo '<br/> Date: '.$row['date']; echo '<br/> Time: '.$row['time']; echo '<br/><br/>'; } ?> ' But I am still getting 'error getting data' ... :(
Concatenation is not necessary since PHP will expand variables in duoble quoted strings.
Thanks jlundqvist, now you know I never used PHP before LOL! Gees some weird things some languages do...
OK the-arthemis, other humble suggestions: why don't you try running your query on this DB directly in MySql and see what the error is? if you don't get any, then you might not be hitting the right DB with your PHP...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.