Am trying to join two tables with the same column but different values but each time i output it duplicates.
Here is my code:
<?php
$dept = $_SESSION['department'];
$dept1 = strtolower($dept);
$dept2 = str_replace(" ", "_", $dept1);
$dept4 = "$dept2" . "_200";
$dept = $_SESSION['department'];
$level = $_SESSION['level'];
$level2 = str_replace (" ", "_", $level);
if($level ="200_level") {
$query = " SELECT * FROM $dept2 Join $dept4";
}
else
{
$query = " SELCT * FROM $dept2";
}
$result = mysql_query($query) or die('<div class="header5"small_font">Your Courses are not available yet. Pls contact the ICT Unit</div>');
while ($row = mysql_fetch_assoc($result)) {
$id = $row['id'];
$course = htmlspecialchars($row['course_name']);
$code = htmlspecialchars($row['course_code']);
$status = $row['status'];
$unit = htmlspecialchars($row['unit']);
?>
If you are looking to get the contents of two tables with the same columns, a MySQL UNION should help
UNION is used to combine the result from multiple SELECT statements into a single result set.
Like so:
$query = "(SELECT * FROM $dept2)
UNION
(SELECT * FROM $dept4)";
Related
Below is my pretty simple attempt at trying to count the number of times $name string occurs in the $related_company string row of a table.
I was hoping it would spit out a number, but when i upload it to my webserver, it doesnt spit out anything.
Any ideas?
<?php
$db = new SQLite3('database.sqlite3');
$red = $db->query('SELECT * FROM news');
$name = strtolower("Tesla");
$related_company = $red->fetchArray($row['related_company']);
while ($row = $red->fetchArray()) {
if (in_array($name, $related_company)){
$count+1;
}}
echo $count;
?>
You can do it all in a single query, without having to loop through the results. SQL has a LIKE operator to perform pattern matching in columns.
$db = new SQLite3('database.sqlite3');
$stmt = $db->prepare("SELECT COUNT(*) AS count FROM news WHERE related_company LIKE :name");
$name = '%' . strtolower("Tesla") . '%';
$stmt->bindParam(':name', $name);
$stmt->execute();
$row = $stmt->fetchArray(SQLITE3_ASSOC);
$count = $row['count'];
echo $count;
If the name comes from another table, you can join the tables.
$result = $db->query("
SELECT c.name, COUNT(*) as count
FROM Companies AS c
JOIN news AS n ON n.related_company LIKE '%' || LOWER(c.name) || '%'");
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
echo "{$row['name']}: {$row['count']}<br>";
}
I need to connect a initiative table with locations one, there is a column at initiative table which ill contain multiple ids or single ids, depends on how much is added. The data will be saved like this:
"1,4,3"
public function getallRIS(){
$connect = $this->connect;
/*
THIS QUERY WORKS WITHOUT MULTIPLE IDS ON THE COLUMN
$qx = "SELECT initiatives.location_id as init_location, initiatives.name as init_name, initiatives.startyear as init_startyear, initiatives.endyear as init_endyear, ST_AsText(locations.location) as locations_coord, locations.name as locations_name
FROM locations
JOIN initiatives on initiatives.location_id = locations.id";
*/
$qx = "SELECT GROUP_CONCAT(initiatives.location_id SEPARATOR ',') as init_location, initiatives.name as init_name, initiatives.startyear as init_startyear, initiatives.endyear as init_endyear, ST_AsText(locations.location) as locations_coord, locations.name as locations_name
FROM locations
JOIN initiatives
ON FIND_IN_SET(locations.id, initiatives.location_id)
GROUP BY initiatives.location_id";
$queryx = "SELECT ST_AsText(ri_location) FROM ri;";
if($query = $connect->query($qx)){ }else{ echo $connect->error; }
$count = $query->num_rows;
$row = 1;
while($fetch = $query->fetch_array(MYSQLI_ASSOC)){
$point = $fetch['locations_coord'];
$point = str_replace(array("POINT(",")"),array("",""),$point);
$point = str_replace(" ",",",$point);
$coord = explode(",",$point);
$lat = $coord[0];
$long = $coord[1];
echo'
{
lat: '.$lat.',
lng: '.$long.',
text: "'; echo "<b style='font-size:17px;'>"; echo''.$fetch['init_name'].'</b><br>Country: '.$fetch['locations_name'].'<br>Info: '; echo"<a href=''>"; echo'View</a><br>Start Year: '.$fetch['init_startyear'].'<br>End Year: '.$fetch['init_endyear'].'"
},
';
$row++;
}
}
However is not working properly , shows only first item of each column
I have a database have 20 tables I want to search all of these I got stuck that how can I search this and solve this query help is very appreciated. Like:
table1
table2
table3
table4
and so one. This is my script.
<?php
if(isset($_GET["search"]))
{
$condition = '';
//$query = explode(" ", $_GET["search"]);
$query = explode(" ", $_GET["search"]);
foreach($query as $text)
{
$condition .= "`title` LIKE +'%".mysqli_real_escape_string($connect, $text)."%' OR ";
}
$condition = substr($condition, 0, -4);
$sql_query = "SELECT * FROM countries2 WHERE " . $condition;
$result = mysqli_query($connect, $sql_query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
echo '<tr><td>'.$row["title"].'</td></tr>';
}
}
else
{
echo '<label>Data not Found</label>';
}
}
?>
Try with this
sql_query = "SELECT * FROM `countries`,`countries2` WHERE " . $condition;
i have noticed that you are making a search directory or something like this.
you may use FULLTEXT SEARCH with operators and Stemming
after this your query will look like this.
implement as per your requirement ;).
(SELECT * FROM table1 WHERE(col1,col2,clo3) AGAINST(."$search".) IN NATURAL LANGUAGE MODE)
I am trying to get the key value from the multidimensinal array which I have created using .The Array snapshot is given after the Code.
Below is my PHP code-
$selectTicket = "select ticketID from ticketusermapping where userID=$userID and distanceofticket <=$miles;";
$rsTicket = mysqli_query($link,$selectTicket);
$numOfTicket = mysqli_num_rows($rsTicket);
if($numOfTicket > 0){
$allRowData = array();
while($row = mysqli_fetch_assoc($rsTicket)){
$allRowData[] = $row;
}
$key = 'array(1)[ticketID]';
$QueryStr = "SELECT * FROM ticket WHERE ticketID IN (".implode(',', array_keys($key)).")";
Array Snapshot-
I need the tickedID value from this array . Like the first one is 49 .
Please help.
change your code like
$selectTicket = "select ticketID from ticketusermapping where userID=$userID and distanceofticket <=$miles;";
$rsTicket = mysqli_query($link, $selectTicket);
$numOfTicket = mysqli_num_rows($rsTicket);
if ($numOfTicket > 0) {
$allRowData = array();
while ($row = mysqli_fetch_assoc($rsTicket)) {
$allRowData[] = $row['ticketID'];
}
$QueryStr = "SELECT * FROM ticket WHERE ticketID IN (" . implode(',', $allRowData) . ")";
$ids = array_column( $allRowData, 'ticketID'); //this will take all ids as new array
$QueryStr = "SELECT * FROM ticket WHERE ticketID IN (".implode(',', $ids).")";
You should do a single query using JOIN for this:
$query = "
SELECT t.*
FROM ticket t
JOIN ticketusermapping tum
ON t.ticketID = tum.ticketID
AND tum.userID = '$userID'
AND tum.distanceofticket <= '$miles'
";
$stmt = mysqli_query($link, $query);
$numOfTickets = mysqli_num_rows($stmt);
while($row = mysqli_fetch_assoc($stmt)){
var_dump($row); // here will be the ticket data
}
In the below code i want to join or implode all arrays of $trackersurl in a single line. i am getting the results in different lines, so i want to join in a single line.
Can anyone help me out?
I am searching results in stackoverflow, but could not follow.
My code is in below:
$sql = "SELECT * FROM announce WHERE torrent = $id ORDER BY seeders DESC";
$query = #mysql_query($sql);
while ($result = #mysql_fetch_array($query)) {
$trackersurl1 = $result['url'];
$trackersurl2 = "&tr=".$trackersurl1;
$trackersurl = array($trackersurl2);
}
Results of [var.trackersurl] in html page is below:
&tr=http:ajgdsjhg/ann
&tr=udp://iuysidfu/ann
&tr=udp:wutefghgw/ann
&tr=http://sdhgsjdhgj/ann
I want to join them in a single line below
&tr=http:ajgdsjhg/ann&tr=udp://iuysidfu/ann&tr=udp:wutefghgw/ann&tr=http://sdhgsjdhgj/ann
You should be careful of sql injection.
Are you looking to create an array['trackers'] with a string of all the trackers for a magnet link?
<?php
$sql = "SELECT * FROM announce WHERE torrent = ".mysql_real_escape_string($id)." ORDER BY seeders DESC";
$query = mysql_query($sql);
$tracker = null;
if(mysql_num_rows($query)>=1){
while ($result = mysql_fetch_array($query)) {
$tracker .= "&tr=".$result['url'];
}
}
$tracker = array('trackers'=>$tracker);
//$tracker['trackers'] = "&tr=a.com&tr=b.com&tr=c.com";
?>
Try this code
$newArray=array();
while ($result = #mysql_fetch_array($query)) {
$trackersurl1 = $result['title'];
$newArray[] = "&tr=".$trackersurl1;
}
$urlString=implode('',$newArray);