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
$sql = mysql_query("SELECT DISTINCT business_region FROM business WHERE business_category='occupation'");
// count the output amount
$businessCount = mysql_num_rows($sql);
if ($businessCount > 0) {
while ($row = mysql_fetch_array($sql)) {
$business_region = $row["business_region"];
$Region_view .= "display value";
}
}
This gives a following result:
Michigan Toronto Ohio Manchester London Sydney Paris
I want to select only Ohio and London
as a variable value as:
$Region_view .
Perhaps I can use if else structure, but I could not get it to work.
i.e. I need to select only Ohio, London
I can do it using joint MySQL statement but it is too slow so I need and ask for a different way.
$sql = mysql_query("SELECT DISTINCT business_region FROM business WHERE business_category ='occupation'");
// count the output amount
$businessCount = mysql_num_rows($sql);
if ($businessCount > 0) {
while($row = mysql_fetch_array($sql)){
$business_region = $row["business_region"];
if ($business_region == 'Ohio' || $business_region == 'London') // added
$Region_view .= $business_region;//changed
}
}
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
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.
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 8 years ago.
Improve this question
I want to check if I have an hour in the database but If I have more than hour in the database it outputs twice, if I have 3 three times...
In this specific case this gives me: 9 (in blue color) and then 9 (in red color)
$result = mysqli_query($con, 'SELECT * FROM consulta
WHERE professional=1
AND client=0');
while ($row = mysqli_fetch_array($result)) {
if ($row['hora']=='09:00:00') { echo '<p class="blue">9</p>'; }
else { echo '<p class="red">9</p>'; }
}
I have a MySql database with columns: id, professional and hour. I have stored 2 hours and this is what I get if I echo $row['hora'] 09:00:00 and 10:00:00
In this case the if should give me just 9 in blue because it is in the database. I do not understand why it happens. How can I solve this?
This is the structure of the MySql database:
Presumably you are building up a calendar or the like.
The query you are using is returning all rows (and you are looping over each row, regardless of the hour). If you want to check if specific hours exist you could build up an array of hours:
$result = mysqli_query($con, 'SELECT * FROM consulta
WHERE professional=1
AND client=0');
$hours = array();
while ($row = mysqli_fetch_array($result)) {
$hours[$row['hora']] = $row;
}
Now to check a specific hour we can do:
if (!empty($hours['09:00:00'])) {
echo '<p class="blue">9</p>';
}else {
echo '<p class="red">9</p>';
}
Edit: Explanation
In the first loop we are building up an array. This will look like:
$hours = array(
'08:00:00' => $dataForThisRow,
'09:00:00' => $dataForThisRow
);
In the above '08:00:00' is called a key. Each key is a time that was found in the database.
To check if there is an entry in the database for our time we can check if there is a non empty key. Hence we use:
if(!empty($hours['09:00:00'])
This if will run if $hours has a key with the value '09:00:00'. If there is no key then the other part of the if will run.
You are comparing with this value: 09:00:00, but the database seems to have stored this other: 9:00:00
Change your sql query like below:
$query = "SELECT TIME_FORMAT(hora,'%H:%i:%S') FROM consulta
WHERE professional=1
AND client=0";
$result = mysqli_query($con, $query);
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>";
}
?>
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 work on a page(.php) that show some information from databases(like article name,article author).I also have a pagination script that work good(the ideea of pagination script is when I press a link the url of the site goes to something like ?page=2)Now I try to numbering the records that I show form databases.But how can I do that?
Pagination script:
$per_pages = 6;
$nrofarticles_query = mysql_query("SELECT COUNT('id') FROM tbl_articles");
$nrdearticole = mysql_result($nrofarticles_query, 0);
$totalpages = ceil($nrdearticole/$per_pages);
$currentpagedisplay = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$firstpair = ($currentpagedisplay - 1) * $per_pages;
I echo record from database like so:
$articles_set = mysql_query("SELECT * FROM tbl_articles LIMIT $firstpair, $per_pages");
while($article = mysql_fetch_array(article_set)){
echo $article['article_name'];
}
So I show 6 records from database on page change show the next 6 records or if is less than 6 show how many remained .How can I numbering the records like this:
?page=1
1 article_name1
2 article_name2
3 article_name3
....
6 article_name6
?page=2
keep going
7 article_name7
8 article_name8
.....
EDIT
I was thinking to something like
<?php
$nrofarticles = 0;
$articles_set = mysql_query("SELECT * FROM tbl_articles LIMIT $firstpair,$per_pages");
confirm($articles_set);
while($article = mysql_fetch_array($articles_set)){
$nrofarticles ++;
}
echo "<ul id=\"id\">";
for($i = $firstpair+1; $i<=$nrofarticles * $currentpagedisplay; $i++){
echo '<li class="nr_id">' .$i. '<sub>#</sub></li>';
}
echo "</ul>";
?>
It's works but only on first 2 page of 3 because I have 16 articles in total - so on last page $firstpair + 1 will be 13 and $nrofarticles * $currentpagedisplay will be 12(4(articles) * 3(page=3)).
Go use a framework. You'll get pagination (and all sorts of extras) for free.