Jump to content

Recommended Posts

My PHP search code always returns 0 records, even when the record exists.

Any help would be appreciated.

<?php
$conn = new mysqli("localhost", "root", "xxxxxx", "SAVR1984");

$Surname =$_POST['Surname'];
$Maidenname = $_POST['Maidenname'];
$Firstname = $_POST['Firstname'];
$sql="SELECT * FROM 1984 WHERE Surname LIKE $Surname AND Maidenname LIKE '%".$Maidenname."%' AND Firstname LIKE '%".$Firstname."%'";
If(! $conn ) {
die('Could not connect: ' . mysqlerror());
}

$result = $conn->query($sql);

if ($result->num_rows > 0){
while ($row = $result->fetch_assoc() ){
    echo $row["Surname"] . " " . $row["Maidenname"] . " " . $row["Firstname"] . " " . $row["ID"] . " " . $row["Occupation"] . " " . $row["Address"] . " " . $row["Notes"] . "<BR>";
      }
} else {
echo "0 records found";
}

$conn->close();
?>

Link to comment
https://forums.phpfreaks.com/topic/314871-code-always-returns-0-records/
Share on other sites

1 - Please use the <> icon above to post your code proerly as I am doing now.

$conn = new mysqli("localhost", "root", "xxxxxx", "password");
$Surname = $_POST['Surname'];
$Maidenname = $_POST['Maidenname'];
$Firstname = $_POST['Firstname'];
$sql = "SELECT * FROM 1984 WHERE Surname LIKE '$Surname' AND Maidenname LIKE '%$Maidenname%' AND Firstname LIKE '%$Firstname%'";
If(!$conn) 
	die('Could not connect: ' . mysqlerror());	// MYSQLI ERROR????
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
	while ($row = $result->fetch_assoc())
	{
		echo $row["Surname"] . " " . $row["Maidenname"] . " " . $row["Firstname"] . " " . $row["ID"] . " " . $row["Occupation"] . " " . $row["Address"] . " " . $row["Notes"] . "<BR>";
	}
} 
else 
	echo "0 records found";
$conn->close();

You should read up on using prepared queries and change this to use them.  Security.

Don't know if you are using mysqli or PDO for your database access but I'm guessing that mysqlerror is not a valid function.  Could be wrong.

Note the changes I made to your query.  Could be the whole issue.

Programming change:  you should check the connection results right away instead of moving on to building the query  and gathering the data items.  What's the point of doing more steps if the connection won't allow you to use them?

Suggestion.  Add the query statement to your echo when you get 0 records.  That way you can see the actual query you ran.

Bad SQL syntax, not uncommon when using concatenated segments.

If I provide variable values then echo your SQL

$Surname = 'Smith';
$Maidenname = 'Jones';
$Firstname = 'Mary';
$sql="SELECT * FROM 1984 WHERE Surname LIKE $Surname AND Maidenname LIKE '%".$Maidenname."%' AND Firstname LIKE '%".$Firstname."%'";
echo $sql;

giving

SELECT * FROM 1984 WHERE Surname LIKE Smith AND Maidenname LIKE '%Jones%' AND Firstname LIKE '%Mary%'
                                      ^^^^^ 

Don't use SELECT *, specify the columns you need.

1984 table name implies you have separate tables for the people for different years (of birth?). Why not put them in one table and add year column to the data.

As G said, use prepared statements, then the query becomes SELECT * FROM 1984 WHERE Surname LIKE ? AND Maidenname LIKE ? AND Firstname LIKE ? ;

Provide the data values when you execute the query.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.