Questions tagged [mysql]

MySQL is a free, open source Relational Database Management System (RDBMS) that uses Structured Query Language (SQL). DO NOT USE this tag for other DBs such as SQL Server, SQLite etc. These are different DBs which all use SQL to manage the data.

2781
votes
28answers
1.4m views

How can I prevent SQL injection in PHP?

If user input is inserted without modification into an SQL query, then the application becomes vulnerable to SQL injection, like in the following example:$unsafe_variable=$_POST['user_input']; ...
2167
votes
14answers
178k views

Why shouldn't I use mysql_* functions in PHP?

What are the technical reasons for why one shouldn't use mysql_* functions? (e.g. mysql_query(), mysql_connect() or mysql_real_escape_string())?Why should I use something else even if they work on ...
853
votes
32answers
686k views

mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource

I am trying to select data from a MySQL table, but I get one of the following error messages:mysql_fetch_array() expects parameter 1 to be resource, boolean givenormysqli_fetch_array() ...
890
votes
30answers
153k views

Reference - What does this error mean in PHP?

What is this?This is a number of answers about warnings, errors and notices you might encounter while programming PHP and have no clue how to fix. This is also a Community Wiki, so everyone is ...
480
votes
11answers
130k views

When to use single quotes, double quotes, and back ticks in MySQL

I am trying to learn the best way to write queries. I also understand the importance of being consistent. Until now, I have randomly used single quotes, double quotes, and back ticks without any real ...
968
votes
14answers
120k views

UTF-8 all the way through

I'm setting up a new server, and want to support UTF-8 fully in my web application. I have tried in the past on existing servers and always seem to end up having to fall back to ISO-8859-1.Where ...
478
votes
4answers
186k views

SQL injection that gets around mysql_real_escape_string()

Is there an SQL injection possibility even when using mysql_real_escape_string() function?Consider this sample situation. SQL is constructed in PHP like this:$login=mysql_real_escape_string(...
77
votes
5answers
9k views

Can I mix MySQL APIs in PHP?

I have searched the net and so far what I have seen is that you can use mysql_ and mysqli_ together meaning:<?php$con=mysqli_connect("localhost", "root" ,"" ,"mysql");if( mysqli_connect_errno( ...
48
votes
1answer
90k views

Syntax error due to using a reserved word as a table or column name in MySQL

I'm trying to execute a simple MySQL query as below:INSERT INTO user_details (username, location, key)VALUES ('Tim', 'Florida', 42)But I'm getting the following error:You have an error in ...
227
votes
8answers
225k views

MySQL pivot table

If I have a MySQL table looking something like this:company_name action pagecount-------------------------------Company A PRINT 3Company A PRINT 2Company A PRINT 3...
837
votes
29answers
766k views

SQL select only rows with max value on a column

I have this table for documents (simplified version here):+------+-------+--------------------------------------+| id | rev | content |+------+-------+-------------...
38
votes
3answers
12k views

Trouble with UTF-8 characters; what I see is not what I stored

I tried to use UTF-8 and ran into trouble.I have tried so many things; here are the results I have gotten:???? instead of Asian characters. Even for European text, I got Se?or for Señor.Strange ...
22
votes
1answer
4k views

My PDO Statement doesn't work

This is my PHP sql statement and it's returning false while var dumping$password_md5=md5($_GET['password']);$sql=$dbh->prepare('INSERT INTO users(full_name, e_mail, username, password, ...
672
votes
21answers
412k views

Retrieving the last record in each group - MySQL

There is a table messages that contains data as shown below:Id Name Other_Columns-------------------------1 A A_data_12 A A_data_23 A A_data_34 B ...
263
votes
12answers
430k views

Connect Java to a MySQL database

How do you connect to a MySQL database in Java?When I try, I get java.sql.SQLException: No suitable driver found for jdbc:mysql://database/tableat java.sql.DriverManager.getConnection(...
461
votes
14answers
342k views

MySQL Error 1093 - Can't specify target table for update in FROM clause

I have a table story_category in my database with corrupt entries. The next query returns the corrupt entries:SELECT * FROM story_category WHERE category_id NOT IN (SELECT DISTINCT category....
58
votes
1answer
68k views

MySQL pivot row into dynamic number of columns

Lets say I have three different MySQL tables:Table products: id | name1 Product A2 Product BTable partners: id | name1 Partner A2 Partner BTable sales:partners_id | ...
287
votes
13answers
253k views

Using LIMIT within GROUP BY to get N results per group?

The following query:SELECTyear, id, rateFROM hWHERE year BETWEEN 2000 AND 2009AND id IN (SELECT rid FROM table2)GROUP BY id, yearORDER BY id, rate DESCyields:year id rate2006 p01 8...
470
votes
14answers
453k views

How to do a FULL OUTER JOIN in MySQL?

I want to do a Full Outer Join in MySQL. Is this possible? Is a Full Outer Join supported by MySQL?
19
votes
3answers
65k views

check if row exists with mysql

I need help checking if a row exists. I am getting "email no longer exists publisher@example.com". Is there a better way to check if row exists with mysqli?if (count($_POST)) {$email=$dbl-&...
748
votes
10answers
497k views

INNER JOIN ON vs WHERE clause

For simplicity, assume all relevant fields are NOT NULL.You can do:SELECTtable1.this, table2.that, table2.somethingelseFROMtable1, table2WHEREtable1.foreignkey=table2.primarykey...
141
votes
26answers
375k views

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

I'm working on getting my database to talk to my Java programs.Can someone give me a quick and dirty sample program using the JDBC?I'm getting a rather stupendous error:Exception in thread "main"...
149
votes
1answer
370k views

The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead [duplicate]

When I attempt to connect to a MySQL server from PHP, I see the following error:Deprecated: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /path/...
342
votes
13answers
127k views

mysqli or PDO - what are the pros and cons? [closed]

In our place we're split between using mysqli and PDO for stuff like prepared statements and transaction support. Some projects use one, some the other. There is little realistic likelihood of us ever ...
419
votes
10answers
339k views

How to do a regular expression replace in MySQL?

I have a table with ~500k rows; varchar(255) UTF8 column filename contains a file name;I'm trying to strip out various strange characters out of the filename - thought I'd use a character class: [^a-...
213
votes
21answers
391k views

ROW_NUMBER() in MySQL

Is there a nice way in MySQL to replicate the SQL Server function ROW_NUMBER()?For example:SELECT col1, col2, ROW_NUMBER() OVER (PARTITION BY col1, col2 ORDER BY col3 DESC) AS intRow...
96
votes
10answers
118k views

Get top n records for each group of grouped results

The following is the simplest possible example, though any solution should be able to scale to however many n top results are needed:Given a table like that below, with person, group, and age ...
71
votes
13answers
150k views

Project Links do not work on Wamp Server

I am installing the Wamp Server on another computer to run a mid-sized database and UI. I have been successful in blocking IIS and routing the server to Localhost:8080. But whenever I try to access on ...
377
votes
6answers
683k views

SQL query return data from multiple tables

I would like to know the following:how to get data from multiple tables in my database?what types of methods are there to do this?what are joins and unions and how are they different from one ...
129
votes
13answers
170k views

How to create a MySQL hierarchical recursive query

I have a MySQL table which is as follows:id | name | parent_id19 | category1 | 020 | category2 | 1921 | category3 | 2022 | category4 | 21......Now, I want to have a single ...

153050per page
angop.ao, elkhabar.com, noa.al, afghanpaper.com, bbc.com, time.com, cdc.gov, nih.gov, xnxx.com, github.com,