Posted by: Rinie Engelen Date: October 19, 2008 05:04PM Hello everone, I have this code: CREATE TABLE testgeval12 ( `ID_nr` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(50), `e-mail` VARCHAR(80), `show_e-mail` TINYINT(1), ⦠For example, the following statement finds the city and country of the customer number 103 and stores the data in two corresponding variables ⦠Set some value to the variable with the help of SET command â mysql> SET @engine='start'; Query OK, 0 rows affected (0.00 sec) After that, we can check the value we have given above. January 22, 2008 05:51PM Re: how to use PHP variable in MYSQL query? I have many example for it, that I will give you in the below. Forum List » PHP. The syntax for assigning a value to a SQL variable within a SELECT query is @var_name := value, where var_name is the variable name and value is a value that youâre retrieving. Example 1. If not, what was the error message? For now, you should know that all PHP variables used inside an SQL query must either be escaped or included inside the query using prepared statements. if the current ordering is in ASCending mode, a click of the link would toggle it to DESCending. how to use PHP variable in MYSQL query? Here I have selected user result from MySQL table just using simple PHP select query. By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy, 2020 Stack Exchange, Inc. user contributions under cc by-sa. Alternatives à cette fonction : I'd like to use the post variable value in my sql query to sort data in order chosen by a user. This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Specifies the SQL query to send (should not end with a semicolon) connection: Optional. Let us create a session variable using SET command. Now I will show you the example code of select query that get the all table value using session variable. This function returns FALSE if there are no more rows. $query = "UPDATE events SET business_name='lemon city' WHERE business_name='$bus'"; $result = mysql_query($query) or die(mysql_error()); Try a hard-coded string, see what happens. I'm aware this is subject to sql injection, however, I'm doing this for training purposes on my local server. For this reason, I usually build the query string in a variable ($sql or $query, for example) so that you can use that variable both in mysql_query() as well as outputting it when an error occurs. Posted by ... New Topic. Then, you can use mysqli_query() to perform queries against the database. ok, I didn't understand your second part about the link. Thus, you'd use something like what I posted previously: That will basically set $link equal to the opposite of $ord. SELECT * FROM NHL_GBG_PLAYERS ORDER BY DESC id in /DB_API/sort_column.php on line 20 You can also provide a link from the web. Try hard-refreshing this page to fix the error. March 17, 2007 10:20AM Re: how to use PHP variable in MYSQL query? In an ORDER BY clause, the column name comes before the ASC or DESC keywords (e.g. The syntax is as follows â SET @anyVariableName = ( yourQuery); To understand the above concept, let ⦠//EDIT Hold on, if you get blanks in business_name field when you run the query. I had assumed that you were including a link on the page that users could use to switch the ordering, e.g. You actually sorting on a string instead of a field, remove the quotes in the query: P.S. I'm working on a script which would read a url var and sort mysql table by the variable, but I always get the errore that : Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in DB_API/sort_column.php on line 13, Can somebody help me fix my problem. MySQL SELECT INTO multiple variables example. Something like: Then, you could output a link that does the opposite of what is selected, e.g. Content reproduced on this site is the property of the respective copyright holders. query: Required. New Topic. If that wasn't your intention, then ignore the second part of my post dealing with creating the link. I think it's the variable that is not use right in the MYSQL query mysql_db_query â Sélectionne une base de données et y exécute une requête Avertissement Cette fonction était obsolète en PHP 5.3.0, et la totalité de l'extension original MySQL a été supprimée en PHP 7.0.0. Thanks, https://stackoverflow.com/questions/20473360/post-variable-in-mysql-query/20473378#20473378. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used. Okay, so.. did it work? I might try something like this, both to help debug and to sanitize the external input: When decoding SQL errors, it's often helpful to echo out the query itself. ok, I didn't understand your second part about the link. Instead, the MySQLi or PDO_MySQL extension should be used. March 16, 2007 07:33PM Re: how to use PHP variable in MYSQL query? Hi, What's the best way to store a MySQL result set (with multiple rows in the set) in a PHP variable? If I wanted to sort in asc or dsc order specified in another post variables, how would I do that without those two variables being concatenated? Apprently, the 'col' element you're POST'ing to the script isn't a column (or else it is, but the column name contains illegal characters/words and must be delimited with backticks). SELECT * FROM NHL_GBG_PLAYERS ORDER BY `` in /DB_API/sort_column.php on line 19 SQL error. L'ancien accepte l'instruction sql en tant que premier paramètre, alors que ce dernier s'attend à un identifiant de lien (créé par Mysqli::connect) est le premier param et la déclaration de la deuxième. Check if $GET['ord'] is empty (or invalid) and, if so, set a default value. 1. When you use variables to create your query, you must ALWAYS make sure that these variables are safe to use. "ORDER BY col1 ASC"). [edit]After posting this answer, some useful comments were made to your OP by MarcB and to the other answer by zan. Something went wrong while trying to load the full version of this site. So if I am getting the correct info from the db and setting the variables correctly, which I must be to be able to echo them successfully, then I can only assume it ⦠Il semble que vous êtes confus mysql_query avec mysqli_query.