0 votes

Problems with "&"

Ok, as we can see in my CRUD I have two columns and in some of them sometimes you can see the "&" character.

I want to edit any column and I can do it perfectly. http://imgur.com/a/cWPBr I select the edit button and it automatically appears at the top of the boxes and can be editable.

But if I select one that has "&" it does not display anything but picks up the data by the URL. http://imgur.com/a/ievHj

The PDO connection I have is as follows

public function __CONSTRUCT()
        $this->pdo = new PDO('mysql:host=localhost;dbname=deimos1;charset=UTF8', 'root', '');
                    $this->pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
                    $this->pdo->exec("SET NAMES 'utf8';");
    catch(Exception $e)

I do not modify neither password nor pass as they are the default ones since this is testing.

And this would be the part in which it should obtain the data that was the one that I modified before with what you told me.

public function Obtener($acronimo) {
    try {
        $stm = $this->pdo->prepare('SELECT * FROM categoria WHERE acronimo = ?');
        $r = $stm->fetch(PDO::FETCH_ASSOC);

        $alm = new Categoria();

        $alm->__SET('acronimo', $r["acronimo"]);
        $alm->__SET('categoria', $r["categoria"]);

        return $alm;
    } catch (Exception $e) {

I get the data here:

<?php foreach ($model->Listar() as $r): ?>
                        <td><?php echo $r->__GET('acronimo'); ?></td>
                        <td><?php echo $r->__GET('categoria'); ?></td>

                            <a href="?action=editar&id=<?php echo $r->acronimo; ?>"><img src="icon_editthis.png" width="30px" height="30px"/></a>
                            <a href="?action=eliminar&acronimo=<?php echo $r->acronimo; ?>"><img src="delete.png" width="30px" height="30px"/></a>


cnbandicoot Points 2370

Use fetchObject() instead of fetch() if you want to treat rows as objects and be able to use their properties.

$r = $stm->fetchObject(PDO::FETCH_OBJ);


A. Cedano Points 48884


When we send values by PHP in a URL, the language uses by default the symbol & to separate the different values. If you look at the navigation bar you will see:

... index.php?action=editar&id=&CC

Means that all what's after the symbol ? are values that are passed to the server and are then retrieved by _$POST by _$GET etc.

Suppose something like this:

... index.php?action=editar&id=1&nombre=Juan&apellido=Mendez

between each & there will be values in an associative form: id=1 nombre=Juan apellido=Mendez

which are then obtained by $_POST["id"] , $_POST["nombre"] etc.

The problem is that in your case: action=editar&id=&CC id is equal to null or empty, because the & that is before CC what it does is to tell PHP which starts another variable call CC .

Solution :

Use instead of that value, another value that uniquely identifies the rows of your table, if it is a numeric value better, for example, the autoincrement column of the table.

If the table categoria had a numeric field called for example id_categoria that uniquely identifies each row of the table, it is better to pass this field:

    $alm->__SET('categoria', $r["id_categoria"]);

Then your URL would look like this, for example:

... index.php?action=editar&id=8945

then you get id with _$GET or with $_POST . Example:


and pass it the value of $id in the WHERE id_categoria=? .

As for the PDO connection, you have set the charset twice. In other words, this line is not needed:

$this->pdo->exec("SET NAMES 'utf8';");

because above you already set the charset by doing:

$this->pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");


Actually, the problem with the ñ and the two news (which is not the same as error) you are having, are two different things .

A. The problem of the ñ

It is a character encoding problem. It can be found at several levels.

You've already said that HTML is fine:

<meta http-equiv="Content-Type" content="text/html"; charset=utf-8"/>

then it is very likely that the problem is that, when making your PDO connection, you are not telling it to connect with the utf-8 charset. You can check what the PHP Manual . Sometimes, even if the database charset is correct, it is not displayed correctly. if we do not indicate in the connection that we want to use the utf-8 charset.

Examples of connection by setting the charset:

$pdo = new PDO("mysql:host=localhost;dbname=DB;charset=UTF8");


$dsn = 'mysql:host=localhost;dbname=testdb';
$username = 'username';
$password = 'password';
$options = array(

$dbh = new PDO($dsn, $username, $password, $options);

If you already have the charset set correctly in the DB and in the connection and you are still having problems with the ñ, then you should check that that column that is giving you problematic values has the right coding in the database or, as a last resort, you should consider setting the charset in the server configuration (PHP or other) to utf-8.

About coding problems you can also see the answer to this question: Inspector character appears in some data obtained from the database.

B. The two news

Notice: Trying to get property of non-object in C:\xampp\htdocs\ScriptBBDD\2\alumno.model.php on line 56

Notice: Trying to get property of non-object in C:\xampp\htdocs\ScriptBBDD\2\alumno.model.php on line 57

indicate that you are trying to access a property of an object that does not exist. That is, it is as if object did not exist and you did this: objeto->dame_esta_propiedad(); . Then the code tells you: but buenoooo, objeto does not exist I can't give you that property . It is a news that is, PHP informs you that that's not right but it lets you pass and so the code works. If it were an error you would probably see a blank screen.

Where are you doing that? In two lines of your code, lines 56 and 57 of your code.

Will these be the two:

    $alm->__SET('acronimo', $r->acronimo);
    $alm->__SET('categoria', $r->categoria);

It is very likely. If acronym and category are two columns of your database and you want to set those values to the variable $alm you are not doing well.

You can do this:

$r = $stm->fetch(PDO::FETCH_ASSOC);

And then:

    $alm->__SET('acronimo', $r["acronimo"]);
    $alm->__SET('categoria', $r["categoria"]);

Notes :

  1. Between the square brackets after $r there must be exactly the column names as they are in the database.
  2. To check that there is data in $r you can make a print_r($r) in which you will see an associative array if your query has fetched data.

Note that PDO::FETCH_ASSOC is one of the ways that PDO has to get our results. What it does is to create an associative array of our results in which the key is the name of each column of the result and the value is the content of that column.


HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: