hi i am trying to write a php to grab a specific category of post in a wordpress database, this is what i got so far:
$q = "select * from table1 where column like 'condition'";
$r = mysql_query($q);
$id = array();
if($r){
while ($row = mysql_fetch_array($r)) {
$link = $row["object_id"];
$id[] = array(
"postid"=>$link,
);
}
}
else{
echo mysql_error();
}
now i am trying to use the result i got from pervious code to search another table to get something else. i am new to php so i am hoping to get some help
thanks in advance
Related
I'm trying to learn how to put a useful search function on my site, where users can affix certain tags to their database entries. The tags live in a keyword column which holds the data as a comma separated string (ie red, car, apple), that (in theory) can be searched for at a later time.
When I construct my sql search like this:
$sql = "SELECT * FROM reporting WHERE (keywords LIKE '%" . $search. "%' )
AND user_id = $user_id
ORDER BY spc_class";
//-run the query against the mysql query function
$result = mysqli_query($conn, $sql);
//-create while loop and loop through result set
$colNames = array();
$data = array();
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
$colNames = array_keys(reset($data));
} else {
echo "0 results";
}
It works reasonably well, except if someone searches for multiple words. That problem has been covered extensively in these forums, and it seems that most answers recommend converting the column to be searched to a full text value format in mysql.
I have done this, but when I change my search query like this, I get a non-object found error lwhen using the MATCH AGAINST construct:
$sql = "SELECT * FROM reporting WHERE MATCH(keywords) AGAINST('".$search."')
AND user_id = $user_id
ORDER BY spc_class";
//-run the query against the mysql query function
$result = mysqli_query($conn, $sql);
//-create while loop and loop through result set
$colNames = array();
$data = array();
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
$colNames = array_keys(reset($data));
} else {
echo "0 results";
}
Why am I able to execute the mysql search using the WHERE LIKE construct, but not the MATCH AGAINST query? Does the latter not return an object? I can't seem to figure out my error in using the full-text search query.
This question already has answers here:
How can I get an unknown username given an ID?
(2 answers)
Closed 12 months ago.
I have a search function on my site which displays results from a MySQL db. Each search result is linked to a dynamic php page with the URL, i.e. /details.php?id=123.
I need the details.php page to get the ID (e.g 123) from the URL and then fetch all rows from the database with this ID, storing them in a variable for use later. I then need to be able to echo the rows at various points throughout the page to populate the content.
The code I have so far is:
<?php
$db = mysql_connect("","","") or die("Database Error");
mysql_select_db("items",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `items- table` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
?>
I’m fairly new to PHP so not sure if the code above will get all rows based on the ID, and then how to echo the rows within divs on the page?
You can use mysql_fetch_array() OR mysql_fetch_assoc() function with while loop (if multiple rows there) OR without while loop if there is only one row.
$query = "SELECT * FROM `items- table` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
while($fetch = mysql_fetch_array($result))
{
echo "<div>".$fetch['column_name']."</div>";
}
Almost correct. Just add something like this:
while ($row = mysql_fetch_assoc($result)) {
echo '<div>';
foreach ($row as $key => $val) {
echo $key.' = '.$value.'<br/>';
}
echo '</div>';
}
I've been trying to run queries through my website(online) and managed to do it with INSERT , DELETE and UPDATE but I've got some problems with SELECT... Not sure how to print the result. Here what I tried:
$execute = mysqli_query($con, $query);
$res = mysqli_fetch_array($execute);
foreach($res as $key=>$value) {
echo $key.' '.$value.'<br>';
}
That prints only one row..
also tried:
while($row = mysqli_fetch_array($execute))
but still nothing.So let's say for example i've got a table users with ~50 rows and columns - id, name and username. And when I run:
SELECT * FROM users
I want to print everything from that table.
Can someone help me with that?
Are you looking for something like this?
<?php
$execute = mysqli_query($con, $query);
while($recSet = mysqli_fetch_array($execute)) {
echo $recSet['id'].' - '.$recSet['name'].' - '.$recSet['username'];
}
I can't figure out why this keeps breaking my page. Could someone take a look? Thanks!
while ($row = mysql_fetch_array(mysql_query("SELECT * FROM `mytable` WHERE `col1` = 0"))) {
echo $row['id'];
}
I've seen this type of while loop show up pretty regularly in google searches and just browsing through stackoverflow. I don't know why it isn't working for me.
How would I achieve the desired result? (Echo the id of each row where col1 = 0)
Obviously it is because you create new MySQL resource with every iteration and then use it's first row.
Use something like
$res = mysql_query("...");
while ($row = mysql_fetch_array($res)) {
echo $row['id']'
}
By the way, you do know that mysql is deprecated, do you not?
$result = mysql_query("SELECT * FROM `mytable` WHERE `col1` = 0");
while ($row = mysql_fetch_array($result)) {
echo $row['id'];
}
I am writing a web application in PHP which will store employee data and generate employee ID cards to PDF. I am using FPDF for creation of PDFs and that works fine. I am having a problem with showing results from MySQL database.
I have to generate PDF with 4 employee ID cards and I am not sure how to get them from the database. So far I am using LIMIT option in the query to get only 4 results and i will have an if statement based on mysql.php?id=1 id which will define the limit. It is a little messy but there are not going to be more than 80 employees.
This is my code:
$id = $_GET['id'];
if ($id == 1) {
$limit_start = 0;
$limit_end = 4;
}
$result=mysql_query("SELECT users.tajemnik, users.dateCreated, users.showmeID,
users.workerName, users.dateCreated, users.workerPlace, users.workerSID, uploads.userID, uploads.data, uploads.filetype
FROM users INNER JOIN uploads ON users.showmeID = uploads.userID ORDER BY workerName DESC LIMIT $limit_start, $limit_end") or die (mysql_error());
mysql_query("SET NAMES 'utf8'") or die('Spojení se nezdařilo');
while($row = mysql_fetch_array($result)){
$workerName = $row["workerName"];
$workerPlace = $row["workerPlace"];
$workerSID = $row["workerSID"];
$tajemnik = $row["tajemnik"];
$showmeID = $row["showmeID"];
$mysqldatetime = strtotime($row['dateCreated']);
$image = $row["data"];
$phpdatetime = date("d.m.Y",$mysqldatetime);
}
This will get me the first result from the query. I need to get information from all 4 rows and have them stored in variables like $workerName1, $workerName2 etc. I hope it makes sense what I am trying to do.
Thank you for your replies!
V.
I need to get variables like $workerName1, $workerName2 etc
Nope, you don't.
You actually need an array.
So, first, get yourself a function
function sqlArr($sql){
$ret = array();
$res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
if ($res) {
while($row = mysql_fetch_array($res)){
$ret[] = $row;
}
}
return $ret;
}
then write a code
mysql_query("SET NAMES 'utf8'") or die('Spojení se nezdařilo');
$sql = "SELECT users.tajemnik, users.dateCreated, users.showmeID, users.workerName,
users.dateCreated, users.workerPlace, users.workerSID, uploads.userID,
uploads.data, uploads.filetype
FROM users
INNER JOIN uploads ON users.showmeID = uploads.userID
ORDER BY workerName DESC LIMIT $limit_start, $limit_end";
$data = sqlArr($sql);
Now you have all your data in the $data array.
So, you can loop over it or access single values like
echo $data[0]['tajemnik'];
or
foreach($data as $row) {
//do whatever you want with database row
echo $row['tajemnik'];
}