trying to find out what is wrong...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

EvilRadish
Forum Newbie
Posts: 14
Joined: Wed Mar 31, 2004 7:31 am

trying to find out what is wrong...

Post by EvilRadish »

can someone help me with this code?

it is suposed to work (well sometimes 8O )

i can´t find what is wrong....


thanks in advance.....

<html>
<head>
<title></title>
</head>
<?php
if ($nome) {
$sql1=($nome) ? "nome like '".$nome."%'" :"" ;
}
if ($logic) {
$sql1=($nome) ? "nome like '%".$nome."%'" :"" ;
if ($nomex) {
$sql1.= ($sql1) ? " $logic " : "";
$sql1.= "telefone like '%" .$telefone. "%'" ;
}
}

if ($nome or $telefone) {
$ligação=mysql_connect("localhost", "root", "");
if (!$ligação) {
print ("Connection to MySql database error");
}
$sql="select * from teste where";
$sql.= $sql1. " order by nome asc";
print ("Database Query Results <br><br>");
$resultado = mysql_db_query ("testedb", $sql);

if ($resultado) {
$reg_pag=3;
if (!$pag) {
$pag=1;
}
$pag_ant=$pag-1;
$pag_seg=$pag+1;
$pag_ini=($reg_pag * $pag)-$reg_pag;
$num_reg=mysql_num_rows($resultado);
print ("<b> The Database Query Found $num_reg Records <p>");
print ("<table>");
print ("<tr><td>Proc</td><td>Client</td></tr>");
if ($num_reg<=$reg_pag) {
$num_pag=1;
} else if (($num_reg % $reg_pag)==0) {
$num_pag=$num_reg/$reg_pag;
}else {
$num_pag=$num_reg/$reg_pag + 1;
}
$sql=$sql." limit $pag_ini,$reg_pag";
$resultado=mysql_db_query ("testedb", $sql);
while ($registo=mysql_fetch_array($resultado)) {

$nome2=$registo["nome"];

$nome4=$registo["telefone"];

print ("<tr><td>$nome2</td><td>$nome4</td></tr>");
}
echo ("</table>");
print "page - <p>";
if (($pag_ant) && ($pag>1)) {
echo "<a .href=\"$php_self?pag=$pag_ant&nome=$nome&logic=$logic&telefone=$telefone&sql=$sql\">Previous </a>|- ";
}
for ($i=1; $i<=$num_pag;$i++) {
if($i !=$pag) {
echo "<a href=\"$php_self?pag=$i&nome=$nome&logic=$logic&telefone=$telefone&sql=$sql\">$i</a>-| ";
}else {
echo"$i -| ";
}

}
if ($pag+1 <$num_pag) {
echo "<a href=\"$php_self?pag=$pag_seg&nome=$nome&logic=$logic&telefone=$telefone&sql=$sql\"> Next </a>";
}
}else{
print ("No record found");
}
}else{
echo "Please choose the search method <br>";
echo "(blabla)<p>";

?>
<form method="post" action="<?php echo $PATH_INFO ?>">
<p align=center>Proc - <input type="text" name="nome" size="25">
<p align=center><b><input type="radio" value="and" name="logic"> e
<input type="radio" value="or" checked name="logic"> ou </b></p>
<p align=center>Client - <input type="text" name="telefone" size="12"></p>
<p align=center><input type="submit" value="search" name="Search"></p>
</form>
<?php }
mysql_free_result ($resultado);

?>
</body>
</html>
EvilRadish
Forum Newbie
Posts: 14
Joined: Wed Mar 31, 2004 7:31 am

one important thing!

Post by EvilRadish »

well the result of this code should be a search facility to a database.

it´s seems to process it but the final result is:

no record found even when the name supplied in the search field exist in the database.

??

thanks
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

I would recommend that you output the value of the $sql variable once it is complete.
Try the select displayed yourself within MYSQL and see if that returns nothing.
If you get results then we can look more deeply at the code itself.

