Select statement not returning all values - mysql

This function works fine, what is flipping me out is that the array returned has all the information needed except $row[15] which has data in it on the table Orders
function SelectOrder($orderid)
{
connect();
$result = mysql_query("SELECT * FROM `Orders` WHERE `OrderID` =".$orderid." LIMIT 0 , 30");
$row = mysql_fetch_row($result);
return $row;
}
print_r($row);
Prints
Array ( [0] => 24
[1] => Grei
[2] => Tristram
[3] => 19 2nd Blvd.
[4] => Richmond
[5] => J7V 5R6
[6] => Ontario
[7] => Canada
[8] => grei#email.ca
[9] => (514) 555-5555
[10] => Snow Removal
[11] => 210
[12] => 32.5
[13] => 23.07
[14] => 200.57
[15] =>
[16] => 123 same street
[17] => 1
[18] => 0 )
When I use the same select statement within PHPMyAdmin
SELECT *
FROM `Orders`
WHERE `OrderID` = 24
LIMIT 0 , 30
I get value [15] (SNAME = Frank Ditripani)
PHPMyAdmin SQL Results
OrderID-Fname-Lname-Address-City-Pcode-Prov-Country-Email-Phone-Service-Price-Discount-Tax PYMNTAmount-SNAME-SADD-Agreed-PayPalPaid
24-Grei-Tristram-19 2nd Blvd.-Richmond-J7V 5R6-Ontario-Canada-grei#email.ca-(514) 555-5555-Snow Removal-210-32.5-23.07-200.57-Frank Ditripani-123 same street-1-0
Both SNAME and SADD are the exact same properties in the table which is varchar(50) and SADD is returned but not SNAME.
This is the first time I have ever posted a question I usually find my answers here but this one is driving me nuts! and I am a bit embarrassed as the answer is probabley an easy one.

The query might be the same, but is the database the same?
Check that you are connecting to the same database!
My bet is the connection parameters are different for the two programs.

Check the connection character set - DB, client and connection should use the same one.

Related

MySQL JOIN whether null or not

