-1

Hi i have a problem qith this select i try to import default items from my dtaabse to a select for a signup of users the error is on this line if ($result->num_rows > 0) can i do!!!?

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "gimnasio";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT idTipo de Documento,Tipo_de_Documento FROM tipo de documento";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table><tr><th>idTipo de Documento</th><th>Tipo_de_Documento</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["idTipo de Documento"]."</td><td>".$row["Tipo_de_Documento"]."</td></tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
?>

            <div>



        <div class="form-group">
                   <label>Tipo de Documento:</label>

            <select id="tipo_tel" class= "form-control" name="txt_utdoc">

                <option value="101">CEDULA DE CIUDADANIA</option>
                    <?php 

                    while($row = $result->fetch_assoc()) 
                    { 

                        ?>
                        <option value="<?php echo $row['idTipo de Documento']; ?>"><?php echo $row['Tipo_de_Documento']; ?></option>
                        <?php 
                    }

                    ?>
            </select>

        </div>
            </div>
vp_arth
  • 14,461
  • 4
  • 37
  • 66
SERGIO
  • 13
  • 5
  • $sql = "SELECT idTipo de Documento,Tipo_de_Documento FROM `tipo de documento`"; (use back-tics around table name) table name and column name with spaces are completely wrong practice. – Alive to die - Anant Mar 12 '17 at 17:54
  • Possible duplicate of [How do I escape reserved words used as column names? MySQL/Create Table](http://stackoverflow.com/questions/2889871/how-do-i-escape-reserved-words-used-as-column-names-mysql-create-table) – vp_arth Mar 12 '17 at 17:55

1 Answers1

0

If you are getting a "property of a non-object" error then this suggests that an object you are expecting to be assigned to your variable has not been correctly set.

In your case, $result is either not an object or has returned as null/false.

You should probably use some form of try {} catch($e) {} to make sure that if your database query fails, the error is caught.

Documentation on what will be returned by MySQLi::query() can be found here

http://php.net/manual/en/mysqli.query.php

Spholt
  • 3,724
  • 1
  • 18
  • 29
  • ok i find the error. but now in my form only display one colum of three columns who have in my db !!! – SERGIO Mar 12 '17 at 18:31