Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Variable in Select Statement

I've written this PHP-Script which is working, and now I want to change the row name into a variable to (not sure if row is correct), I mean the "name" from the select name... I've tried nearly everything, but nothing gave me the right result. I know that the normal thing how I can use variables in a statement like ("'. $var .'") won't work.

<?php
require_once 'config.php';

$id = $_GET["id"]; //ID OF THE CURRENT CONTACT
$user = $_GET["user"];  //ID OF THE CURRENT USERS

$query = mysql_query("SELECT name FROM contacts WHERE contact_id='". mysql_real_escape_string( $id ) ."' and user_id='1';");

$retval = mysql_fetch_object($query)->name;

$retval = trim($retval);
echo $retval;
?>
like image 789
mikepenz Avatar asked Aug 13 '10 10:08

mikepenz


People also ask

What is select * in PHP?

The SELECT statement is used to select data from one or more tables: SELECT column_name(s) FROM table_name. or we can use the * character to select ALL columns from a table: SELECT * FROM table_name.

How do you declare a variable in PHP?

Rules for PHP variables: A variable starts with the $ sign, followed by the name of the variable. A variable name must start with a letter or the underscore character. A variable name cannot start with a number.


1 Answers

This is much easier isn't it?

$sql_insert = 
"INSERT INTO customers (        
`name`,
`address`,
`email`,
`phone`
) 
VALUES (        
'$name',
'$address',     
'$email',
'$phone'
)";
like image 98
Izzy Avatar answered Nov 15 '22 10:11

Izzy