I'm pretty out of practice with MySQL and PHP, but I have a project I'm working on for a friend that involves selecting data from two tables, and combining them into one result - seems simple.
Table 1 has 13 fields, but the important ones are id (auto-increment, primary key) and serial (unique). The rest are just ones like customer, description, etc. etc.
Pictures has 3 fields, picID (auto-increment, primary key), imagePath and serial
I need to retrieve all data from Table 1, and if there is a matching photo (identified by the same serial - only ever 1 photo possible per serial) in Pictures, then retrieve that data too. I then output the data from Table1, and use imagePath from Pictures to build an image in HTML if one has been uploaded.
The query I've been using is:
$sql = "SELECT * FROM Table1 LEFT JOIN Pictures ON Table1.serial = Pictures.serial ORDER BY Table1.serial";
Which seems perfect, EXCEPT if any row from Table1 does not have a photo match in Pictures, the serial is no longer returned with the rest of the data, although the remainder of the row is all correct.
I have looked into the different types of JOIN, and whether it's just UNION that I need, but I am a bit stumped. How should I query to get each row of Table1 plus Pictures.imagePath added on to the matching Table1 row, if it exists?
Thank you for your time!!!! :)
EDIT with dumped array output
Array
(
[0] => Array
(
[id] => 51
[0] => 51
[client] => Test Client
[1] => Test Client
[location] => Ayreford House
[2] => Ayreford House
[description] => Ceiling cavity of building XYZ
[3] => Ceiling cavity of building XYZ
[serial] =>
[4] => 18001
[blah] => 123456
[5] => 123456
[fw] => Wall
[6] => Wall
[pm] => Plasterboard
[7] => Plasterboard
[stuff] => ventilation ducting
[8] => ventilation ducting
[ref] => S1000-2018
[9] => S1000-2018
[otheref] => XTX-1325
[10] => XTX-1325
[notes] => Updated photo
[11] => Updated photo
[date] => 2018-06-28 21:37:49
[12] => 2018-06-28 21:37:49
[picID] =>
[13] =>
[imagePath] =>
[14] =>
[15] =>
)
It's doing that because both Table1 and Pictures have a column called serial and it drops the table names when it is generating the array keys. Probably its doing something like this internally:
$result = array()
$result[0] = Table1.serial;
$result['serial'] = Table1.serial;
$result[1] = Table1.client;
$result['client'] = Table1.client;
....
$result[14] = Pictures.serial;
$result['serial'] = Pictures.serial;
So you end up with only Picture.serial as the value for the key 'serial' in the resulting array.
One way to fix this would be to specify your columns explicitly and don't include Pictures.serial, like this:
SELECT
Table1.id,
Table1.client,
Table1.location,
Table1.description,
Table1.serial,
Pictures.notes
FROM
Table1
LEFT JOIN
Pictures ON Table1.serial = Pictures.serial
ORDER BY
Table1.serial

Joined 3 tables, not outputting all results from third table?

I have three tables:
Student - UPN, Name, Year, House
Seclusion_Status - ID, Arrived, FTE, Rebuild, DateTimeAdded, Staff, Student_UPN (fk), Comment
Period_Rating - ID, Slot_ID, Rating, Date, Seclusion_ID (fk)
Each student can have many entries in the Seclusion_Status table, and then there are also many entries in the Period_rating table, which is linked to the Seclusion_status table with the Seclusion_ID
I am running the following query to return a record from Seclusion_Status based on a date, and then all the records in the Period_rating table that relate to the Seclusion_status record.
$sql="SELECT * FROM Seclusion_Status
INNER JOIN Students ON Seclusion_Status.Student_UPN=Students.UPN
JOIN Period_Rating ON Seclusion_Status.ID=period_rating.Seclusion_ID
WHERE period_rating.Date = '$start'
GROUP BY period_rating.Seclusion_ID
ORDER BY Seclusion_Status.DateTimeAdded ASC";
$result=mysql_query($sql);
// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
The query is returning the Seclusion_Status record, and then the first record in Period_rating, but not the others.
Array
[0] => 348
[ID] => 157
[1] => Y
[Arrived] => Y
[2] => N
[FTE] => N
[3] =>
[Rebuild] =>
[4] =>
[Text] =>
[5] => 2016-03-04 09:30:50
[DateTimeAdded] => 2016-03-04 09:30:50
[6] => Mr S Holland
[Staff] => Mr S Holland
[7] => K80222800
[Student_UPN] => K8022280
[8] => Refusing instructions
[Incident] => Refusing instructions
[9] =>
[Period] =>
[10] =>
[Period_In_ID] =>
[11] => Not sitting properly in class despite being asked
[Comment] => Not sitting properly in class despite being asked
[12] => K80222800
[UPN] => K80222800
[13] => Student Name
[Name] => Student Name
[14] => Year 9
[Year] => Year 9
[15] => Acer
[House] => Acer
[16] => 157
[17] => P2
[Slot_ID] => P2
[18] =>
[Rating] =>
[19] => 2016-03-04
[Date] => 2016-03-04
[20] => 348
[Seclusion_ID] => 348
[21] => 1
[Status] => 1
The query is returning the Seclusion_Status record, and then the first record in Period_rating, but not the others.
You have a GROUP BY period_rating.Seclusion_ID that instructs mysql to return one record per Seclusion_ID. Take the group by clause out, and the query will return multiple records, that match.
The thing is if you use join, if data is there in all the three tables then only it will fetch.
If you want to fetch all the entries in the table you need to use outer join, unfortunately u cant use outer join in mysql.
but you can fetch results by unioning leftjoin results and right join results

Zend DB join selects all columns

I am attempting to perform a query using Zend_Db_Select. Here is my code:
$db = $this->db;
$select = $this->db->select(false)
->from('invoice',$data1)
->join('partner_settings', $db->quoteInto('partner_settings.clientid = ?', $clientid), array()) //'toggle_value'))
->join('partner_info', $db->quoteInto('partner_info.rowid = partner_settings.partnerinfoid', array())) //'type', 'shipper_name' => 'partner_info.name')))
->join('partner_shipping', $db->quoteInto('partner_shipping.partnersettingsid = partner_settings.rowid', array())) //'default_method_id')))
->join('partner_ship_methods', $db->quoteInto('partner_ship_methods.rowid = partner_shipping.default_method_id'), array()) //'shipping_method' => 'name'))
->where('storeid IN (?)',$inputid)
->where('partner_info.type = ?', 'shipping')
->where('partner_settings.toggle_value = ?', 'on')
->order(array('datetime_cre DESC'));
$data1 is an array containing these values:
Array
(
[0] => invoice_date AS inv_invoice_date
[1] => invoice_id AS inv_invoice_id
[2] => name AS inv_name
[3] => ups_track AS inv_ups_track
[4] => shipping_pdf AS inv_shipping_pdf
[5] => invoice_pdf AS inv_invoice_pdf
[6] => alert AS inv_alert
[7] => invoice_date AS inv_invoice_date
[8] => invoice_id AS inv_invoice_id
[9] => name AS inv_name
[10] => subtotal AS inv_subtotal
[11] => tax_inclusive AS inv_tax_inclusive
[12] => total AS inv_total
[13] => shipping_pdf AS inv_shipping_pdf
[14] => invoice_pdf AS inv_invoice_pdf
[15] => alert AS inv_alert
[16] => rowid
[17] => partner_info.name AS shipper_name
[18] => partner_ship_methods.name AS shipping_method
)
The resulting MYSQL query looks like this:
SELECT `invoice`.`invoice_date` AS `inv_invoice_date`, `invoice`.`invoice_id` AS `inv_invoice_id`, `invoice`.`name` AS `inv_name`, `invoice`.`ups_track` AS `inv_ups_track`, `invoice`.`shipping_pdf` AS `inv_shipping_pdf`, `invoice`.`invoice_pdf` AS `inv_invoice_pdf`, `invoice`.`alert` AS `inv_alert`, `invoice`.`invoice_date` AS `inv_invoice_date`, `invoice`.`invoice_id` AS `inv_invoice_id`, `invoice`.`name` AS `inv_name`, `invoice`.`subtotal` AS `inv_subtotal`, `invoice`.`tax_inclusive` AS `inv_tax_inclusive`, `invoice`.`total` AS `inv_total`, `invoice`.`shipping_pdf` AS `inv_shipping_pdf`, `invoice`.`invoice_pdf` AS `inv_invoice_pdf`, `invoice`.`alert` AS `inv_alert`, `invoice`.`rowid`, `partner_info`.`name` AS `shipper_name`, `partner_ship_methods`.`name` AS `shipping_method`, `partner_info`.*, `partner_shipping`.* FROM `invoice`
INNER JOIN `partner_settings` ON partner_settings.clientid = '33'
INNER JOIN `partner_info` ON partner_info.rowid = partner_settings.partnerinfoid
INNER JOIN `partner_shipping` ON partner_shipping.partnersettingsid = partner_settings.rowid
INNER JOIN `partner_ship_methods` ON partner_ship_methods.rowid = partner_shipping.default_method_id WHERE (storeid IN ('43')) AND (partner_info.type = 'shipping') AND (partner_settings.toggle_value = 'on') ORDER BY `datetime_cre` DESC
My biggest problem is with the SELECT columns clause which includes: partner_info.*, partner_shipping.* I don't want to include all columns from these tables. I have set the join() columns argument to empty array(), but it doesn't help.
Has anyone found a solution to this problem? I have been searching futilely.
You haven't set the columns argument to an empty array, you're passing the array as the second argument to quoteInto(). You have:
->join('partner_info', $db->quoteInto('partner_info.rowid = partner_settings.partnerinfoid', array()))
The quoteInto() serves no purpose since you aren't passing in any variables, so what you probably want is:
->join('partner_info', 'partner_info.rowid = partner_settings.partnerinfoid', array())
so the first parameter to join() is the table name, the second is the condition, the third is the columns.

Making a conditional MySQL join involving a small table and a large table efficient

The MySQL query I'm currently trying to perform is functionally equivalent to this:
SELECT small_table.A, small_table.B, small_table.C, huge_table.X, huge_table.Y
FROM small_table LEFT JOIN huge_table
ON small_table.D = huge_table.Z
WHERE small_table.E = 'blah'
except that the query doesn't appear to terminate (at least not within a reasonable amount of time), probably because the second table is huge (i.e. 7500 rows with a total size of 3 MB). Can I perform a functionally equivalent join in a reasonable amount of time, or do I need to introduce redundancy by adding columns from the huge table into the small table. (I'm a total beginner to SQL.)
The clause WHERE small_table.E = 'blah' is static and 'blah' never changes.
Here is the EXPLAIN output as requested:
Array ( [0] => Array ( [0] => 1 [id] => 1 [1] => SIMPLE [select_type] => SIMPLE [2] => small_table [table] => small_table [3] => ref [type] => ref [4] => E [possible_keys] => E [5] => E [key] => E [6] => 1 [key_len] => 1 [7] => const [ref] => const [8] => 1064 [rows] => 1064 [9] => Using where [Extra] => Using where ) [1] => Array ( [0] => 1 [id] => 1 [1] => SIMPLE [select_type] => SIMPLE [2] => huge_table [table] => huge_table [3] => eq_ref [type] => eq_ref [4] => PRIMARY [possible_keys] => PRIMARY [5] => PRIMARY [key] => PRIMARY [6] => 4 [key_len] => 4 [7] => my_database.small_table.D [ref] => my_database.small_table.D [8] => 1 [rows] => 1 [9] => [Extra] => ) )
A few things ...
1) Are you executing this query directly in MySQL (either Workbench GUI or command line), or is this query embedded in PHP code? Your EXPLAIN output seems to suggest PHP. If you haven't done so already, try executing the query directly in MySQL and take PHP out of the mix.
2) Your EXPLAIN output looks Ok, except I'm wondering about your WHERE clause with small_table.E = 'blah'. The EXPLAIN output shows that there's an index on column E but the key length = 1, which is not consistent to the comparison with 'blah'. What data type did you use for the column definition for small_table.E?
3) MySQL is estimating that it needs to scan 1064 rows in small_table. How many total rows are in small_table, and how many do you expect should match this particular query?

