Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

select mysql myisam table by secondary key

Posted on 2014-02-01
5
Medium Priority
?
379 Views
Last Modified: 2014-02-15
Hi
I have been asked to recreate a 10-year old access system in mySQL and PHP.
The data has been extracted from access and 30 tables created in a mysql database on a Centos linux server. I have written a dozen or so php scripts which are working but have now hit a problem.
I have a table of clients with primary key client_number and a tbl of client_notes with primary key note_id and secondary key client_number.
I have an update program to which I enter a client_id 'sel_id' which has a mySQL SELECT FROM client WHERE client_number='sel_id' followed by a mysql SELECT FROM client_notes WHERE client_number='sel_id'.
Then I create a form, update the data and then REPLACE INTO client and client_notes.
client updates correctly but every time a new record is created in client_notes
I have confirmed that the initial SELECT FROM client_notes fails to read the record I want.

(There is actually a third table involved as well with primary key client_number and that SELECTs correctly and REPLACEs correctly.
Surely I should be able to SELECT by the secondary key.
I've tried googling for an answer but all I can find is arguments about INNODB in place of MySQL
0
Comment
Question by:peter_lawrie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39827121
Can you please create a small test data set from the existing tables and post that along with the output you want to get from the queries?   Once we see the data and the desired output we can almost certainly show you the code to bridge the gap.
0
 

Author Comment

by:peter_lawrie
ID: 39827621
phpHi
Thanks for the response.
Please see attached the code and the three tables concerned - I have extracted the table set up and the first few records. (these three tables are 5Mb with 18000 records in tblClient)
In tblClientNotes.sql the first few records are those created by my code testing on client 18365, it creates a new tblClientNotes record each time, 6572, 6573, 6574 etc because it fails to SELECT existing record successfully.
tfpupdclient.php
tfpupdclient2.php
tblClient.sql
tblClient-Personal-Notes.sql
tblClientNotes.sql
updclient2-output.png
0
 

Author Comment

by:peter_lawrie
ID: 39828133
Some additional detail -
I amended
if (mysqli_num_rows($get_tblClientNotes_res) > 0)  to > 0 rather than ==1
So as there a multiple entries in the table due to the problem I at least managed to read the last record from the table for client_number. - This displays correctly, however the REPLACE still adds another, incrementing note_id rather than replacing it.
I can't see why it does this
       //update tblClientNotes table
                $upd_tblClientNotes_sql = "REPLACE INTO tblClientNotes (client_number, note_id, site_code, notes, entry_date)
                                                   VALUES ('".$_POST["client_number"]."', '".$_POST["note_id"]."',  '".$_POST["site_code"]."',
                                                            '".$_POST["notes"]."', '".$_POST["entry_date"]."'
                                                        )";
                $upd_tblClientNotes_res = mysqli_query($mysqli, $upd_tblClientNotes_sql) or die(mysqli_error($mysqli));

        mysqli_close($mysqli);
0
 

Accepted Solution

by:
peter_lawrie earned 0 total points
ID: 39848240
Silly me, I had not extracted the file primary key in the original read, so of course it would not write the same record back. Once I fixed my mistake the code now works correctly
0
 

Author Closing Comment

by:peter_lawrie
ID: 39861156
found a stupid mistake in the code - sorry for wasting anyone's time.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question