E
E
Evgeny Ivanov2018-11-16 07:08:00
css
Evgeny Ivanov, 2018-11-16 07:08:00

How to write a prepared query?

Decided to optimize requests.
I'm trying to write prepared queries. For some reason it doesn't come out.
Data types in table
id - int
time - int
ticker - char

//Получаем данные
$mysqli_request="SELECT `time`,`$column_value_from_base` FROM `$table_name` WHERE `time`>'$begin_time' AND `time`<'$end_time'  AND `ticker`='$ticker'";
$mysqli_result=$mysqli->query($mysqli_request) or die ($mysqli->error);
$mysqli->close;

//Выводим их в цикле
$num_rows = $mysqli_result->num_rows;
//Заполняем массив данных
for($num_rows; $num_rows>0; $num_rows--)
{
$row = $mysqli_result->fetch_assoc(); 
$time=$row['time'];
}
//и далее ещё код....

//Подготавливаемый запрос
$stmt = $mysqli->prepare("SELECT `time`,? FROM $table_name WHERE `time`>? AND `time`<? AND `ticker`=?");
$stmt->bind_param('siis',$column_value_from_base,$begin_time,$end_time,$ticker);
$stmt->execute();
$mysqli_result = $stmt->get_result();
$mysqli->close; //Принудительно закрываем соединение (оптимизация)
$stmt->close; //Принудительно закрываем запрос (оптимизация) 

//Выводим их в цикле
$num_rows = $mysqli_result->num_rows;
//Заполняем массив данных
for($num_rows; $num_rows>0; $num_rows--)
{
$row = $mysqli_result->fetch_assoc(); 
$time=$row['time'];
}
//и далее ещё код....

The result of the prepared request is erroneous, empty - in general, something is wrong.
In addition, in the request, I was not allowed to replace $table_name with ? (an error occurs)
How to write a prepared query correctly?
And does it make sense? Estimated sample is 1000 values ​​($num_rows will be around 1000).

Answer the question

In order to leave comments, you need to log in

6 answer(s)
D
Denis Bukreev, 2015-11-04
@denisbookreev

roughly threw rubber)
codepen.io/anon/pen/KdBJew
HTML:

<header>
  
  <div class="logo">
    <h1>Logo</h1>
  </div>
  
  <div class="right-header">
    
    <div class="name">
      <h2>name</h2>
    </div>


    <div class="description">
      <p>description</p>
    </div>
    
  </div>
  
</header>

CSS:
header {
  position: relative;
  width: 100%; height: 200px;
  outline: 2px solid #000;
}
header, header * {
  padding: 0;
  margin: 0;
}

.logo {
  position:absolute;
  outline: 1px solid #000;
  width: 200px; height: 200px;
}

.right-header {
  position: absolute;
  right: 0; top: 0;
  width: calc(100% - 200px);
}
  .name {
    width: 100%;
    height: 70px;
    outline: 1px solid #000;
    right: 0; top: 0;
    text-align: center;
  }
  .description {
    width: 100%;
    height: 130px;
    outline: 1px solid #000;
    right: 0; top: 0;
    text-align: center;
  }

D
dm, 2015-11-04
@Chekhoved

HTML

<header class="header clearfix">
  <div class="left"></div>
  <div class="right-top"></div>
  <div class="right-bottom"></div>
</header>

css
.left {
  float: left;
  width: 33%;
  height: 300px;
  background: green;
}
.right-top,
.right-bottom {
  float: right;
  width: 67%;
}
.right-top {
  height: 100px;
  background: red;
}
.right-bottom {
  height: 200px;
  background: blue;
}
.clearfix::after {
  display: block;
  content: '';
  clear: both;
}

aa8fde7bbffd43e2a27c5947d7dced91.jpg

Z
zooks, 2015-11-04
@zooks

Read two articles and understand: CSS Flex
Document Flow

A
Alex_Zamazan, 2015-11-08
@Alex_Zamazan

Sorry for the long response - thank you so much! Have a nice day!

R
Rsa97, 2018-11-16
@logpol32

Placeholders can only substitute values. The names of columns, tables, databases are not substituted.
PS In general, see errno / error after each function.

D
Dave Hornigan, 2018-11-16
@DeyvHorni

More or less like this

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$stmt = $dbh->prepare("SELECT * FROM table where `time` > ? AND `time` < ? AND ticker = ?");
if ($stmt->execute(array($minTime, $maxTime, $ticker))) {
  while ($row = $stmt->fetch()) {
    print_r($row);
  }
}

php.net/manual/ru/book.pdo.php
and if you need to limit the number of lines that you output. It’s easier and more correct to simply not get the extra ones, and not to count how many came out ... offset limit

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question