Jump to content


Photo

stuck again


  • Please log in to reply
7 replies to this topic

#1 Renlok

Renlok
  • Members
  • PipPipPip
  • Advanced Member
  • 258 posts
  • Locationnewbury

Posted 29 September 2006 - 12:22 AM

<?php
$searchtype=$_POST['searchtype'];
$searchterm=$_POST['searchterm'];
$searchterm= trim($searchterm);

if (!$searchtype || !$searchterm)
{
echo 'You have not entered search details. Please go back and try again.';
exit;
}

if (!get_magic_quotes_gpc())
{
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}

@ $db = mysqli_connect('*', '*', '*', '*');
if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}

$query = "select * from link where ".$searchtype." like '%".$searchterm."@'";
$result = $db->query($query);

$num_results = $$result->num_rows;

echo '<p>Numbers of Links found '.$num_results.'</p>';

for ($i=0; $i <$num_results; $i++)
{
$row = $result->fetch_assoc();
echo '<p><b>'.($i+1).'. Site Name: ';
echo htmlspecialchars(stripslashes($row['siteName']));
echo '</b><br>URL: ';
echo stripslashes($row['url']);
echo '<br>Discription: ';
echo stripslashes($row['discription']);
echo '<br>Keywords: ';
echo stripslashes($row['keywords']);
echo '<br>Date Added: ';
echo stripslashes($row['dateAdded']);
}
mysqli_free_result($result);
$db->close();

?>
Is there any errors in this page anyone can see because when i run it, it comes up with a blank page

the code is in a html template if that makes any difference i dunno well thanks for any feedback.

#2 yonta

yonta
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 29 September 2006 - 12:52 AM

You have a typo here (notice the double $$ in result)

$num_results = $$result->num_rows;
do it, do it right, do it right now

#3 Renlok

Renlok
  • Members
  • PipPipPip
  • Advanced Member
  • 258 posts
  • Locationnewbury

Posted 29 September 2006 - 06:53 AM

thanks for that but it now retrives nothing from the database

it will just show 'Numbers of Links found 0' even if you enter an exact entery from the database.
i dont know what im doing wrong.

#4 yonta

yonta
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 29 September 2006 - 01:36 PM

Well, maybe it's the @ - shouldn't it be % ?

$query = "select * from link where ".$searchtype." like '%".$searchterm."@'";

Or maybe try this

$num_results  = mysqli_num_rows($result);

Other than that i don't see anything wrong. Check this page http://www.php.net/m...li-num-rows.php

do it, do it right, do it right now

#5 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 29 September 2006 - 01:55 PM

Why not give this a try?  I removed the '@' sign from the query, but if it was meant to be there (if you're searcing for a string followed by an @ sign) then just add it in before the last % in the query.

I've changed the query too, there's no need to escape normal variables when using double quotes " " as php translates them.

<?php
$searchtype=$_POST['searchtype'];
$searchterm=$_POST['searchterm'];
$searchterm= trim($searchterm);

if (!$searchtype || !$searchterm)
{
echo 'You have not entered search details. Please go back and try again.';
exit;
}

if (!get_magic_quotes_gpc())
{
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
}

$db = mysqli_connect('*', '*', '*', '*');
if (mysqli_connect_errno())
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}

$query = "select * from link where "$searchtype" like '%$searchterm%'";
$result = $db->query($query);

$num_results = $result->num_rows;

echo "<p>Numbers of Links found $num_results</p>";

for ($i=0; $i <$num_results; $i++)
{
$row = $result->fetch_assoc();
echo '<p><b>'.($i+1).'. Site Name: ';
echo htmlspecialchars(stripslashes($row['siteName']));
echo '</b><br>URL: ';
echo stripslashes($row['url']);
echo '<br>Discription: ';
echo stripslashes($row['discription']);
echo '<br>Keywords: ';
echo stripslashes($row['keywords']);
echo '<br>Date Added: ';
echo stripslashes($row['dateAdded']);
}
mysqli_free_result($result);
$db->close();

?>

If it doesn't work then post the errors back here.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#6 Renlok

Renlok
  • Members
  • PipPipPip
  • Advanced Member
  • 258 posts
  • Locationnewbury

Posted 29 September 2006 - 06:42 PM

thanks for the help it was just the error with the @ not being a %

now i have one last question. In the part of the code where it echos the data:
{
$row = $result->fetch_assoc();
echo '<p><b>'.($i+1).'. Site Name: ';
echo htmlspecialchars(stripslashes($row['siteName']));
echo '</b><br>URL: ';
echo stripslashes($row['url']);
echo '<br>Discription: ';
echo stripslashes($row['discription']);
echo '<br>Keywords: ';
echo stripslashes($row['keywords']);
echo '<br>Date Added: ';
echo stripslashes($row['dateAdded']);
}

how could i make it so echo stripslashes($row['url']); will show up as a link i have tried with

echo '</b><br>URL: ';
echo '<a href="'
echo stripslashes($row['url']);
echo '">'
echo stripslashes($row['url'])
echo '</a>'
but it will show as an error

#7 Renlok

Renlok
  • Members
  • PipPipPip
  • Advanced Member
  • 258 posts
  • Locationnewbury

Posted 30 September 2006 - 01:19 PM

anyone? ???

#8 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 30 September 2006 - 01:35 PM

how could i make it so echo stripslashes($row['url']); will show up as a link i have tried with

echo '</b><br>URL: ';
echo '<a href="'; // <- added ; as the most likely omission
echo stripslashes($row['url']);
echo '">'; // <- added ; as the most likely omission
echo stripslashes($row['url']); // <- added ; as the most likely omission
echo '</a>'; // <- added ; as the most likely omission
but it will show as an error


The error that appears is always useful, often helpful, and allows anyone here to understand what might be the problem.  Always post the precise error message(s) you get, please.

As to a solution, my guess is that you've omitted the final ; on several lines.
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users