Basically this script exports all of the orders in our little system to a CSV file. There are 4 separate tables that are joined to complete this. SEorder, SEtracking, SE_user, users_orders.
Here is the error:
<div id="Error">An error occurred in script '/home/sites/mywebsite.com/web/administration/allorders.php' on line 81:
<br />Undefined index: UserLast
Here is my database query:
//Make the query-select all orders
$query2 ="SELECT DISTINCT o.OrderID, Basket, Title, FirstName, LastName, Suffix, Company, Address1, Address2, City, State, Zip, GiftCard, GiftCardMsg, t.Tracking, u.UserFirst, u.UserLast AS doo ".
"FROM SEorder o ".
"LEFT JOIN SEtracking t ON (o.OrderID = t.OrderID) ".
"INNER JOIN users_orders uo ON (uo.OrderID = o.OrderID) ".
"INNER JOIN SEuser u ON (u.UserID = uo.UserID) ".
"AND Submitted='Y' ".
"ORDER BY OrderDate ASC";
and here is my php to grab the data. this is line 81 that is throwing the error:
$username = $row['UserFirst'] . " " . $row['UserLast'];
I am positive that the table SEuser exists, has the column UserLast, and that column has data in it. I am not completely versed on JOIN's though so am I missing something here? I did not create this script, just troubleshooting. Thanks!
It looks like you aliased the UserLast column as doo.
because you have queried the UserLast as doo
so use the doo index not UserLast
u.UserLast AS doo
Your PHP should say $row['doo']
Related
I have the following problem.
I have a MySQL-query with UNION ALL:
$zoek = trim($_POST['zoekkey']);
$zoekstring = "%" . $zoek . "%";
$stmtsk = $user_home->runQuery(
"(SELECT city_zip, city_name, tp_status, tp_city as city, themepark_id as id, tp_logo as logo, tp_name as name, tp_address as address, 'Uitstap' as categorie FROM themepark "
. "INNER JOIN city ON themepark.tp_city=city.city_id "
. "WHERE (tp_name LIKE :search OR tp_address LIKE :search) AND tp_status = 1) "
. "UNION ALL "
. "(SELECT city_zip, city_name, hot_status, hot_city as city, hotel_id as id, hot_logo as logo, hot_name as name, hot_address as address, 'Overnachten' as categorie FROM hotel "
. "INNER JOIN city ON hotel.hot_city=city.city_id "
. "WHERE (hot_address LIKE :search OR hot_name LIKE :search) AND hot_status = 1) "
. "UNION ALL "
. "(SELECT city_zip, city_name, rest_status, rest_city as city, restaurant_id as id, rest_logo as logo, rest_name_".$_SESSION['lang']." as name, rest_address as address, 'Restaurant' as categorie FROM restaurant "
. "INNER JOIN city ON restaurant.rest_city=city.city_id "
. "WHERE (rest_address LIKE :search OR rest_name_".$_SESSION['lang']." LIKE :search) AND rest_status = 1) "
. "UNION ALL "
. "(SELECT city_zip, city_name, sbs_status, sbs_city as city, sbs_id as id, sbs_logo as logo, sbs_name as name, sbs_address as address, 'Detailhandel' as categorie FROM sbs " //detailhandel
. "INNER JOIN city ON sbs.sbs_city=city.city_id "
. "WHERE (sbs_address LIKE :search OR sbs_name LIKE :search) AND sbs_status = 1 AND sbs_categorie = 'detail') "
. "UNION ALL "
. "(SELECT city_zip, city_name, sbs_status, sbs_city as city, sbs_id as id, sbs_logo as logo, sbs_name as name, sbs_address as address, 'Horeca' as categorie FROM sbs " //horeca
. "INNER JOIN city ON sbs.sbs_city=city.city_id "
. "WHERE (sbs_address LIKE :search OR sbs_name LIKE :search) AND sbs_status = 1 AND sbs_categorie = 'horeca')
`ORDER BY categorie,name`"
);
$stmtsk->bindParam(":search",$zoekstring);
$stmtsk->execute();
$rowsk = $stmtsk->fetch(PDO::FETCH_ASSOC);
The strange thing is, the first row off the result is not coming up.
All the others are OK.
When I run the same query in PHPMyAdmin it is working fine...
I really don't understand why it isn't showing it.
Thanks in advance!
Very unlikely it's a problem with the query. But we can't entirely rule out an issue with the LIKE comparisons and a characterset difference. And we can't rule out issues with the values being supplied for the bind parameters,
We're just guessing. But the most likely explanation for the observed behavior is that the code is fetching the first row, and then doing nothing with it, and then fetching the next row. We could reproduce the reported behavior with a code pattern like this:
$sth->execute();
$row = $sth->fetch();
// do nothing with the row we just fetched
while( $row = $sth->fetch() ) {
// fetch another row and output it
}
This pattern would explain why we don't "get" the first row. (We actually get the row, we just ignore it, and fetch the next row.
Other thoughts:
Without an ORDER BY clause, the database can return rows in any order. So what is the "first" row with one execution could be the third row, or last row, on subsequent executions.
If the issue was with particular rows (values) not being returned, other than just the reported "missing first row", that would likely be due to conditions in the WHERE clause, how the query is evaluated. But that issue could affect more than just the one "first" row.
I am debuging php website and don't know how mysql query in it works. The code is:
$query = "
SELECT
WR.*,
W.*,
D.*
FROM
{#table} WR
LEFT JOIN words W
ON WR.word_id = W.word_id
LEFT JOIN words_descriptions WD
ON W.word_id = WD.word_id
LEFT JOIN descriptions D
ON WD.description_id = D.description_id
" . $user_words_sql . "
WHERE
title_len > 3
AND W.in_game = 1
" . $frequency_sql . "
" . $type_sql . "
GROUP BY
WR.word_id,
WR.description_id
ORDER BY
$additional_order W.frequency DESC
";
My question is where table WR is created, is it temporary table created in this peace of code or is it created somewhere else? Also, what this expression {#table} does?
The value "WR" is an alias for the query, not a temporary table. It doesn't copy the data in memory, it's just a shortcut to make the query shorter/easier to read. Here's an example:
SELECT * FROM countries c WHERE c.code = "GB"
Expands to be:
SELECT * FROM countries WHERE countries.code = "GB"
#machineaddict was correct, the {#table} has no significance in MySQL - it's likely that this is being replaced at a later stage by another part of your PHP application.
I have four tables:
tournaments (id, name, slots, price, gameId, platformId)
games (id, name)
platforms (id, name)
participations (id, tournamentId, playerId)
I want to get tournament's game name, platform name, slots, price, reservedSlots (participations count), information whether some player (his id is provided by php) participate in this tournament (bool/true) and conditions are:
- gameId must be in specified array provided by php
- platformId must be in specified array provided by php
I have created something like this but it doesn't work correctly:
php:
$platformsList = "'". implode("', '", $platforms) ."'"; //
$gamesList = "'". implode("', '", $games). "'";
mysql:
SELECT
t. NAME AS tournamentName,
t.id,
t.slots,
p. NAME,
g. NAME AS gameName,
t.price
FROM
tournaments AS t,
platforms AS p,
games AS g,
participations AS part
WHERE
t.PlatformId = p.Id
AND t.GameId = g.Id
AND t.Slots - (
SELECT
count(*)
FROM
participations
WHERE
TournamentId = t.id
) > 0
AND part.TournamentId = t.Id
AND t.PlatformId IN ($platformsList)
AND t.GameId IN ($gamesList)
I will not dwelve into handling your post and get values, I will assume that everything is all right:
$possibleGameIDs = getPossibleGameIDs(); //function will return the array you need for possible game id values. Inside your function make sure that the id values are really numeric
$possiblePlatformIDs = getPossiblePlatformIDs(); //function will return the array you need for possible platform id values. Inside your function make sure that the id values are really numeric
$playerId = getPlayerId(); //function returns the player id and makes sure that it is really a number
$sql = "select games.name, platforms.name, tournaments.slots, tournaments.price, ".
"(select count(*) from participations where participations.tournamentId = tournaments.tournamentId) as reservedSlots, ".
"(select count(*) > 0 from participations where participations.tournamentId = tournaments.tournamentId and playerId = ".$playerId.") as isParticipating ".
"from tournamens ".
"join games ".
"on tournaments.gameId = games.id ".
"join platforms ".
"on tournaments.platformId = platforms.id ".
"where games.id in (".implode(",", $possibleGameIDs).") and ".
" platforms.id in (".implode(",", $possiblePlatformIDs).") and ".
" tournaments.slots > 0"
Code was not tested, so please, let me know if you experience any problems using it. Naturally you need to run it, but as you did not specify what do you use to run the query, I did not allocate time to deal with technical details of running it. Beware SQL injections though.
Here is what I am working with:
$query = "SELECT Products.Title, Product_Lines.pl_Title, Manufacturers.man_Title".
"FROM Products, Product_Lines, Manufacturers ".
"WHERE Products.pl_ID = Product_Lines.pl_ID AND Product_Lines.man_ID = Manufacturers.man_ID";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['Title']. " - ". $row['pl_Title']. " - ". $row['man_Title'];
echo "<br />";
}
I am getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'WHERE Products.pl_ID = Product_Lines.pl_ID AND
Product_Lines.man_ID = Manufactur' at line 1
I am unfamiliar with this method and this error
SELECT Products.Title, Product_Lines.pl_Title, Manufacturers.man_Title
FROM Products INNER JOIN Product_Lines ON Products.pl_ID = Product_Lines.pl_ID INNER JOIN Manufacturers ON Product_Lines.man_ID = Manufacturers.man_ID
WILL DO
I don't see white space before your FROM clause. This is a possible cause for the error. Try:
$query = "SELECT Products.Title, Product_Lines.pl_Title, Manufacturers.man_Title".
" FROM Products, Product_Lines, Manufacturers ".
"WHERE Products.pl_ID = Product_Lines.pl_ID AND Product_Lines.man_ID = Manufacturers.man_ID";
Need To follow two things
Foriegn Key
Join
Search Join i.e [left join right join...] In Inter u ll get anwser for ur question/..
I have 2 tables; members and teams
members table
memberID, firstName, lastName
(firstName and lastName are fulltext indexes)
teams table
team_id, member1ID, member2ID
Here's my query
$sql = "SELECT a.* ";
$sql .= "FROM teams a WHERE ";
$sql .= "a.member1ID IN (SELECT b.memberID FROM members b ";
$sql .= "WHERE MATCH(b.firstName, b.lastName) AGAINST('$q' IN BOOLEAN MODE)) ";
$sql .= "OR a.member2ID IN (SELECT b.memberID FROM members b ";
$sql .= "WHERE MATCH(b.firstName, b.lastName) AGAINST('$q' IN BOOLEAN MODE)) ";
if($year)
$sql .= "AND a.team_y = $year ";
$sql .= "ORDER BY a.team_num ASC ";
if($limit)
$sql .= "$limit";
This query has to be close, but its not working yet.
Im trying to build a query that will let me show me all of the teams "$q" is on.
Ex. $q=doe , Show me all teams that doe is on.
This query has to output the teams.
One possible reason your query doesn't work is there is a minimum length on full-text searching, which defaults to 4 characters. "doe" would fail this match. You can increase this via variable "ft_min_word_len"
http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_ft_min_word_len
By the way, if you want to avoid Normalizing (which isn't always the "best" way to go), you could at least use JOIN's instead of sub-selects.. e.g. (field names renamed to save on typing)
select t.* from teams t
inner join members me1 on t.m1 = me1.id
inner join members me2 on t.m2 = me2.id
where MATCH(me1.fname, me1.lname, me2.fname, me2.lname)
AGAINST('smith' IN BOOLEAN MODE);
Normalize your database.
In your case, this would mean having a table Team (team_id, name, whatever else), a table Member (member_id, first_name, last_name), and a table MemberToTeam (member_id, team_id). In other words, move the member1ID and member2ID into their own table.
Following this practice, apart from "improving" your database schema in the general sense, means that the query that bothers you will become trivial to write.
If you know the member_id:
SELECT team_id FROM MemberToTeam WHERE member_id = 1
If you search by first or last name:
SELECT mtt.team_id FROM Member m
LEFT JOIN MemberToTeam mtt ON m.member_id = mtt.member_id
WHERE m.first_name LIKE '%your search string here%' OR m.lastname LIKE '%your search string here%'