Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does not like mysql_fetch........ 1

Status
Not open for further replies.

scitech

Technical User
Jan 6, 2005
40
GB
I'm trying to create an address book, search the table from a form either OR, AND whatever but why does this script not work?
the error is in the fetch_array function, how can I fix this?

<?PHP
include 'config.php';
include 'opendb.php';
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
Enter First Name:-<INPUT NAME="FirstName" TYPE="TEXT" id="FirstName" size="50" maxlength="50"><br>
Enter SurName:-<INPUT NAME="SurName" TYPE="TEXT" id="SurName" size="50" maxlength="50"><br>
<INPUT TYPE="SUBMIT" NAME="Search" id="SUBMIT" VALUE="Search">
</FORM>

<?PHP
$queryItem = array();
$result = array();

if (isset($_POST['FirstName'])){
$queryItem[] = 'FirstName="'.mysql_escape_string($_POST['FirstName']).'"';
}
if (isset($_POST['SurName'])){
$queryItem[] = 'SurName="'.mysql_escape_string($_POST['SurName']).'"';
}
# now you create your query statement (I assume AND)
$querySQL = "SELECT * FROM Addressbook WHERE ".implode(' AND ', $queryItem) or die(mysql_error());
$result = mysql_query($querySQL);

// get the entry from the result
while($row = mysql_fetch_array($result)) // Print out the contents
{
echo"<br>";
echo $row['FirstName']." ".$row['SurName'];
}

echo("$querySQL");

include 'close.php';
?>
 
Code:
<?PHP
$queryItem = array();
$result = array();

if (isset($_POST['FirstName'])){
$queryItem[] = 'FirstName="'.mysql_escape_string($_POST['FirstName']).'"';
}
if (isset($_POST['SurName'])){
$queryItem[] = 'SurName="'.mysql_escape_string($_POST['SurName']).'"';
}
# now you create your query statement (I assume AND)
$querySQL = "SELECT * FROM Addressbook WHERE ".implode(' AND ', $queryItem) [COLOR=red][s]or die(mysql_error());[/s][/color]
$result = mysql_query($querySQL) [COLOR=red]or die(mysql_error());[/color]


// get the entry from the result
while($row = mysql_fetch_array($result)) // Print out the contents 
{
echo"<br>";
echo $row['FirstName']." ".$row['SurName'];
}

echo("$querySQL");

include 'close.php';
?>

Regards,

Martin

Computing Help And Info:
 
Thanks Martin
It works, I did not know that the mysql_error() was compulsory!
It has thrown up another error:-
-You have an error in your SQL syntax, check the manual...right version near " at line 1-

Any ideas on what I should be looking for?
Charlie
 
HI there. mysql_error() is not compulsory, but it tells you the error code and message.
Your problem does not like in the mysql_error(), but your quotes have caused confusion to the query string.
You should be using this one.

if (isset($_POST['FirstName'])){
$queryItem[] = "FirstName='".mysql_escape_string($_POST['FirstName'])."'";
}
if (isset($_POST['SurName'])){
$queryItem[] = "SurName='".mysql_escape_string($_POST['SurName'])."'";
}

$querySQL = "SELECT * FROM Addressbook WHERE ".implode(" AND ", $queryItem) or die(mysql_error());
$result = mysql_query($querySQL) or die(mysql_error());
 
Hi Woody
This is working apart from the same error message that I had before. If I add an 'isset' in front of the 'if ($_POST)' it does not echo, so somthing is not quite right!. Changing the punctuation made the AND statement work
Code:
<?PHP
include 'config.php';
include 'opendb.php';
?>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
Enter First Name:-<INPUT NAME="FirstName" TYPE="TEXT" id="FirstName" size="50" maxlength="50"><br>
Enter SurName:-<INPUT NAME="SurName" TYPE="TEXT" id="SurName" size="50" maxlength="50"><br>
<INPUT TYPE="SUBMIT" NAME="Search" id="SUBMIT" VALUE="Search"> 
</FORM>

<?PHP
$queryItem = array();

if ($_POST['FirstName'])
	{
   $queryItem[] = "FirstName='".mysql_escape_string($_POST['FirstName'])."'";
	}
if ($_POST['SurName'])
	{
   $queryItem[] = "SurName='".mysql_escape_string($_POST['SurName'])."'";
	}
# now you create your query statement (I assume AND)
$querySQL = "SELECT * FROM Addressbook WHERE ".implode(" AND ", $queryItem)  or die(mysql_error());
$result = mysql_query($querySQL) or die(mysql_error());

// get the entry from the result
	while($row = mysql_fetch_array($result)) // Print out the contents 
		{
		echo"<br>";
		echo $row['FirstName']." ".$row['SurName'];
		echo"<br>";
		echo $row['Address1']." ".$row['Address2']." ".$row['Address3'];
		echo"<br>";
		echo $row['Town']." ".$row['Postcode'];
		echo"<p></P>";
		}
echo "<br>";
echo ("$querySQL");

include 'close.php';
?>
 
Hi there.
You can try to change this part.
Code:
<?PHP
if (isset($_POST["Search"]))
{
    $queryItem = array();
     if ($_POST['FirstName'])
    {
        $queryItem[] = "FirstName='".mysql_escape_string($_POST['FirstName'])."'";
    }
    if ($_POST['SurName'])
    {
       $queryItem[] = "SurName='".mysql_escape_string($_POST['SurName'])."'";
    }
# now you create your query statement (I assume AND)
$querySQL = "SELECT * FROM Addressbook WHERE ".implode(" AND ", $queryItem)  or die(mysql_error());
$result = mysql_query($querySQL) or die(mysql_error());

// get the entry from the result
    while($row = mysql_fetch_array($result)) // Print out the contents
        {
        echo"<br>";
        echo $row['FirstName']." ".$row['SurName'];
        echo"<br>";
        echo $row['Address1']." ".$row['Address2']." ".$row['Address3'];
        echo"<br>";
        echo $row['Town']." ".$row['Postcode'];
        echo"<p></P>";
        }
echo "<br>";
echo ("$querySQL");
}
 
Many thanks Woody
It now all works like a dream, I've added a postcode search on the bottom and no conficts even when all three fields are full.
Thanks again it's been driving me mad!
I have a question why specify the button now. In an earlier version i made it did not like the button to be included.
I had something like
Code:
if(isset($POST['FirstName']=='Search'))
{
}
This way much more elegant, I was going to repeat the code 3 times
Thanks again
Charlie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top