Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to search MySQL table and get in result in which don't have . (dot). example is below
example1.com
example2.com
example3
example4 com
So i want return, example 3 and 4. So basically i want row which don't have . (dot).
You can just use NOT LIKE:
SELECT column
FROM tablename
WHERE column NOT LIKE '%.%'
You can do that #fthiella suggested, or you can filter your search results in this was, using the stristr PHP function:
<?php
$searchQuery = 'example';
$sql = 'SELECT * FROM yourTable WHERE domain LIKE "%' . mysql_real_escape_string($searchQuery) . '%"';
$query = mysql_query($query);
$total = mysql_num_rows($query);
if($total) {
$data = array();
while($row = mysql_fetch_array($query)) {
if(stristr($row['domain'], '.') !== false) {
$data[] = $row['domain'];
}
}
die('<pre>' . print_r($data, true) . '</pre>');
} else {
die('Nothing was found.');
}
Note that I don't know your database table structure, so this is a psuedo-example.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
this is the code
$get_favory = mysql_query("SELECT * FROM favory WHERE user1='$username'");
$values = "";
while($fav = mysql_fetch_assoc($get_favory)) {
$user2 = $fav['user2'];
$values = " OR added_by='".$user2."'";
}
echo $values;
the values variable must be like this : OR added_by='zac' OR added_by='john' OR added_by='emily'
but i get this result : OR added_by='zac'
what i have to do?
Why your code is not working
You're overwriting the variable every time instead of appending the new value.
Solution
Replace
$values = " OR added_by='".$user2."'";
With
$values .= " OR added_by='".$user2."'";
Don't use mysql_ extension
mysql_ extension is deprecated. You should use mysqli_ instead.
Your code should be:
$connect = mysqli_connect(host,username,password,db_name);
$get_favory = mysqli_query($connect,"SELECT * FROM favory WHERE user1='$username'");
$values = "";
while($fav = mysqli_fetch_assoc($get_favory)) {
$user2 = $fav['user2'];
$values .= " OR added_by='".$user2."'";
}
echo $values;
Might be simpler using the IN clause:
// you only need user2
$get_favory = mysql_query("SELECT user2 FROM favory WHERE user1='$username'");
while($fav = mysql_fetch_assoc($get_favory)) {
$user2[] = $fav['user2'];
}
$values = "'" . implode("','", $user2) . "'";
echo "SELECT * FROM table WHERE added_by IN ($values)";
You should move to PDO or at a minimum the MySQLi extension.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have already seen examples for string manipulation using "explode" but none of them does my job..
I have an array of exploded strings:
$grand_result = 12,5,7,3,2,1 ;
$grandparent_id = explode(",",$grand_result);
i want to insert some values in db having id = $grand_result except first value returned (12) in this case
$grand_result can have many values i want to fetch only first 7 values..
how can i do that ...thanks in advance
you can use array_slice
$output = array_slice($grandparent_id, 0, 7);
$grand_result = "12,5,7,3,2,1" ;
$grandparent_id = explode(",",$grand_result);
first update
$avoid = grandparent_id[0];
foreach ($grandparent_id as $id) {
if ($avoid != $id) {
//run your update query
}
}
second fetch
$query = "select * from `table_name` where id in (".$grand_result.") limit 7";
Set Limit of explode
$grand_result = 12,5,7,3,2,1 ;
$grandparent_id = explode(",",$grand_result,8);
unset($grandparent_id[7]); or $grandparent_id = array_slice($grandparent_id, 0, 7);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Im trying to find the best killer (most kills) out of a kills database.
Put the killerId of $important into an array, and compare it with the rest of the killers. Find the best killer with the weapon $important.
How do I do that?
$array = array();
$index = 0;
while($mData = $q->fetch_assoc())
{
$index++;
$arr = explode('with ', $mData['killText']);
$unimportant = array(" (in paintball)", " (in event)");
$important = str_replace($unimportant, "", $arr[1]);
if(empty($important)) { $important = "Suicide"; }
$array[$important]['Kills']++;
$array[$important]['Gun'] = $important;
$query2 = $mysql->query("SELECT * FROM `kills` WHERE `killText` LIKE '%$important%' AND `killerID` = '". $mData['killerID'] ."'") or die($mysql->error);
while($kData = $query2->fetch_assoc())
{
// put the killerId of $important into an array, and compare it with the rest of the killers. Find the best killer with the weapon $important
}
}
A GROUP BY will do the trick:
"SELECT killerID, COUNT(*) FROM kills WHERE killText LIKE '%$important%' GROUP BY killerID;"
You ca just fetch "killerID" and got the killer with most kills of weapon $important:
$kData = $query2->fetch_assoc();
$kData['killerID'];
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I tried to create a shopping basket in my project, but some problem to handle this.
My project basket must allow duplicate value, but my code auto duplicate last insert to array. Why does this happen, and how to solve this auto duplicate?
<?php
if(isset($_SESSION['basket'])){
$point = count($_SESSION['basket']);
echo " point = ".$point;
$value = end((array_keys($_SESSION['basket'])));
$value++;
}else{
$value = 0;
echo " point = 0";
}
if(isset($_GET['id'] , $_GET['meter'] , $_GET['color'])){
$id = $_GET['id'];
$color = $_GET['color'];
$meter = $_GET['meter'];
$selected_product = array($id , $color , $meter);
list($_SESSION['basket'][$value][0],$_SESSION['basket'][$value][1] , $_SESSION['basket'][$value][2]) = $selected_product;
echo "<pre>";
var_dump($_SESSION['basket']);
echo "</pre>";
}
?>
Though its not exactly clear from your question. To remove duplication in array use array_unique()
You also had a typo at
array($_SESSION['basket'][$value][0],$_SESSION['basket'][$value][1] , $_SESSION['basket'][$value][2]) = $selected_product;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to put a validation for when you get the same date, with the same time and same venue, it will not insert in the database. It will only insert in the database if 1 instance will return false.
$res_date = $_POST['res_date'];
$res_venue = $_POST['res_venue'];
$res_dur = $_POST['res_duration'];
$qryjay = mysql_query("SELECT * FROM tbl_reservation WHERE res_date='$res_date' AND res_dur = '$res_dur' AND res_venue = '$res_venue'");
if ((mysql_num_rows($qryjay) == $res_date) && ($qryjay == $res_dur) && ($qryjay) == $res_venue){
echo "<script language=javascript>alert('".$res_venue." is not available on ".$res_dur." on ".$res_date."!')</script>";
}
Try to fix your code like this:
<?
$res_date = mysql_real_escape_string($_POST['res_date']);
$res_venue = mysql_real_escape_string$_POST['res_venue']);
$res_dur = mysql_real_escape_string$_POST['res_duration']);
$qryjay = mysql_query("SELECT count(*) as countNbr FROM tbl_reservation WHERE res_date='".$res_date."' AND res_dur = '".$res_dur."' AND res_venue = '".$res_venue."'");
$result = mysql_fetch_assoc($qryjay);
if ($result['countNbr'] > 0)){
echo "<script language=javascript>alert('".$res_venue." is not available on ".$res_dur." on ".$res_date."!')</script>";
}
?>