0

I've got this code:

  if (is_user_logged_in()) {
$user = wp_get_current_user();
  $tenantsInfo = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM exp_ten WHERE tenant_number = %d ", $user->ID ) );
if ($tenantsInfo) {
        foreach ($tenantsInfo as $tenant) { 
           echo "<h2>Welcome, " .$tenant->tenant_name. "</h2>";
           echo "<h3>Your details:</h3>";
           echo "<span class='bold-hc'>Name:</span>" . " " .$tenant->tenant_name. "" . "<br>";
           echo "<span class='bold-hc'>Tenant Reference Number:</span>" . " " .$tenant->tenant_number. "" . "<br>";
           echo "<span class='bold-hc'>Address:</span>" . "<br>";
           echo "$tenant->unit_description";
           echo "<span>" . " " .$tenant->house_number. "</span>";
           echo "<span>" . " " .$tenant->address_line_1. "</span>" . "<br>";
           echo "<span>" . " " .$tenant->address_line_2. "</span>" . "<br>";
           echo "<span>" . " " .$tenant->address_line_3. "</span>" . "<br>";
           echo "<span>" . " " .$tenant->postcode. "</span>" . "<br>";
           echo "<span class='bold-hc'>Phone:</span>" . " " .$tenant->telephone_number. "" . "<br>";
           echo "<span class='bold-hc'>Email:</span>" . " " .$tenant->email_address. "" . "<br>";
        }
}
else {
    echo 'No data found for the current user';
}
}
else {
    echo "<p style='padding-bottom:20px;'>You are not logged in. Please use 
the form below to log in.</p>";
}

Problem I have is that every time when in database there's tenant with apostrophe in their surnames, no data is displayed on frontend. All I get is "No data found for the current user" info.

Code works well for all other tenants without apostrophe in their names.

Can anyone please help me with finding what is wrong with my code?

Thank you.

  • is the data definitely there in the database? – inarilo Nov 20 '17 at 14:38
  • Yes, it is in the database – webhappy Nov 20 '17 at 14:53
  • does your query work in phpmyadmin? what do you get with a var_dump of the results? – inarilo Nov 20 '17 at 17:17
  • Thank for your clue and sorry, I was wrong, double checked and there's no data in db for tenants with ' in their surnames. Data is inserted to db every day automatically from csv files, it's scheduled with cron job. In php file I found query which causes issues by using single quotes: $StrSQL = "INSERT INTO exp_ten (tenant_number, tenant_name) VALUES (' $data[0] ',' $data[1] ',);"; How can I replace single quotes here so query can be executed properly? Thanks – webhappy Nov 21 '17 at 16:11
  • use us3.php.net/manual/en/mysqli.real-escape-string.php on the string data before putting it in the query – inarilo Nov 24 '17 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.