Using Variables in SQL Statements

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Using Variables in SQL Statements

Post by a94060 »

Hi,i was wondering if anyone wold be able to post be a simple guide on how to use variables in SELECTand INSERT statements. I think it might also help other people in the community who may not want to ask this question.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

You don't say what DBMS you're working with, but here's how to do it within MySQL.
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

sorry if i was vauge. i mean using php variables in sql statments and on mysql as you said.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

SQL statements are strings, so you would use PHP's string capabilities. See Strings and String Functions. Those two sections are part of the essential reading for all PHP programmers and that information needs to be put to memory to be effective in PHP.
(#10850)
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

ok,thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Using Variables in SQL Statements

Post by RobertGonzalez »

a94060 wrote:Hi,i was wondering if anyone wold be able to post be a simple guide on how to use variables in SELECTand INSERT statements. I think it might also help other people in the community who may not want to ask this question.

Code: Select all

<?php
$numvar = 4;
$stringvar = 'This is a lousy string';

// Insert 
$sql = "INSERT INTO mytable (table_id) VALUES ($numvar)";

// Select
$sql = "SELECT * FROM mytable WHERE table_id = $numvar";

// Update
$sql = "UPDATE mytable SET table_text = '$stringvar' WHERE table_id = $numvar";

// Delete
$sql = "DELETE FROM mytables WHERE table_id = $numvar";
?>
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

ok,thanks for making it simple for me,i will jsut add this thread to my favorites:) (sorry about not undestanding that joni which you showed me)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Its all good. When I first saw them my stomach started hurting :D . It'll come to you. Either by revelation or necessity, but it'll come to you.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Ah, I see what you meant now, I thought you were referring to variables within SQL, not in the string passed to the DBMS. PHP question, not SQL.
Post Reply