Page 1 of 1
Using Variables in SQL Statements
Posted: Mon Jun 05, 2006 12:22 pm
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.
Posted: Mon Jun 05, 2006 12:47 pm
by bdlang
You don't say what DBMS you're working with, but
here's how to do it within MySQL.
Posted: Mon Jun 05, 2006 2:44 pm
by a94060
sorry if i was vauge. i mean using php variables in sql statments and on mysql as you said.
Posted: Mon Jun 05, 2006 2:54 pm
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.
Posted: Mon Jun 05, 2006 3:05 pm
by a94060
ok,thanks
Re: Using Variables in SQL Statements
Posted: Mon Jun 05, 2006 3:05 pm
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";
?>
Posted: Mon Jun 05, 2006 3:08 pm
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)
Posted: Mon Jun 05, 2006 3:19 pm
by RobertGonzalez
Its all good. When I first saw them my stomach started hurting

. It'll come to you. Either by revelation or necessity, but it'll come to you.
Posted: Mon Jun 05, 2006 3:22 pm
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.