I have a table with 15-20 columns that is getting a little unwieldy because the columns no longer match what I feel is a "logical" order.
If I change the order of the columns - either by creating a new table or by using ALTER - will this break any of my existing queries or code?
That is, does either SQL or PHP care that the column order in my code/queries does not match the physical order of my tables?! :-/
how do you view your table other than with a SELECT clause? Why would it matter the physical order when you can determine what shows up with a SELECT clause?
I think you worry FAR TOO MUCH about minute things in your database(s), table(s), column name(s) etc. Focus on your work and don't sweat the small stuff.
unless you use (deprecated) ordinal position numbers in your ORDER BY clause, it will have no effect on sql
if you use arrays in handling your query results in php, with offset positions instead of column names in those arrays, you could mess up big time
What about in a Prepared Statement like this...
// Find Articles. *
// Build query.
$q1 = 'SELECT section, heading, image, published_on, summary
// Prepare statement.
$stmt1 = mysqli_prepare($dbc, $q1);
// Bind variable to query.
mysqli_stmt_bind_param($stmt1, 's', $_GET['section']);
// Execute query.
// Store results.
// Check # of Records Returned.
// Articles Found.
// Bind result-set to variables.
mysqli_stmt_bind_result($stmt1, $section, $heading, $image, $publishedOn, $summary);
// Fetch below in loop...
i don't do php so i can't say with certainty, but it looks okay, you're referencing the result using the same column names and not positions
P.S. Is it bad for me wanting to "pretty up" the order of my table columns? :-/
Bad? Not if you're doing it in your own time. If you were working for a boss, he might not be too pleased.
Lot of extra, useless work? IMO yes
Changing the order of the columns in your table will not break this code because you are defining the order of the columns you want in the SELECT statement, which still match your bind_result statement. If you had used SELECT * instead of listing your column names, OR if you change the order of your column names in your SELECT statement, yes your code will break.
Yep, it's my system.
If you had used SELECT * instead of listing your column names, OR if you change the order of your column names in your SELECT statement, yes your code will break.
This topic is now archived. It is frozen and cannot be changed in any way.