<?php
$sql = "SELECT id FROM chatsystem WHERE user1 = ? or user2 = ?";
$stmt = mysqli_stmt_init($connect);
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_bind_param($stmt, "ss", $username, $username);
mysqli_stmt_execute($stmt);
$resultData = mysqli_stmt_get_result($stmt);
mysqli_stmt_close($stmt);
while ($row = mysqli_fetch_assoc($resultData)) {
var_export($row);
}
<?php
function split_number($num, $count) {
$result = [];
for ($i = 1; $i<$count; $i++) {
// random number between 1 and half of given number
$result[$i] = rand(1, $num / 2);
$num -= $result[$i];
}
$result[0] = $num;
return $result;
}
$chunks = split_number(500, 5);
<?php
$request = [
87=>'AMD',
86=>'8'
];
$filter = implode(' or ', array_fill(0, count($request), '(pv.property_id = ? and pv.value = ?)'));
$filter_values = [];
foreach($request as $key=>$val) {
$filter_values[] = $key;
$filter_values[] = $val;
}
$query = 'select p.* from products p
join property_values pv on p.id = pv.product_id
where ' . $filter . '
group by p.id, p.name
having count(distinct pv.id) = ' . count($request);
<?php
$str = '[contact-form-7 id="6274" title="Какой то текст с пробелами"]';
preg_match("/\[(\S+) (\S+) (.+)\]/im", $str, $matches);
var_export($matches);
$titlepage = explode( "\n" , $text);
$new_text = implode(PHP_EOL, array_slice($titlepage, 1));
echo $new_text;
echo preg_replace('/\A(.*)$/mi', '', $text);
select
* ,
case
when role = 'boss' then 3
when role = 'admin' then 2
else 1
end as weight
from gamers order by weight desc;
select *
from gamers
order by role = 'boss' desc, role = 'admin' desc;
<?php
$start = "10:00";
$stop = "02:00";
$current_time = time();
$start_time = strtotime($start);
$stop_time = strtotime($stop);
// if end time less then start add one day
if ($stop_time < $start_time) $stop_time += 60 * 60 * 24;
echo "$start_time < $current_time < $stop_time" . PHP_EOL;
if ($current_time < $start_time || $current_time > $stop_time) {
die('Closed');
}
echo 'Open';
$bd->Query("SELECT COUNT(*) FROM users_nykfageubf WHERE p = '$p' AND promo_new=1"))
$bd->Query("UPDATE users_nykfageubf SET balance=balance+10, promo_new = 0 WHERE p = '$p' AND promo_new=1");
$stmt = $bd->Query("SELECT 1 FROM users_nykfageubf WHERE p = '$p' AND promo_new=1 LIMIT 1");
if ($stmt->rowCount() > 0) {
...........
}
Что пробовал:
JSON.stringify. В итоге на выходе получаем строку, вместо объекта. Но как ее теперь закинуть в php файлик?
$json = json_decode($_POST['json']);
<?php
echo implode( // собираем строку
",",
array_slice( // берем 3 первых эдемента
explode(",", $address), // разбиваем на массив по запятой
0,
3
)
);
preg_match_all('/,/', $address, $matches, PREG_OFFSET_CAPTURE);
echo substr($address, 0, $matches[0][2][1]);
<?php
function getOne($conn) {
$result = mysqli_query($conn,"
SELECT *
FROM `stat`
JOIN `order` ON `order`.`id` = `stat`.`order_id`
WHERE `delivery`='Почта' AND `cost`='0'"
);
if (mysqli_num_rows($result)>0) {
while ($row = mysqli_fetch_assoc($result)) {
echo json_encode($row);
}
}
else {
echo "Ошибка в getOne";
}
mysqli_close($conn);
}
$result = mysqli_query($conn, "SELECT * FROM `country` WHERE `city`='Москва'");
$row = mysqli_fetch_assoc($result);
printf('Country ID is: %s', $row['id']);
<?php
if ($reviews) {
foreach ($reviews as $review) {
if (!preg_match('/[^A-Za-z0-9]/', $review['text'])) {
echo '<div class="review-list">
<div class="author"><b>' . $review['author'] .'</b> '. $text_on . $review['date_added'] . '</div>
<div class="rating"><img src="catalog/view/theme/default/image/stars-' . $review['rating'] . '.png" alt="' . $review['reviews'] .'" /></div>
<div class="text">' . $review['text'] .'</div>
</div>';
}
}
}
$result = array_reduce(
$data,
function($res, $el) {
if (isset($res[$el["id_product_attribute"]])) {
$res[$el["id_product_attribute"]] .= ", " . $el["attribute_name"];
} else {
$res[$el["id_product_attribute"]] = $el["attribute_name"];
}
return $res;
},
[]
);
var_export($result);
function addList($data, $conn)
{
$cart = json_decode($data["cart"], true);
$stmt = $conn->prepare(
"UPDATE `list` SET `count` = GREATEST(`count` - ?, 0) WHERE `id`= ? ;"
);
/* bind parameters for list */
$stmt->bind_param("ds", $count, $id);
foreach ($cart as $value) {
$id = $value["id"];
$count = $value["count"];
/* execute query */
$stmt->execute();
}
}
<?php
$str = "@User, Привет";
preg_match('/(@)([^,]+),(.+)/i', $str, $matches);
var_export($matches);
array (
0 => '@User, Привет',
1 => '@',
2 => 'User',
3 => ' Привет',
)