I want to make for each data exist on databases then do loop up to get unique data.
Here my code:
$id = rand(10000000,99999999);
$check_id = $db->prepare("SELECT * FROM sh_url WHERE sh_id='$id'");
$check_id->execute();
$count_id = $check_id->rowCount();
for ($count_id != 0) {
$lid = $id+1;
}
$shorturl = htmlentities(base_convert($lid,20,36));
$query = $db->prepare("INSERT INTO `sh_url`(`sh_id`) VALUES (:id)");
$query->bindParam(":id", $lid);
$query->execute();
Why don't you use distinct keyword to do this.
SELECT DISTINCT column1, column2, ...
FROM table_name;
Related
How can I do multiple while query from 2 different table?
My code looks like this now:
$query = "SELECT * FROM user_group WHERE email = '$email'";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$group = $row['group'];
$sql = "SELECT * FROM matches WHERE group_name1 = '$group' OR group_name2 = '$group'";
$result = $connection->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
//some HTML code
}
}
?>
It's works but showing only one row from the matches table because of the first query (?) where I get the '$row['group'];'
You need to loop through all the results from the first query, and then perform the second query within the loop.
But it's better to join the two queries into one.
$query = "
SELECT m.col1, m.col2, m.col3, ...
FROM matches AS m
JOIN user_group AS g ON g.group IN (m.group_name1, m.group_name2)
WHERE g.email = ?";
$stmt = $connection->prepare($query);
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($col1, $col2, $col3, ...);
while ($stmt->fetch()) {
// some code that uses the variables $col1, $col2, $col3, ...
}
Replace col1, col2, col3, etc. with the actual column names from your table.
I'm trying to select count of repeated dates and output its numbers
$user_curr_id = $_SESSION['user_id'];
$sql = "SELECT COUNT(datum) FROM table_name WHERE user_ids = $user_curr_id";
I have no idea how to do it.
2014-07-23,2014-07-23,2014-07-23 => 3
2014-07-24,2014-07-24 =>2
2014-07-25 => 1
and get $result = 3,2,1
I assume you're looking after the GROUP BY clause:
$sql = "SELECT datum, COUNT(datum) as cnt
FROM table_name
WHERE user_ids = $user_curr_id
GROUP BY datum
ORDER BY COUNT(datum) DESC;";
if your column datum is of the data type DATE.
Note
As already mentioned you're vulnerable to sql injection. You should use a parameterized prepared statement and bind your input value to this parameter like that:
$sql = "SELECT datum, COUNT(datum) cnt
FROM table_name
WHERE user_ids = ?
GROUP BY datum
ORDER BY COUNT(datum) DESC;";
$result = array();
if ($stmt = $mysqli->prepare($sql)) {
if ($stmt->bind_param('s', $user_curr_id)) {
if($res = $stmt->execute()) {
while ($row = $res->fetch_assoc()) {
$result[] = $row['cnt']; // add the content of field cnt
}
}
}
}
echo implode(',', $result);
I got a table named "Serials" with 5 comumns
Serial, Code, Name, Redeemed, Redeem_date
i am selecting some fields from that table with this query:
$query = "SELECT `Name`,`Redeemed`,`Redeem_date` FROM `Serials` WHERE `Serial` = '$serial' AND `Code` = '$code'";
$db->setQuery($query);
$db->query();
But i dont know how to pass these values in the following variables so i can use them in if statements later
$name= //retured value from column Name
$redeemed= //retured value from column Redeemed
$redeem_date= //retured value from column Redeem_date
just like this..
// Your query here..
$query = "SELECT `Name`,`Redeemed`,`Redeem_date` FROM `Serials` WHERE `Serial` = '$serial' AND `Code` = '$code'";
$db->setQuery($query);
$results = $db->query();
//fetch data and stored into variables
while($row = fetch_array($results)){
$name = $row['Name'];
$redeemed = $row['Redeemed'];
$redeem_date = $row['Redeem_date'];
}
try something like this :
<?php
$result = $db->query("SELECT `Name`,`Redeemed`,`Redeem_date` FROM `Serials` WHERE `Serial` = '$serial' AND `Code` = '$code'");
while (list($name, $redeemed, $redeem_date) = $result->fetch(PDO::FETCH_NUM)) {
// DO SOMETHING
}
?>
while ($row = $db->fetch()) {
$name= $row['name'];
$redeemed= $row['redeemed'];
$redeem_date= $row['redeem_date'];
}
this one might fetch your results and assign to vars
I've a PHP application that works with MySQL through PDO. I have a table with different records and I have to prevenet inserting a duplicate one. But when I want to check existing items, select statement does not return a true value. This is my code:
$sql = "SELECT COUNT(id) FROM tbl_product_category1 WHERE title = '?'";
$q = $db->prepare($sql);
$q->execute(array($title));
if ($q->fetchColumn() == 0)
{
...
I also tested this one:
$sql = "SELECT id FROM tbl_product_category1 WHERE title = '?'";
$q = $db->prepare($sql);
$q->execute(array($title));
$rows = $q->rowCount();
if ($rows == 0)
{
...
Imagine $title=1. I have 4 records with this value. But I can not see anything in SELECT statement. What is wrong here ?
try this: (don't wrap the value of the title with single quotes)
$sql = "SELECT COUNT(id) FROM tbl_product_category1 WHERE title = ? ";
is there any way how in this situation insert and update DB with single queries?
$message = 'Hello to all group members';
$userdata = mysql_query("SELECT memberid, membernick FROM members WHERE groupid='$cid'") or die('Error');
while(list($memberid, $membernick) = mysql_fetch_row($userdata)) {
$result1 = mysql_query("INSERT INTO messages VALUES (NULL,'$membernick', '$memberid', '$message')") or die('Error');
$result2 = mysql_query("UPDATE users SET new_messages=new_messages+1, total_messages=total_messages+1 WHERE id='$memberid'") or die('Error');
}
The update and insert can be one-ified with mysql >= 5.1. Try
$message = 'Hello to all group members';
$userdata = mysql_query("SELECT memberid, membernick FROM members WHERE groupid='$cid'") or die('Error');
$memberids = array();
$values = array();
while(list($memberid, $membernick) = mysql_fetch_row($userdata)) {
array_push($values, "(NULL,'$membernick', '$memberid', '$message')");
array_push($memberids, "'$memberid'");
}
// ==> replace colX with the names of your columns
// check http://dev.mysql.com/doc/refman/5.1/en/insert.html for further information
$result1 = mysql_query("INSERT INTO messages (col1,col2,col3,col4) VALUES ".implode(",", $values)) or die('Error');
$result2 = mysql_query("UPDATE users SET new_messages=new_messages+1, total_messages=total_messages+1 WHERE id IN (".implode(",", $memberids).")") or die('Error');
I Hope this will help
Jerome Wagner
Nope, MySQL doesn't have a support for updating or inserting multiple rows in multiple tables with a single query.