Good day,
I have a problem with following code, I have to limit companies ID_CL (it`s outcome). I want to limit the display to the only one ID_CL (one because , right now it displays everything) is it possible ?
Sorry for stupid question, I usually don't work with mysql/php
{
//$size_h =1;
list($list_num, $size_w, $size_h) = $this->getImageDefSize('list');
//$table = TB_JOB." jb LEFT JOIN ".TB_CL." cl USING(".ID_CL.") ";
$table = TB_JOB." jb LEFT JOIN ".TB_CL." cl USING(".ID_CL.") ";
$field = "jb.".ID_JOB.", jb.job_code, jb.shop_name, jb.job_name, jb.intro, jb.video_url, jb.salary, jb.area_0, jb.area_1, jb.area_2, jb.biz_0, jb.biz_1, jb.biz_2, jb.emid, jb.tel, jb.email, jb.access, jb.nenshu_range, jb.sv_1, jb.company, jb.worktime, jb.holiday";
$field .= ", jb.rail_1_0, jb.rail_1_1, jb.rail_1_2, jb.rail_2_0, jb.rail_2_1, jb.rail_2_2, jb.free_1_0, jb.free_1_1, jb.free_2, jb.free_3, jb.moddate, jb.regdate, jb.contents, jb.qualification,jb.zip,jb.pfid,jb.address1,jb.address2";
$field .= ", cl.".ID_CL.", cl.cl_name, cl.business, cl.email AS client_email ";
//$field .= ", cl.".ID_CL.", cl.cl_name, cl.business, cl.email AS client_email ";
if (! empty($_SESSION["srch"]["new"])) {
$od = " ORDER BY jb.setdate DESC, cl.rank, jb.jobid ASC";
} else {
$od = " ORDER BY cl.rank, jb.moddate DESC, jb.jobid ASC";
}
//$total = $this->countTB($table, ID_JOB, $this->wh);
$total = $this->countTB($table, ID_JOB, $this->wh);
\
if ($total > 0) {
// $_SESSION["srch"]["idlist"] = $this->arrayTB($table, ID_JOB, $this->wh.$od);
$_SESSION["srch"]["idlist"] = $this->arrayTB($table, ID_JOB, $this->wh.$od);```
Yes, you can use MySQL LIMIT for this purpose.
According to your query, After , jb.jobid ASC you should add LIMIT 1
This will help you.
I have the following code
$sql = "SET #uid := (SELECT ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);";
$sql = "UPDATE channels SET Used = 1 WHERE ID = #uid;";
$sql = "SELECT * FROM channels WHERE ID IN = #uid;";
$result = mysqli_multi_query($conn, $sql)
or die( mysqli_error($sql) );
if (mysqli_num_rows($result) > 0) {
$text = '';
while($row = mysqli_fetch_assoc($result)) {
$Channel_Location = $row['Channel_Location'];
$text = $text . $Channel_Location;
}
}
Now the issue i'm having is the php isnt displaying the result returned by the MYSQL query which is stored in a session later on in the code to be displayed on a dummy page it comes up with the following error
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result
The my SQL query does exactly what I need it to I just need to, so I don't really want to change it. I just need some advice on how i'd get the PHP to echo the #uid
is there anyone willing to help me solve the issue? if so thankyou.
You have 3 queries in your $sql so you should use multi_query function
http://php.net/manual/en/mysqli.multi-query.php
And you can change your first query to:
SET #uid = 0;
SELECT #uid := ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);
Update You can try this fragment of your code modified with all commented improvements.
$sql = 'SET #uid = 0;';
$sql .= 'SELECT #uid:= ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);';
$sql .= 'UPDATE channels SET Used = 1 WHERE ID = #uid;';
$sql .= 'SELECT * FROM channels WHERE ID IN = #uid;';
if (mysqli_multi_query($conn, $sql)) {
do {
$result = mysqli_store_result($conn);
} while(mysqli_next_result($conn));
if (mysqli_num_rows($result) > 0) {
$text = '';
while($row = mysqli_fetch_assoc($result)) {
$Channel_Location = $row['Channel_Location'];
$text = $text . $Channel_Location;
}
}
} else {
die( mysqli_error($conn) );
}
i have a simple query who select me 3 news from table, but i wont to change this number from other file whith variable.
So this is query:
$query = 'SELECT * FROM news ORDER BY created_at DESC LIMIT 3';
I tried differently, but did not work...
Help please
My code(i can't answer on my question so i add it here)
$newsAmount = 3;
function get_news() {
$query = "SELECT * FROM news ORDER BY created_at DESC LIMIT $newsAmount";
$result = mysql_query($query);
$news = array();
while ($row = mysql_fetch_array($result)) {
$news[] = $row;
}
return $news;
if (!$result) {
trigger_error('Invalid query: ' . mysql_error() . " in " . $query);
}
}
This is this code.
Actually you can just do this:
$query = "SELECT * FROM news ORDER BY created_at DESC LIMIT $newsAmount";
But make sure to keep the string in double quotes so the variable can be evaluated, Single quotes will be printed out as it is.
Try to echo $query, you will notice that its being printed.
Try this:
$newsAmount = 3;
$query = 'SELECT * FROM news ORDER BY created_at DESC LIMIT ' + $newsAmount;
you cannot return an array like you did in your code.
I would suggest to do the query inside your main code instead of writing it in a function.
I have the following code and all of the search functions work except for the title field. So I can search by genre, date, location etc... but not by title. When attempting to search by title nothing is returned at all. Can anyone help me with this?
Also, is there a more efficient way to count all the fields before limiting it for use in pagination later on?
$today = date("Y-m-d");
$query = "SELECT * FROM TABLE_NAME WHERE Date >= '$today'";
$bind = Array();
if ($_GET["Title"] && $_GET["Title"] != "") {
$query .= " and Title like %?%";
$bind['Title'] = $_GET['Title'];
}
if ($_GET["Genre"] && $_GET["Genre"] != "") {
$query .= " and Genre like %?%";
$bind['Genre'] = $_GET['Genre'];
}
if ($_GET["Location"] && $_GET["Location"] != "") {
$query .= " and Location like %?%";
$bind['Location'] = $_GET['Location'];
}
if ($_GET["Date"] && $_GET["Date"] != "") {
$query .= " and Date = %?%";
$bind['Date'] = $_GET['Date'];
}
$stmt = $db->prepare($query);
$stmt->execute($bind);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$num = count($rows);
$query .= " ORDER BY Date LIMIT $limit, 9";
$stmt = $db->prepare($query);
$stmt->execute($bind);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
Edit: After everyone's help I thought I would post my now revised code for future reference. It turns out the other fields were not working, but instead due to the if statement all this was nested in the code simply wasn't being executed.
$today = date("Y-m-d");
$query = "SELECT * FROM TABLE_NAME WHERE Date >= '$today'";
$countq = "SELECT count(*) FROM TABLE_NAME WHERE Date >= '$today'";
$bind = Array();
if ($_GET["Title"] && $_GET["Title"] != "") {
$query .= " and Title like :title";
$countq .= " and Title like :title";
$bind[':title'] = "%{$_GET['Title']}%";
}
if ($_GET["Genre"] && $_GET["Genre"] != "") {
$query .= " and Genre like :genre";
$countq .= " and Genre like :genre";
$bind[':genre'] = "%{$_GET['Genre']}%";
}
if ($_GET["Location"] && $_GET["Location"] != "") {
$query .= " and Location like :loc";
$countq .= " and Location like :loc";
$bind[':loc'] = "%{$_GET['Location']}%";
}
if ($_GET["Date"] && $_GET["Date"] != "") {
$query .= " and Date = :date";
$countq .= " and Date = :date";
$bind[':date'] = "{$_GET['Date']}";
}
$stmt = $db->prepare($countq);
$stmt->execute($bind);
$rows = $stmt->fetchAll();
$num = count($rows);
$query .= " ORDER BY Date LIMIT $limit, 9";
$stmt = $db->prepare($query);
$stmt->execute($bind);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
all of the search functions work
With the given query it is not true
From PDO tag wiki:
placeholders cannot represent an arbitrary part of the query, but a complete data literal only. Neither part of literal, nor whatever complex expression or a syntax keyword can be substituted with prepared statement.
Prepare FULL literal first: $name = "%$name%"; and then bind it.
As for the "more" efficient method for pagination - yes, oh yes.
With your current way of counting data you don't actually need other queries. as you have ALL the data already and can paginate it as well.
But of course it will pollute all the memory soon. So, if you want to get a count of rows from database, get the very count: run the same query but instead of SELECT * make it "SELECT count(*)
There are not any errors returned, that's why I am so confused
From PDO tag wiki again:
It is essential to set ERRMODE_EXCEPTION as a connection option as it will let PDO throw exceptions on connection errors. And this mode is the only reliable way to handle PDO errors.
Thank you, very nice solution, that was answered my question.
How can I use your solution if my $sql is as follows:
$sql = "SELECT * FROM $tbl_main, $tbl_country, $tbl_members
WHERE ($tbl_main.country_id = $tbl_country.country_id) AND ($tbl_main.member_id = $theselect) ORDER BY $chosenTable.$orderby LIMIT $startpoint, $limit";
i.e. how can I use your solution somewhere in the middle of a code.
You can conditionally concatenate the extra condition:
$sql = "SELECT * FROM $tbl_main, $tbl_country, $tbl_members
WHERE ($tbl_main.country_id = $tbl_country.country_id)";
if(isset($theselect)) {
$sql .= " AND ($tbl_main.member_id = $theselect)";
}
$sql .= " ORDER BY $chosenTable.$orderby LIMIT $startpoint, $limit";