You need to help us help you by narrowing the error down as much as possible :wink:
Pozor
Forum Commoner
Posts: 74
Joined: Tue Mar 30, 2004 11:11 pm
Location: Switzerland

Post by Pozor »

hello,

only a littel note,

your code is not very good readable, please use indention to make the code easily readable. It improve the probability of a fast and good answer...


greez Pozor
EvilRadish
Forum Newbie
Posts: 14
Joined: Wed Mar 31, 2004 7:31 am

Ok

Post by EvilRadish »

OK! :lol:
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Also, please put [syntax=php][/syntax] tags around your code so the forum highlights it for you.

Thanks

Mark
EvilRadish
Forum Newbie
Posts: 14
Joined: Wed Mar 31, 2004 7:31 am

Ok... here it is...

Post by EvilRadish »

Here is the code a little bit more cleaner:

Here you have the bit where it checks for the variables in the fatabase and display them if they exist.
I think it´s where the problem is... 8O

Code: Select all

$sql="select * from InfoTotal where"; 
$sql.=$sql1. " order by Client asc";

Here is the code

Code: Select all

if ($nome) {
$sql1=($nome) ? "nome like '%".$nome."%'" :"" ;
}

if ($logic) {
$sql1=($nome) ? "nome like '%".$nome."%'" :"" ;
}

if ($nome0) {
$sql1.=($sql1) ? " $logic " : "";
$sql1.= "nome0 like '%".$nome0."%'" ;
}

if ($nome or $nome0) {
$ligação=mysql_connect("localhost", "root", "");
	if (!$ligação) {
	print ("Connection to MySql database error");
		}
$sql="select * from InfoTotal where"; 
$sql.=$sql1. " order by Client asc";

print ("Database Query Results <br><br>");
$resultado = mysql_db_query ("GeneralInfo", $sql);

Then it will display the results (if any)


Here you have the search form where you will insert the keywords:

Code: Select all

<form method="post" action="<?php echo $PATH_INFO ?>">
<p align=center>Process number - <input type="text" name="nome" size="10">
<p align=center><b><input type="radio" value="and" name="logic"> and
<input type="radio" value="or" checked name="logic"> or </b></p>
<p align=center>Client name - <input type="text" name="nome0" size="25"></p>
<br>
<p align=center><input type="submit" value="search" name="Search"></p>
<br>
</form>
Thanks!
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

and what's the error message?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Also note that the code requires register_globals to be On ... so might be worth checking that first.
EvilRadish
Forum Newbie
Posts: 14
Joined: Wed Mar 31, 2004 7:31 am

and the error code is.....

Post by EvilRadish »

THE RESULT IS:


No records found...

even with an existing record on the database.


if i bypass the

Code: Select all

$sql="select * from InfoTotal where "; 
$sql.=$sql1. " order by Client asc";
with for example

Code: Select all

$sql="select * from InfoTotal ";

it will work just fine

yes the register_globals is on

:?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

echo $sql right before you do the query and see what it says
EvilRadish
Forum Newbie
Posts: 14
Joined: Wed Mar 31, 2004 7:31 am

?

Post by EvilRadish »

where exatly?!?!!

new guy with php here :oops:
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Code: Select all

<?php
$sql="select * from InfoTotal where"; 
$sql.=$sql1. " order by Client asc"; 
echo "$sql"; //HERE

?>
EvilRadish
Forum Newbie
Posts: 14
Joined: Wed Mar 31, 2004 7:31 am

Thanks!

Post by EvilRadish »

Thanks for your help,

the result is...

select * from InfoTotal where nome0 like '%Langaker%' order by Client asc

No record found
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$resultado = mysql_db_query ("GeneralInfo", $sql); ..

mysql_db_query() is depreciated, so you should do :
mysql_select_db('GeneralInfo') or die(mysql_error());
$resultado = mysql_query($sql) or die(mysql_error());
Post Reply