Google Maps API - geocoding accuracy chart?

Where in the Google Maps API docs can I find a table explaining the accuracy values of Geocode lookups?
Has the range of values changed in between V2 and V3?
Here are the Google Maps API Docs. It contains a table with accuracy values...
http://code.google.com/apis/maps/documentation/reference.html#GGeoAddressAccuracy
Constant | Description
0 Unknown location.
1 Country level accuracy.
2 Region (state, province, prefecture, etc.) level accuracy.
3 Sub-region (county, municipality, etc.) level accuracy.
4 Town (city, village) level accuracy.
5 Post code (zip code) level accuracy.
6 Street level accuracy.
7 Intersection level accuracy.
8 Address level accuracy.
9 Premise (building name, property name, shopping center, etc.) level accuracy.
Here are the real status answers from geocoder:
You can output the status inside your geocoding function:
myMap.geocoder.geocode(
{ address: someAdress } ),
function ( responses, status ) {
console.log( status );
}
);
When passing the status, you can switch those four values:
switch ( status )
{
case 'ROOFTOP' :
var precision = 'precise';
break;
case 'RANGE_INTERPOLATED' :
var precision = 'interpolated';
break;
case 'APPROXIMATE' :
var precision = 'approximately';
break;
case 'ZERO_RESULTS' :
var precision = 'no address';
break;
}
#Pekka
I don't know if you saw, but V3 does not include accuracy anymore. It seems that there is a different way though. If you add the results of the address_component elements you get a similar result. Disclaimer: I am not 100% sure about this but it looks like that they included the accuracy this way. I am currently doing some testing is this the way to go.
Here an example:
I searched for: 555 Pearl Street, Boulder
Here the result with Address Level accuracy (8 levels deep).
[address_component] => Array
(
[0] => SimpleXMLElement Object
(
[long_name] => 555
[short_name] => 555
[type] => street_number
)
[1] => SimpleXMLElement Object
(
[long_name] => Pearl St
[short_name] => Pearl St
[type] => route
)
[2] => SimpleXMLElement Object
(
[long_name] => Boulder
[short_name] => Boulder
[type] => Array
(
[0] => locality
[1] => political
)
)
[3] => SimpleXMLElement Object
(
[long_name] => Boulder
[short_name] => Boulder
[type] => Array
(
[0] => administrative_area_level_3
[1] => political
)
)
[4] => SimpleXMLElement Object
(
[long_name] => Boulder
[short_name] => Boulder
[type] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[5] => SimpleXMLElement Object
(
[long_name] => Colorado
[short_name] => CO
[type] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[6] => SimpleXMLElement Object
(
[long_name] => United States
[short_name] => US
[type] => Array
(
[0] => country
[1] => political
)
)
[7] => SimpleXMLElement Object
(
[long_name] => 80302
[short_name] => 80302
[type] => postal_code
)
)