Forum Moderators: coopster
This is my form: (url = /test-sog-form)
<form action="/test-sog" method="post">
<input type="text" /><br />
<input type="submit" value="search" />
</form>
This is my PHP: (url = /test-sog)
<?php
$search = mysql_real_escape_string($_POST["search"]);
$con = mysql_connect("localhost","grenzjob_grenzjo","#*$!");
if (!$con) { // checking of connection errors
die('Could not connect: ' . mysql_error());
}
else {
mysql_select_db("grenzjob_grenzjobber", $con);
$result = mysql_query("SELECT * from profile WHERE firstname LIKE '%$search%'");
if (mysql_num_rows($result) > 0) { // checking that there is a result
echo $result;
}
else {
echo "Sorry! Couldn't find match.";
}
}
?>
The result of entering anything in the search form is:
"Resource id #22"
What is wrong?
<input type="text" name="search"/><br />
Replace "echo $result;" with:
while ($row = mysql_fetch_assoc($result)) {
echo "<pre>"; print_r($row); echo "</pre>";
}
It will echo every row in array format. You can use "echo $row['column_name'];" to echo specific column
This is my expanded PHP code:
<?php
$city = mysql_real_escape_string($_POST["city"]);
$country = mysql_real_escape_string($_POST["country"]);
$education = mysql_real_escape_string($_POST["education"]);
$language = mysql_real_escape_string($_POST["language"]);
$branche = mysql_real_escape_string($_POST["branche"]);
$con = mysql_connect("#*$!","#*$!","#*$!");
if (!$con) { // checking of connection errors
die('Could not connect: ' . mysql_error());
}
else {
mysql_select_db("#*$!", $con);
$result = mysql_query("SELECT * from profile WHERE city LIKE '%$city%'
AND country LIKE '%$country%'
AND education LIKE '%$education%'
AND language LIKE '%$language%'
AND branche LIKE '%$branche%'");
if (mysql_num_rows($result) > 0) { // checking that there is a result
while ($row = mysql_fetch_assoc($result)) {
echo "<pre>"; print_r($row); echo "</pre>";
}
}
else {
echo "Sorry! Couldn't find advertiser.";
}
}
?>
The error message is:
« MODx Parse Error »
MODx encountered the following error while attempting to parse the requested resource:
« PHP Parse Error »
PHP error debug
Error: mysql_num_rows(): supplied argument is not a valid MySQL result resource
Error type/ Nr.: Warning - 2
File: /home/grenzjob/public_html/manager/includes/document.parser.class.inc.php(769) : eval()'d code
Line: 18
Parser timing
MySQL: 0.0074 s(3 Requests)
PHP: 0.0392 s
Total: 0.0466 s
Im not the best at PHP so I find it difficult to track down errors like these.
Hope someone can help again :-)
<?php
$city = mysql_real_escape_string($_POST["city"]);
$country = mysql_real_escape_string($_POST["country"]);
$education = mysql_real_escape_string($_POST["education"]);
$language = mysql_real_escape_string($_POST["language"]);
$branche = mysql_real_escape_string($_POST["branche"]);
$con = mysql_connect("localhost","#*$!","#*$!");
if (!$con) { // checking of connection errors
die('Could not connect: ' . mysql_error());
}
else {
mysql_select_db("#*$!", $con);
$result = mysql_query("SELECT * from profile WHERE city LIKE '%$city%'
AND branche LIKE '%$branche%'");
if (mysql_num_rows($result) > 0) { // checking that there is a result
while ($row = mysql_fetch_assoc($result)) {
echo "<pre>"; print_r($row); echo "</pre>";
}
}
else {
echo "Sorry! Couldn't find advertiser.";
}
}
?>
$result = mysql_query('SELECT *
FROM profile
WHERE city LIKE "%$city%"
AND country LIKE "%$country%"
AND education LIKE "%$education%"
AND language LIKE "%$language%"
AND branche LIKE "%$branche%"');