I'm currently trying to get into php
and MySQL
and i'm using this little project as a learning curve, but I've hit a bump that I can't get through. I have the following code that is accessing my MySQL database and I am currently trying to echo the data out using the following search function:
<?php
ob_start();
require("config.php");
ob_end_clean();
$req=$_REQUEST['workingDate'];
$req2=$_REQUEST['location'];
mysql_connect("XXXXXXXXXXX",$username,$password);
mysql_select_db($database) or die( "Unable to select database");
if ($req!="all" && $req2!="all") $query="SELECT * FROM TrackerTable WHERE workingDate='$req' AND location1='$req2'";
else if($req=="all" && $req2!="all" ) $query="SELECT * FROM TrackerTable WHERE location1='$req2'";
else if($req!="all" && $req2=="all" ) $query="SELECT * FROM TrackerTable WHERE make='$req'";
else if($req=="all" || $req2=="all" ) $query="SELECT * FROM TrackerTable";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_query($result);
mysql_close();
$i=0;
for ($i; $i < $num; $i++){
$f12=mysql_result($result,$i,"workingDate");
$f13=mysql_result($result,$i,"location1");
echo $f12." ".$f13."<br />";
}
?>
The problem I have is that whenever I try to search the database using the following html form:
<form method="post" action="searchFunction.php" name="input" id="searchform">
<label class="section">Search Options</label><br />
<input name="workingDate" type="text" id="workingDate" placeholder="Search by Date">
<input name="location1" type="text" id="location1" placeholder="Search by Location">
<input name="submit" type="submit" id="add" value="Find!">
</form>
I get this error thrown at me:
Warning: mysql_query() expects parameter 1 to be string, resource given in /XXXXXX/XXXXXX/XXXXX/XXXX/XXXXX.com/XXXXXX/searchFunction.php on line 20
I have no idea what could be causing the problem as it all seems fairly in place to me. Is there an obvious mistake i'm making or is it all just completely messed up? I've been looking at it that long that I can't tell what makes sense anymore!
Any help would be greatly appreciated. Thanks guys!
You are sure that variable $query is defined? This because in the statement if/elseif is where you define the variable $query.
// first define default value for your $query variable
$query="SELECT * FROM TrackerTable";
// then use your if/else statement
if ($req!="all" && $req2!="all") .....................
You have an error
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_query($result); // error here
mysql_close();
$result is not a string, is resource. mysql_query($result);
Check http://php.net/manual/en/function.mysql-query.php for details
You're trying to run your query twice:
$result=mysql_query($query);
^^^^^^--- your SQL
mysql_query($result);
^^^^^^^---- result handle (aka resource) from previous call
The second query call is utterly irrelevant/useless. You're passing in the wrong argument to the call, and you're not capturing the return value from the query anyways. Even if it was running properly, you're throwing away the query results.
You then close your DB connection, so when you try to call mysql_result()
later on, there's no more DB connection to fetch your results from.
And on top of all this, you're vulnerable to sql injection attacks.
In short, this code is a total disaster.