Jump to content
Flaviaac

my variable doesn't show the right type

Recommended Posts

Hi guys, 

I have a odd problem here.

I'm a beginner and I have no idea what it can be.. 😑

ps. English is not my first language, so I hope you can understand... ☺️

 

In dropdown field below, I put in value property the ID column, but when it registers in the database, it saves as string type and not as integer.

<?php
$sql = "SELECT id, sigla FROM local ORDER BY sigla ASC";
$result = $conn->query($sql);
?>
.
.
.
<select name="local" class="form-control">
  <option selected>Selecione...</option>
  <?php
  while($row = $result->fetch_assoc()){                                  
    echo "<option value=".$row['id'].">".$row['sigla']."</option>";                                  
  }                              
  ?>                            
</select>
.
.
.

I used a var_dump() on ID to know what would happend, and it shows as string type.

<pre>
<?php
$sql = "SELECT id, sigla FROM local ORDER BY sigla ASC";
$result = $conn->query($sql);
$row = $result->fetch_assoc();  
var_dump($row['id']);
?>
</pre>

Result:

  string(1) "5"

 

 

This is the database:

-- phpMyAdmin SQL Dump
-- version 4.8.3
-- https://www.phpmyadmin.net/


SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Banco de dados: `arq`
--

-- --------------------------------------------------------

--
-- Estrutura para tabela `local`
--

CREATE TABLE `local` (
  `id` int(11) NOT NULL,
  `SIGLA` mediumtext NOT NULL,
  `LOCAL` mediumtext NOT NULL,
  `TELEFONE` mediumtext NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Fazendo dump de dados para tabela `local`
--

INSERT INTO `local` (`id`, `SIGLA`, `LOCAL`, `TELEFONE`) VALUES
(1, 'AAC', 'AAC', '2222-2222'),
(2, 'DSV', 'DSV', '1111-1111'),
(3, 'SCA', 'SCA', '3333-3333');

--
-- Índices de tabelas apagadas
--

--
-- Índices de tabela `local`
--
ALTER TABLE `local`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT de tabelas apagadas
--

--
-- AUTO_INCREMENT de tabela `local`
--
ALTER TABLE `local`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Thank you so much!!

 

 

 

Share this post


Link to post
Share on other sites

Everything that the browser submits is a string, there is no data type information that gets submitted.  It it just a name, and a string value.

Share this post


Link to post
Share on other sites

Everything that comes from the database is also a string. If you want it to be an integer you have to cast it.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...