PHP request does not work with variable

On my page I have this code with an echo.

<?php

include("../config.php");

$q = mysql_query("SELECT propertyaddress FROM propertydetail WHERE active='yes' and leasedatefrom='".date("m-d-Y", strtotime('+1 months'))."'");
$res = mysql_fetch_array($q);
echo "<br/>pdetail=".$pdetail=trim($res['propertyaddress']);
echo $query="SELECT * FROM tenantmaster WHERE propertyaddress like '".$pdetail."'";
//echo $query="SELECT * FROM tenantmaster ";
//echo $query="SELECT * FROM tenantmaster WHERE propertyaddress = '1934 Heron Ave Unit D Schaumburg IL 60193'";

$resultdb = mysql_query($query);
if (!$resultdb) {
    die('Invalid query: ' . mysql_error());
}
else{
    echo "<br/>right query";
}

echo "<br/>num of row===".mysql_num_rows($resultdb);
$rowt = mysql_fetch_array($resultdb);
echo "<br/>row===".$rowt['name'];
exit;

?>

config.php

<?php

$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "gms_estate";

/*
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "gms_estate";
*/

$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) 
or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");

?>

And the problem is that my first query $qworks, but the query $queryalso works, but it mysql_num_rows($resultdb)doesn’t work and displays the rows 0, but when I run the echo request into the database, it shows the string 1, Why?

I tried the variable $res['propertyaddress']c trim(), but did not succeed.

But when I use the 1934 Heron Ave Unit D Schaumburg IL 60193 (this is my variable value) instead $res['propertyaddress'], then it works.

So, when I give the value of a variable directly, it works, but when I give a variable, then no. Why?

+4
source share
7

, , , " " "" , . ( gotchas - " " ).

HTML, , ( ), ( ). " " ( ) , .

, , preg_replace

$pdetail = trim( preg_replace("/[^0-9a-zA-Z ]/", "", $res['propertyaddress']) );

, , , " ", , - .


, : mysqli/PDO . , ( ). , , , SO ( ).

http://php.net/manual/en/pdo.prepared-statements.php

+6
<?php
    include("../config.php");

    $connect = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_database", $mysql_user, $mysql_password);

    $q = "SELECT propertyaddress FROM propertydetail WHERE active='yes' and leasedatefrom='".date("m-d-Y", strtotime('+1 months'))."'";
    $result = $connect->prepare($q);
    $status = $result->execute();
    $res = $result->fetch(PDO::FETCH_ASSOC);
    $pdetail = $res["propertyaddress"];

    $q = "SELECT * FROM tenantmaster WHERE propertyaddress = ".$connect->quote($pdetail);
    /* or
    $q = "SELECT * FROM tenantmaster WHERE propertyaddress like ".$connect->quote($pdetail);
    */
    $result = $connect->prepare($q);
    $status = $result->execute();

    echo "<br/>num of row===".$result->rowCount();

    if (($status) && ($result->rowCount() > 0))
    {
        $res = $result->fetch(PDO::FETCH_ASSOC);
        echo "<br/>row===".$res['name'];
    }

    $connect = null;
?>
+2

mysqli , SQL-, . , mysqli PDO.



trim() .

trim - ( )

: , . trim() :

  • "" (ASCII 32 (0x20)), .
  • "\ t" (ASCII 9 (0x09)), .
  • "\n" (ASCII 10 (0x0A)), ().
  • "\ r" (ASCII 13 (0x0D)), .
  • "\ 0" (ASCII 0 (0x00)), NUL-.
  • "\ x0B" (ASCII 11 (0x0B)), .

:

trim() not remove , middle of the string, .

:

trim() , , ( ) . ('abc', 'bad') removes both ' a b', " , " b " , . " ", trim('abc', 'b'), , .

: , , , .

  • , , preg_replace().
  • , , , , , , .

3. You can directly adopt the method by strong the trimmed value into a variable and then echo it.

preg_match -

: , .

: preg_match() 1, , 0, , FALSE, .



1934 Heron Ave Unit D Schaumburg IL 60193 ( ) $res ['propertyaddress'], .

: .

  • - , DB HTML-.
  • , HTML, DB , , .
  • , HTML, CTRL+U, , . , , .

Query:

preg_replace("/(\W)+/", "", $word_to_undergo);

: \W - , , .

, Unicode \W , L N .

:

:

Query:

$needed_text = preg_replace("/[^A-Za-z0-9 ]/", "", $word_to_undergo);

, , .

:

$final_value = preg_replace("/(\W)+/", "", $word_to_undergo);
$final_value = preg_replace("/(\W)+/", "", $res['propertyaddress']);

2:

$final_value = preg_replace("/[^A-Za-z0-9 ]/", "", $word_to_undergo);
$final_value = preg_replace("/[^A-Za-z0-9 ]/", "", $res['propertyaddress']);

, .

<?php
$display=trim($res['propertyaddress']);
echo $display; 
?>
+2

echo $query="SELECT * FROM tenantmaster WHERE propertyaddress like '".$pdetail."'";

echo $query="SELECT * FROM tenantmaster WHERE propertyaddress like '%".$pdetail."'%";
+1

echo "<br/>pdetail=".$pdetail=trim($res['propertyaddress']);

$pdetail=trim($res['propertyaddress']);
echo "<br/><pre>pdetail='".$pdetail."'</pre>";

And you will see the real value of the variable

+1
source

Try with this query. This will be useful to get your result.

$query='SELECT * FROM tenantmaster WHERE propertyaddress like "'.$pdetail.'";
0
source

You are missing mysql_free_result($q);and mysql_free_result($query)to announce that you are finished with the request.

And change to mysqli (or PDO).

-2
source

Source: https://habr.com/ru/post/1655627/


All Articles