Let me start by saying i am new to mysql and php, and I'm sure this is a noob question but I've been searching google and can't find any solution.
Basically I want to create 1 template file that will read all of a table from a database with multiple rows and columns but only display one row at a time.
An example would be http://www.w3schools.com/php/php_mysql_select.asp scroll towards the bottom where it shows peter griffen and glen quagmire i would want it to only display 'glen quagmire' or only display 'peter griffen' depending on which link was previously clicked.
would i need to somehow assign an ID to the link url so php knew which row to parse etc.. ?
One straightforward way is to use the query string. Make the link <a href='stuff.php?id=1'>View #1</a>. Then use a where id='$id' clause in the SQL query, setting $id equal to the query string parameter $_GET['id'].
Related
can someone please explain to me how to approach this issue?
I have a table with 4000 records, a select option and a search field to filter out the table
however I'm trying to figure out the best way to set up pagination.
1st Approach:
Should I make one query against the database and populate the table with all records and then set up the pagination with javascript so I can be able to search for records that are not only shown on the current page?
2nd Approach
Set up the pagination with PHP, make a query via ajax for each page? however I'm afraid that this approach would not allow me to filter out the table if I need to search for a record that is not on the current page.
1st approach: definitely not, loading large number of results for nothing is not recommended.
2nd approach makes more sense. You can use the LIMIT statement inside your SELECT statement to decide which records you want. Ex. if you paginate on 10 elements perpage, you could do
SELECT * FROM Table LIMIT 10
then
SELECT * FROM Table LIMIT 10,10
and so on. Indexes start at 0.
Note that you don't even need Ajax to do that, your next button can specify the offset for the next load of the page. It's a choice based on your knowledge, the size of the rest of the page (minimize load time), ...
I'm looking for a bit of advice on which method is more secure and efficent to use.
OPTION A: I have a database named calendar, and 12 tables in it - "January", "February" etc. Each month has it's own .php page with "select * from jan" etc. I have a dropdown menu, which when the user selects for example "January" I have an ajax script that then loads jan.php into the div "currentmonth"
What I was thinking of doing, because along with the "currentmonth" div, I've also got a "recentlyadded" div showing the last 3 entries sorted by their timestamp. I'm not sure how I could show the 3 most recently added entries in the whole database, so I tried option B.
OPTION B: I have one table called calendar, and each row has a column called month. This made it more simple for display the recently added, but I'm not sure how to go about implementing the dropdown menu. From what I've read the idea I have can leave me open to sql injection.
Here's the idea: I have a jquery variable called "selectedmonth" which is equal to the value of the selected month. I then want to take that variable... for example, user selects "January" the value is "jan", and I want the sql statement to then update with SELECT * FROM calendar WHERE months = "jan", but as I said, I hear this can leave me wide open to SQL injection.
Is there anyway to dynamically update the SQL statement, maybe with AJAX or JSON? Is there a way to do OPTION B without leaving myself open to SQL injection? Or is there an easier way for me to use OPTION A, and be able to find the 3 most recent added rows in the entire database?
The one thing I'm wondering though is with option A, I would obviously have 12 "month".php files, will that add a lot to page load times?
Any advice would be greatly appreciated.
Use prepared statements and you should be fine. The second option is preferable, the first is not a good idea...
All you have to do is something like:
$row = $conn->prepare("SELECT * FROM calendar WHERE months = ?");
$row->bind_param('s', $_GET['month']);
$row->execute();
$row = $row->get_result();
And you'll be SQL injection-free. Read more here.
HI I am working on a php mysql project and need some help.
On one of my fields I use a check box to enter a value. The possible options are 9001 14001 and 18001.
If I tick 14001 and 18001 the result that gets stored is 14001,18001.
When I set up a search I have had to set up an if equals for each possible combination. ..not too bad in this case as only 7.
Bit what would I use in an sql query if I wanted to say if (field) contains?
The following query would give you all line where field contains 14001.
SELECT * FROM table WHERE field LIKE '%14001%'
But you might want to reconsider the way you store your data to make it faster.
One way to do it is nicely described here:
http://www.phpknowhow.com/mysql/many-to-many-relationships/
I feel like there is probably a very simple answer for this, but I've spent about 20 minutes searching and can't find anything.
Basically, I am using PHP to query a table and output the results as a list, using the primary key column (COL_1) of the table to create a link for each record that will bring the user to a detail page for that record. It works fine when the data in COL_1 is a straight-forward string such as "TEST". The edit link will then be detail.php?COL_1=TEST The detail page works by querying the database using the data passed by the link. So in this case it would do a select on the table where COL_1 = 'TEST' and return the correct record.
However, when new line characters are stored in COL_1 things get a bit complicated. For instance, if 'TEST\r\nTEST' is stored in COL_1, when the original query of the entire table is done, $row['COL_1'] for that line will give me 'TESTTEST', which gets passed to the detail page as detail.php?COL_1=TESTTEST, the detail page does a select on the table where COL_1 = 'TESTTEST', and it returns nothing.
However, if I manually link to detail.php?COL_1=TEST\r\nTEST the detail page will query on 'TEST\r\nTEST' and return the correct record.
So basically what I need is a way to do a query and have $row['COL_1'] return 'TEST\r\nTEST' instead of 'TESTTEST'. Does this make sense? How would I go about doing this?
As for why the table is set up like this, don't ask me. I didn't design it. I'd never design keys that can include line breaks like this. But I do have to interact with this table. Bah.
You should encode values that are passed in the URL:
echo urlencode("TEST\r\nTEST");
However, why would TEST\r\nTEST be a primary key? That's crazy. Maybe you need to rethink how you are doing things. Primary keys as integers work nicely.
I'm trying to make a PHP search form that lets you search for something, then compares it to the record with the Name field that most closely matches it. Then, it would open the URL specified in the src field of the same record in the database. I've been looking around many websites but I can't find a tutorial for this, does anyone know a good one or know how to do this? Thanks!
Use the MySql like clause
"SELECT * FROM table where field like '%{$searchTerm}%'"
Now fetch the src field from the selected record.