How can i display search results using mysqli multiquery. I want to display values from my listing-details table and from my user table. Here is my code:
$searchquery="SELECT * FROM `listing-details` WHERE `listing-address` LIKE '%" . $address . "%' AND `listing-address-street` LIKE '%" . $street . "%' AND `listing-address-barangay-id` LIKE '%" . $barangay . "%'";
$searchquery.= "SELECT `user.user-username`, `user.user-firstname`, `user.user-lastname`, `listing-details.user-username` FROM `user`, `listing-details` WHERE `listing-details.user-username`=`user.user-username`";
if (mysqli_multi_query($conn, $searchquery)) {
do {
if ($result=mysqli_store_result($conn,$searchquery)){
while($row=mysqli_fetch_row($result)){
$listingid =$row['listing-id'];
$username =$row['user-username'];
$listingbedquantity =$row['listing-bedquantity'];
$listingbedtype =$row['listing-bedtype-id'];
$listingguestsquantity =$row['listing-guestsquantity'];
$listingplacetype =$row['listing-placetype-id'];
$listingpropertytype =$row['listing-propertytype-id'];
$listingbathroomquantity =$row['listing-bathroomquantity'];
$listingaddress =$row['listing-address'];
$listingstreet =$row['listing-address-street'];
$listingbarangay =$row['listing-address-barangay-id'];
$listingamenities =$row['listing-amenities-basic-id'];
$listingsafetyamenities =$row['listing-amenities-safety-id'];
$listingsaphotos =$row['listing-amenities-safety-photos-id'];
$listingspace =$row['listing-space-id'];
$listinglandmark =$row['listing-landmark'];
$listingpreferences =$row['listing-preferences-id'];
$listingphotoset =$row['listing-photosset-id'];
$listingexperience =$row['listing-experience-id'];
$listingfrequency =$row['listing-frequency-id'];
$listingstartdate =$row['listing-startdate'];
$listingrate =$row['listing-rate-id'];
$listingprice =$row['listing-price'];
$listingrules =$row['listing-rules-id'];
$listingtitle =$row['listing-title'];
$listingdescription =$row['listing-description'];
$firstname =$row['user-firstname'];
$lastname =$row['user-lastname'];
echo "<ul>\n";
echo "<li>"."" . "<h2>" . $listingtitle . "</h2></li>\n";
echo "<li><h6>" . $listingaddress . ", " . $listingstreet . ", " . $listingbarangay . "</h6></li>";
echo "<li><i>" . $listingdescription . "</i></li>";
echo "<ul>\n";
echo "<li>"."" . "<h2>" . $listingtitle . "</h2></li>\n";
echo "<li><h6>" . $listingaddress . ", " . $listingstreet . ", " . $listingbarangay . "</h6></li>";
echo "<li><i>" . $listingdescription . "</i></li>";
echo "<li style='float:right;'>By: " . $firstname . " " . $lastname . "</i></li>";
echo "</ul>";
echo "<hr width='80%' noshade='1'>";
}
mysqli_free_result($result);
}
}
while (mysqli_next_result($conn));
}
However, when I get to run it,the page loads, but results won't show. The purpose of it is to be able to display listing details from a listing-details table listed by the complete name from the user table. Two two tables have user-username column as common key.
Like many learners, you are using the wrong tool, simply because you don't know the proper one.
You need not a multiquery (which you never actually need anyway) but a JOIN.
Simply rewrite your two monster queries to a join like this
$searchquery="SELECT l.*, u.`user-username`, u.`user-firstname`, u.`user-lastname`
FROM `listing-details` l, user u
WHERE l.`user-username`=u.`user-username`
AND `listing-address` LIKE ...";
Besides, your quoting is wrong.
Related
I am trying to make a button on a page that prints out data from the database and then you can press 2 different buttons, one that deletes them from the database and the other one inserts it into another table in the database and deletes the data from the database, but it keeps inserting it twice into the new table and I have no clue why, this here prints out the data and session variables + buttons:
if(!isset($_POST['orderby'])) {
foreach ($requests as $row) {
echo "<div class='requests'>" . "<li class='refunds'>" . "Palauttajan nimi: ".
$row['customer_name'] . "</br>" ."Palautettavat tuotteet: ".$row['product_name']."<br> "."Määrä: ".
$row['product_qty'] . " "
. "<br>Kommentti: " . $row['comment'] . "<br> " . "Hinta: " . $row['refund_total'] . "€ " .
"<br>" . "Päivämäärä: " . $row['request_date'] . " " .
"<a class='right' href='admin-page?deleteid=" . $row['request_id'] . "'>Hylkää</a></li>" .
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']
. "'>Hyväksy</a></li>" . "</div>";
$_SESSION['custname'] = $row['customer_name'];
$_SESSION['prodname'] = $row['product_name'];
}
} else {
foreach ($pergele as $row) {
echo "<div class='requests'>" . "<li class='refunds2'>" . "Palauttajan nimi: ".
$row['customer_name'] . "</br>" ."Palautettavat tuotteet: ".$row['product_name']."<br> "."Määrä: ".
$row['product_qty'] . " "
. "<br>Kommentti: " . $row['comment'] . "<br> " . "Hinta: " . $row['refund_total'] . "€ " .
"<br>" . "Päivämäärä: " . $row['request_date'] . " " .
"<a class='right' href='admin-page?deleteid=" . $row['request_id'] . "'>Hylkää</a></li>" .
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']
. "'>Hyväksy</a></li>" . "</div>";
$_SESSION['custname'] = $row['customer_name'];
$_SESSION['prodname'] = $row['product_name'];
}
}
and this should insert it into the database once and delete the data from the old table:
if(isset($_GET['acceptid'])) {
$accept = $_GET['acceptid'];
$custname = $_SESSION['custname'];
$prodname = $_SESSION['prodname'];
/* Query to do whatever here */
$wpdb->insert("wp_acceptedrequests", [
"customer_name" => "$custname",
"name_product" => "$prodname",
"date" => date("Y/m/d/G:i:sa") ,
]);
$wpdb->query("DELETE FROM wp_refundrequests WHERE request_id = $accept");
}
What makes them insert twice and how do I prevent it from doing that?
I just ran into a similar situation where $wpdb inserts where being duplicated.
In my case it was happening if I was authenticated and browser inspector was open.
I have made a database connection in php and FETCH parts like story and id.
the URL forms ok without '&' but fails when '&' is added in the URL.
Here is my php code with '&id=' added in the URL
$fullurl = /cms/page.php . '?chapter=' . $row['story'] . '&id=' . $row['id'];
Can someone put me right about the correct syntax...
The error is pretty obvious
$fullurl = /cms/page.php . '?chapter=' . $row['story'] . '&id=' . $row['id'];
should be changed so that all text is within the citation marks..
$fullurl = '/cms/page.php?chapter=' . $row['story'] . '&id=' . $row['id'];
Otherwise you will end up in an error.
Also please, consider using error_reporting(E_ALL); when debugging.
The part: /cms/page.php is not a string.... and thus is wrong. You could just change it to something like this:
<?php
// NOTICE THAT THE PART "/cms/page.php" IS NOW EMBEDDED IN QUOTES (STRING)
$fullurl = "/cms/page.php" . '?chapter=' . $row['story'] . '&id=' . $row['id'];
// OR EVEN COMPACT IT LIKES SO:
$fullurl = "/cms/page.php?chapter=" . $row['story'] . '&id=' . $row['id'];
poiz
it still didnt work but i found i dont need to fetch from NEWS_ARTICLES. In fact my variables are already fetched as you see below...
Here is my actual code.
$sql = "SELECT old,title,story,shortstory,author,origauthor,ip,timestamp,allowcomments,short,approved,viewcount,rating,archive date,neverarchive,archived,id,
" . NEWS_USERS . ".user AS authorname,
" . NEWS_USERS . ".avatar AS authoravatar,
commentcount AS comments
FROM " . NEWS_ARTICLES . " INNER JOIN " . NEWS_USERS . " ON " . NEWS_ARTICLES . ".author = " . NEWS_USERS . ".uid WHERE id IN (";
if($nocats == "1"){
$sql .= "SELECT id AS storyid FROM " . NEWS_ARTICLES . " WHERE id NOT IN (SELECT storyid FROM " . NEWS_GROUPCATS . " WHERE type = 'news') UNION ";
}
$sql .= "SELECT storyid FROM " . NEWS_GROUPCATS . " WHERE type = 'news' AND catid IN (SELECT catid FROM " . NEWS_GROUPCATS . " WHERE type = 'rss' AND storyid = ?) ) AND archived = '0' ORDER BY timestamp DESC LIMIT 0, $rssamount";
$newsstories = DataAccess::fetch($sql, $feedid);
foreach($newsstories AS $row){
if(FRIENDLY){
$fullurl = $newslocation . $prefix . $row['id'] . "-0-" . makefriendly($row[title]);
}else{
$fullurl = $newslocation . '?epic-code=' . $row['shortstory'] . '&id=' . $row['id'];
}
'&id' gives...
error on line 12 at column 85: EntityRef: expecting ';'
'id' gives...
[a link] http://www.example.com//stock-charts/share-charts.php?epic-code=ALTPid=3360
the & is missing ....
I have two tables that looks like this:
Put it in images since i dont know how to draw a table here.
My problem is that i can't seem to make a query or anything in my php that will allow me to load the report and the 5 images, so that i can display the images where i want to on the page. As i do it now it loads the report five times and one image in each report.
The edited code after #Terminus suggestions
$sql = "
SELECT *
FROM fangstrapporter AS f, rapportbilleder AS r
WHERE f.id=".htmlspecialchars($_GET["id"])." AND r.id=f.id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<b>Overskrift:</b> " . $row["overskrift"] . "<br><br>" .
"<b>Sted:</b> " . $row["sted"] . "<br><br>" .
"<b>Fangstdato:</b> " . $row["fangstdato"] . "<br><br>" .
"<b>Agn:</b> " . $row["agn"] . "<br><br>" .
"<b>Kategori:</b> " . $row["kategori"] . "<br><br>" .
"<b>Art:</b> " . $row["art"] . "<br><br>" .
"<b>Vægt:</b> " . $row["vaegt"] . "<br><br>" .
"<b>Længde:</b> " . $row["laengde"] . "<br><br>";
do {
echo "<a href='" . $row["image_path"] . "'><img src='" . $row["image_thumb_path"] . "'></a><br><br>";
} while($row = $result->fetch_assoc());
echo $row["beskrivelse"]."<br>";
} else {
echo "0 results";
}
Can anyone help me do this? I have been searching on google for four days now, without any success.
Do as #Kenney suggested and remove the part where you echo the report from the loop.
$sql = "
SELECT *
FROM fangstrapporter AS f, rapportbilleder AS r
WHERE f.id=".htmlspecialchars($_GET["id"])." AND r.id=f.id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<b>Overskrift:</b> " . $row["overskrift"] . "<br><br>" .
"<b>Sted:</b> " . $row["sted"] . "<br><br>" .
"<b>Fangstdato:</b> " . $row["fangstdato"] . "<br><br>" .
"<b>Agn:</b> " . $row["agn"] . "<br><br>" .
"<b>Kategori:</b> " . $row["kategori"] . "<br><br>" .
"<b>Art:</b> " . $row["art"] . "<br><br>" .
"<b>Vægt:</b> " . $row["vaegt"] . "<br><br>" .
"<b>Længde:</b> " . $row["laengde"] . "<br><br>";
$beskrivelse = $row["beskrivelse"];
do {
echo "<a href='" . $row["image_path"] . "'><img src='" . $row["image_thumb_path"] . "'></a><br><br>";
} while($row = $result->fetch_assoc());
echo $beskrivelse . "<br>";
} else {
echo "0 results";
}
I am having a problem displaying a JOIN statement. When I add
WHERE id = " . $team_id;
The information that is on the database will not display, but when I remove that line the information will correctly join and display on the "teaminfo.php " page, but it will display all of the data instead of the data that is unique to that id. Also when I remove the JOIN the the data that is unique to the id will display. Can anyone tell me whats wrong here. Any help will be great. Than you.
teaminfo.php
<html>
<head>
<title>Team Info page</title>
</head>
<body>
<?php
include 'connect.php';
$team_id = $_GET['id'];
// SQL query
$query = " SELECT *
FROM pitscouting
JOIN fieldscouting
ON pteam_number = fteam_number
WHERE id = " . $team_id;
if ($result = mysqli_query($mysqli, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
// Write the data of the team
echo "<br />";
echo "Pit scouting";
echo "<dt>Team:</dt><dd>" . $row["pteam_number"] . " " . $row["pteam_name"] . "</dd>";
echo "<dt>Auto:</dt><dd>" . $row["pauto"] . "</dd>";
echo "<dt>Drive:</dt><dd>" . $row["pdrive"] . "</dd>";
echo "<dt>Objetcs With No Problem?</dt><dd>" . $row["pobjNoProblem"] . "</dd>";
echo "<dt>Objects They have a problem with?</dt><dd>" . $row["pobjWithProblem"] . "</dd>";
echo "<dt>Can they shoot? If yes from where and how acc</dt><dd>" . $row["pshoot"] . "</dd>";
echo "<dt>Extra Notes about their robot?</dt><dd>" . $row["pdrive"] . "</dd>";
echo"<br />";
echo "Field Scouting ";
echo "<dt>Team Number:</dt><dd>" . $row["fteam_number"] . "</dd>";
echo "<dt>Auto:</dt><dd>" . $row["fauto"] . "</dd>";
echo "<dt>Drive:</dt><dd>" . $row["fdrive"] . "</dd>";
echo "<dt>Objetcs With No Problem?</dt><dd>" . $row["fobjNoProblem"] . "</dd>";
echo "<dt>Objects They have a problem with?</dt><dd>" . $row["fobjWithProblem"] . "</dd>";
echo "<dt>Shots taken</dt><dd>" . $row["fshots_taken"] . "</dd>";
echo "<dt>Shorts made</dt><dd>" . $row["fshots_made"] . "</dd>";
echo "<dt>Extra Notes</dt><dd>" . $row["fnotes"] . "</dd>";
}
mysqli_free_result($result);
}
// Close the database connection
mysqli_close($mysqli);
?>
<p>Return to the list</p>
</body>
</html>
Palmetto.php
<?php
include 'connect.php';
// SQL query
$query = "SELECT * FROM pitscouting ORDER BY pteam_number";
if($result = mysqli_query($mysqli, $query)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
$name = $row['pteam_number'] . " " . $row['pteam_name'];
// Create a link to teaminfo.php with the id-value in the URL
$strLink = "<a href = 'teaminfo.php?id= " . $row['id'] . "'>" . $name . "</a>";
// List link
echo "<li>" . $strLink . "</li>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $query. " . mysqli_error($mysqli);
}
// Close connection
mysqli_close($mysqli);
?>
If your tables both have an ID field you will have to specify which table you want to get the data from.
WHERE pitscouting.id = " . $team_id;
or
WHERE fieldscouting.id = " . $team_id;
Please do mention the sql injection in you're code
$team_id = $_GET['id'];
// SQL query
$query = " SELECT *
FROM pitscouting
JOIN fieldscouting
ON pteam_number = fteam_number
WHERE id = " . $team_id;
please take a look at prepared statements, to prevent sql injections in youre code
Try putting an alias.
$team_id = $_GET['id'];
// SQL query
$query = " SELECT *
FROM pitscouting p
JOIN fieldscouting f
ON p.pteam_number = f.fteam_number
WHERE p1.id = " . $team_id;
I copied this code from a website for using ajax jquery pagination in php with mysql. I have a mysql table with 5 columns. I want to show all columns of the mysql table - not only two.
I edited the while loop but I cannot insert more columns. Please help me - I am new here and it may be difficult to understand my question. sorry in advance.
$query_pag_data = "SELECT * from ebook LIMIT $start, $per_page";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$msg = "";
while ($row = mysql_fetch_array($result_pag_data)) {
$htmlmsg=htmlentities($row['title']); //HTML entries filter
$msg .= "<li><b>" . $row['id'] . "</b> " . $htmlmsg . "</li>";
}
$msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data
I have linked all code
http://www.9lessons.info/2010/10/pagination-with-jquery-php-ajax-and.html
You only show $row['id'] and $row['title'], just add some extra with the row names ;)
Example:
$msg .= "<li><b>" . $row['id'] . "</b> <i>" . $row['another_row_name'] . "</i>" . $htmlmsg . "</li>";
while ($row = mysql_fetch_array($result_pag_data)) {
$htmlmsg=htmlentities($row['title']); //HTML entries filter
$msg .= "<li><b>" . $row['id'] . "</b> " . $htmlmsg . "</b> " . $row['somefield'] ."</li>";
}
The different values are accessed by the $row array so it depends on what your column names are for the 5 columns.
you might what something like
$msg .= "<li><b>" . $row['colname1'] . "</b> " . $row['colname2'] . " " . $row['colname3'] . <insert more columns here> . "</li>" ;