hello there I have PHP query that Fetch Records from a database with where condition I have four pages in WebSite I want to Fetch data from DataBase with where Condition Ever pages Fetch own Content From database Through link It's my query that's not work
Prefix already in config file
$prefix = 'v1_';
$link = $p['link'] ;
$data_query = $db->query("
SELECT * FROM ".$prefix.$key."
WHERE "$p['link'] = $link "
");
In WebSite pages are Define with key $x
$x = 'about';
$pages[$x]['title'] = 'About Us';
$pages[$x]['link'] = 'about_us';
$pages[$x]['source'] = 0;
and same variable and own info in both pages
$x = 'objects';
$x = 'services';
and I'm using this query in website its work fine but now I want to change that I mention above details
$data_query = $db->query("
SELECT * FROM ".$prefix.$key."
WHERE online = 1 ;
");
Any contributors to this post, I thank you for your help :)
Try this:
$data_query = $db->query("SELECT * FROM {$table} WHERE link = {$link}");
you can use LIKE query
eg.
SELECT * <TABLE_NAME> WHERE COLUMN_NAME LIKE '<prefix>%'
For multiple where condition read this answere
Related
I am having problems achieving the query to select data from a table in the db after a defined value has been met.
My code to do this is:
$fi = 'first_segment'
$im = popo.jpg
$sqls = "SELECT * FROM $fi,news_pictures
WHERE $fi.pi_id = news_pictures.pi_id
AND news_pictures.i_name = '$im'
GROUP BY news_pictures.id DESC";
I wasn't able to achieve the result with that query.
Basically, I want the query to confirm if news_pictures.i_name = '$im' and if true, return starts from the value of $im followed by other data in the table depending on news_pictures.id DESC.
The sample data and output:
Table news_pictures:
id i_name
-- ------
1 coco.jpg
2 lolo.jpg
3 popo.jpg
4 dodo.jpg
Since $im = popo.jpg, I want my query to display all values starting from popo.jpg with id DESC, i.e. popo.jpg, lolo.jpg, coco.jpg.
I got to solve the question with the help of a friend.
$fsqls = "SELECT * FROM $fi,news_pictures WHERE $fi.pi_id = news_pictures.pi_id AND news_pictures.i_name = '$im' GROUP BY news_pictures.id";
$rres = mysqli_query($connection, $fsqls) or print(mysqli_error($connection));
while($row = mysqli_fetch_assoc($rres))
{
$rnm = $row["id"];
}
$sqls = "SELECT * FROM news_pictures WHERE news_pictures.id <= $rnm ORDER BY news_pictures.id DESC";
I'm trying to create a pagination for my PDO query. I cant figure it out. I've tried numerous google searches, but nothing that will work for me. [I probably didn't search hard enough. I'm not sure]
This is my code:
$sql2 = "SELECT * FROM comments WHERE shown = '1'ORDER BY ID DESC";
$stm2 = $dbh->prepare($sql2);
$stm2->execute();
$nodes2= $stm2->fetchAll();
foreach ($nodes2 as $n1) {
echo "text";
}
I want to be able to limit 10 comments per page, and use $_GET['PAGE'] for the page.
Something that I tried
$sql2 = "SELECT * FROM comments WHERE shown = '1'ORDER BY ID DESC";
$stm2 = $dbh->prepare($sql2);
$stm2->execute();
$nodes2= $stm2->fetchAll();
$page_of_pagination = 1;
$chunked = array_chunk($nodes2->get_items(), 10);
foreach ($chunked[$page_of_pagination] as $n1) {
echo "text";
}
If someone could help out, I appreciate it.
You need to limit the query that you are performing, getting all values from the database and then limiting the result to what you want is a bad design choice because it's highly inefficient.
You need to do this:
$page = (int)$_GET['PAGE']; // to prevent injection attacks or other issues
$rowsPerPage = 10;
$startLimit = ($page - 1) * $rowsPerPage; // -1 because you need to start from 0
$sql2 = "SELECT * FROM comments WHERE shown = '1' ORDER BY ID DESC LIMIT {$startLimit}, {$rowsPerPage}";
What LIMIT does:
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants
More information here: http://dev.mysql.com/doc/refman/5.7/en/select.html
Then you can proceed getting the result and showing it.
Edit after comment:
To get all the pages for display you need to know how many pages are there so you need to do a count on that SELECT statement using the same filters, meaning:
SELECT COUNT(*) as count FROM comments WHERE shown = '1'
Store this count in a variable. To get the number of pages you divide the count by the number of rows per page you want to display and round up:
$totalNumberOfPages = ceil($count / $rowsPerPage);
and to display them:
foreach(range(1, $totalNumberOfPages) as $pageNumber) {
echo '' . $pageNumber . '';
}
So I am trying to use a result from an SQL search $search_course and then having that as the WHERE LIKE part of the next statement $Search_Module.
once the $Search_Module query has finished I would like the results to be in a list which I know should be coded correctly.
I have tried the following code but the search returns no values;
Any help with this would be much appreciated, thanks in advance.
$search_course = "
SELECT title, id, wyl, overview, module, course, careers
FROM course
LEFT JOIN cm
ON course.id=cm.course
WHERE id LIKE '%".$_POST['submitcourseselection']."%'";
$result = $mysqli->query($search_course) or die($mysqli->error);
$display_course = $result->fetch_assoc();
//Searches the module table in the DB for modules within the course
$Search_Module = "
SELECT id, title, level, credits
FROM module
WHERE id LIKE '".$search_course['module']."'";
$M_Results = $mysqli->query($Search_Module) or die($mysqli->error);
$ModuleList = '';
while ($MResults = $M_Results->fetch_assoc()) {
$ID = $MResults['id'];
$Title = $MResults['title'];
$Level = $MResults['level'];
$Credits = $MResults['credits'];
$ModuleList.='<div><h2>'.$Title.'</h2></div>';
}
you don't need LIKE in your second query and you can do all by one query.
$search_course = "
SELECT id, title, level, credits
FROM module
WHERE id IN (SELECT module
FROM course
LEFT JOIN cm
ON course.id=cm.course
WHERE id LIKE '%".$_POST['submitcourseselection']."%'")";
in your where clause WHERE id LIKE '".$search_course['module']."'";, you are referencing an array location of a string $search_course, which is your SQL statement.
I think you meant to reference your result array $display_course, so try this:
$search_course = "
SELECT title, id, wyl, overview, module, course, careers
FROM course
LEFT JOIN cm
ON course.id=cm.course
WHERE id LIKE '%".$_POST['submitcourseselection']."%'";
$result = $mysqli->query($search_course) or die($mysqli->error);
$display_course = $result->fetch_assoc();
//Searches the module table in the DB for modules within the course
$Search_Module = "
SELECT id, title, level, credits
FROM module
WHERE id LIKE '".$display_course['module']."'";
$M_Results = $mysqli->query($Search_Module) or die($mysqli->error);
$ModuleList = '';
while ($MResults = $M_Results->fetch_assoc()) {
$ID = $MResults['id'];
$Title = $MResults['title'];
$Level = $MResults['level'];
$Credits = $MResults['credits'];
$ModuleList.='<div><h2>'.$Title.'</h2></div>';
}
I have two tables (Table1 and Table2). Table1 displays data on Page1 and Table2 displays data on Page2. My script sends a respective row and its ID from Table1 to Table2, when user clicks a button. So, I want to check and prevent from displaying the row from Table1 that already exist in Table2.
The ID from Table1 is "inbox_id" and it goes to Table2 as a reference.
Here is the PHP code
$get_inbox = "select * from inbox";
$run_inbox = mysqli_query($conn, $get_inbox);
while($inbox_row = mysqli_fetch_array($run_inbox)){
$inbox_id = $inbox_row['inbox_id'];
$inbox_icon = $inbox_row['inbox_icon'];
$inbox_fullname = $inbox_row['inbox_fullname'];
$inbox_email = $inbox_row['inbox_email'];
$inbox_phone = $inbox_row['inbox_phone'];
$inbox_subject = $inbox_row['inbox_subject'];
$inbox_url = $inbox_row['inbox_url'];
$inbox_message = $inbox_row['inbox_message'];
$inbox_icon = $inbox_row['inbox_icon'];
$inbox_day = $inbox_row['inbox_day'];
$inbox_date = $inbox_row['inbox_date'];
$inbox_year = $inbox_row['inbox_year'];
$inbox_time = $inbox_row['inbox_time'];
$inbox_timezone = $inbox_row['inbox_timezone'];
{
Here is two screenshots of Table1 and Table2.
Table1
Table2
Take your pick:
SELECT
inbox.*
FROM
inbox
LEFT JOIN trash
ON inbox.inbox_id = trash.inbox_id
WHERE
trash.trash_id IS NULL
SELECT
inbox.*
FROM
inbox
WHERE
inbox_id NOT IN (
SELECT inbox_id
FROM trash
WHERE 1 -- something here?
)
SELECT
inbox.*
FROM
inbox
WHERE
NOT EXISTS (
SELECT 1
FROM trash
WHERE trash.inbox_id = inbox.inbox_id
)
Some light reading to help you decide: http://explainextended.com/2009/09/18/not-in-vs-not-exists-vs-left-join-is-null-mysql/
Additionally, you might be able to save some space/effort, if you want all the fields coming back from the DB to live in variables of the same name, you can replace this:
while($inbox_row = mysqli_fetch_array($run_inbox)){
$inbox_id = $inbox_row['inbox_id'];
$inbox_icon = $inbox_row['inbox_icon'];
$inbox_fullname = $inbox_row['inbox_fullname'];
$inbox_email = $inbox_row['inbox_email'];
$inbox_phone = $inbox_row['inbox_phone'];
$inbox_subject = $inbox_row['inbox_subject'];
$inbox_url = $inbox_row['inbox_url'];
$inbox_message = $inbox_row['inbox_message'];
$inbox_icon = $inbox_row['inbox_icon'];
$inbox_day = $inbox_row['inbox_day'];
$inbox_date = $inbox_row['inbox_date'];
$inbox_year = $inbox_row['inbox_year'];
$inbox_time = $inbox_row['inbox_time'];
$inbox_timezone = $inbox_row['inbox_timezone'];
{
with
while($inbox_row = mysqli_fetch_array($run_inbox)){
extract($run_inbox);
{
See extract()
When you draw table in the Table2, store the inbox_id as id of the table row. So when you want insert data in table2, use the inbox_id to check whether it exist using jquery and if not then add the row at the end of table.
As I gone through your question. I will request you to put screen sort of your Table Structure(Table 1, Table 2) with records of possible. So will easy for any one to answer your query. Looking forward to your response. Try to execute this code in your page. I hope this will work.
$get_inbox = "select * from inbox where inbox_id not in($clicked_id)";
$run_inbox = mysqli_query($conn, $get_inbox);
while ($inbox_row = mysqli_fetch_array($run_inbox)) {
$inbox_id = $inbox_row['inbox_id'];
$inbox_icon = $inbox_row['inbox_icon'];
$inbox_fullname = $inbox_row['inbox_fullname'];
$inbox_email = $inbox_row['inbox_email'];
$inbox_phone = $inbox_row['inbox_phone'];
$inbox_subject = $inbox_row['inbox_subject'];
$inbox_url = $inbox_row['inbox_url'];
$inbox_message = $inbox_row['inbox_message'];
$inbox_day = $inbox_row['inbox_day'];
$inbox_date = $inbox_row['inbox_date'];
$inbox_year = $inbox_row['inbox_year'];
$inbox_time = $inbox_row['inbox_time'];
$inbox_timezone = $inbox_row['inbox_timezone'];
}
I am having a small trouble retrieving results that I hope someone can help me with.
I have a field called $incategory which is a comma based string, and what I want to do is explode the into an array that can be used to retrieve results as below (Hope that makes sense):
<?php
$showlist = $row_listelements['incategory'];
// ** e.g. $incategory = 1,3,5,
// ** What I want to do is look at table 'category'
// ** and retrieve results with an 'id' of either 1, 3 or 5
// ** Display Results
mysql_select_db($database_db, $db);
$query_display = "SELECT * FROM category WHERE id = ".$showlist." ORDER BY name ASC";
$display = mysql_query($query_display, $db) or die(mysql_error());
$row_display = mysql_fetch_assoc($display);
$totalRows_display = mysql_num_rows($display);
?>
You can use the IN keyword of SQL directly like this.
query_display = "SELECT * FROM category WHERE id IN (".$showlist.") ORDER BY name ASC";
Another tip would be to stop using MYSQL_QUERY as it is deprecated in PHP 5.3
Edit: If $showlist = '1,3,5,' you will need to remove the last comma from the string to make it useable in the query. Just use this query then
query_display = "SELECT * FROM category WHERE id IN ('".str_replace(",", "','", substr($showlist, -1))."') ORDER BY name ASC";
Use explode function and use , as delimiter.
refer here http://www.w3schools.com/php/func_string_explode.asp
Hope this helps.
First, you have explode the $incategory string into an array containing all of the category number. For example:
$incategory = explode(",", $incategory);
And then you just have to execute this query:
$query_display = "SELECT * FROM category WHERE id = "
. $incategory[$i] . " ORDER BY name ASC";
The $i should be defined beforehand (usually using loop).