Есть так много ответов для PHP и MySQL, но вот код для PHP и Oracle для предотвращения SQL-инъекций, а также регулярное использование драйверов oci8:
$conn = oci_connect($username, $password, $connection_string);
$stmt = oci_parse($conn, 'UPDATE table SET field = :xx WHERE ID = 123');
oci_bind_by_name($stmt, ':xx', $fieldval);
oci_execute($stmt);
Можно установить псевдонимы для столбцов, которые Вы выбираете:
$query = 'SELECT news.id AS newsId, user.id AS userId, [OTHER FIELDS HERE] FROM news JOIN users ON news.user = user.id'
Можно или использовать числовые индексы ($row[0]
) или лучше, использовать AS
в MySQL:
SELECT *, user.id AS user_id FROM ...
Можно сделать что-то как
SELECT news.id as news_id, user.id as user_id ....
, И затем $row['news_id']
будет идентификатор новостей, и $row['user_id']
будет идентификатор пользователя
Если Вы не испытываете желание искажать Вас, может также просто снабдить префиксом имена таблиц.
Этот путь можно лучше автоматизировать поколение запросов. Кроме того, это - лучшая практика для не использования выбора * (это, очевидно, медленнее, чем просто выбор полей Вы потребность , Кроме того, только явно назовите поля, которые Вы хотите иметь.
SELECT
news.id, news.title, news.author, news.posted,
users.id, users.name, users.registered
FROM
news
LEFT JOIN
users
ON
news.user = user.id
I just figured this out. It's probably a bad practice but it worked for me in this case.
I am one of the lazy people who doesn't want to alias or write out every column name with a table prefix.
You can select all of the columns from a specific table by using table_name.*
in your select statement.
When you have duplicated column names, mysql will overwrite from first to last. The data from the first duplicated column name will be overwritten when it encounters that column name again. So the duplicate column name that comes in last wins.
If I am joining 3 tables, each containing a duplicated column name, the order of the tables in the select statement will determine what data I am getting for the duplicate column.
Example:
SELECT table1.* , table2.* , table3.* FROM table1 LEFT JOIN table2 ON table1.dup = table2.dup LEFT JOIN table3 ON table2.dup = table3.dup;
In the example above, the value of dup
I get will be from table3
.
What if I want dup
to be the value from table1
?
Then I need to do this:
SELECT table3.* , table2.* , table1.* FROM table1 LEFT JOIN table2 ON table1.dup = table2.dup LEFT JOIN table3 ON table2.dup = table3.dup;
Now, table1
comes last, so the value of dup
will be the value from table1.
I got the value I wanted for dup
without having to write out every single freaking column and I still get all of the columns to work with. Yay!
I know the value of dup
should be the same in all 3 tables, but what if table3
doesn't have a matching value for dup
? Then dup
would be blank in the first example, and that would be a bummer.