When I try the delete button I get no errors and the page refreshes but the data won't be deleted, so I think the problem is in passing along the id from a row.
None of the other solutions have worked for me so far. This is the table body.
<tbody>
<?php
$server = "localhost";
$user = "Website";
$pass = "pass";
$db = "db";
$conn = new mysqli($server, $user, $pass, $db);
if ($conn->connect_error)
{
die("connection to database failed");
}
$query = "SELECT * FROM koppeling";
$result = mysqli_query($conn, $query);
while($row = $result-> fetch_assoc())
{
echo "<tr>";
echo "<td>".$row['Wagon_ID']."</td>";
echo "<td name='wagon'>".$row['EPC']."
<a href='delete.php' class='table-button'
id='".$row['id']."'>Delete</a>";
echo "<td>";
echo "</td>";
echo "</tr>";
}
?>
</tbody>
delete.php file:
<?php
$id = $_GET['id'];
$server = "localhost";
$user = "Website";
$pass = "pass";
$db = "db";
$conn = new mysqli($server, $user, $pass, $db);
if ($conn->connect_error)
{
echo "Conn db failed";``
}
$query = "DELETE FROM koppeling WHERE id='$id';";
try
{
mysqli_query($conn, $query);
mysqli_close($conn);
header('Location: koppelen.php');
}
catch (Exception $e)
{
echo "$e";
}
?>
Can anyone help me figure out what is wrong, I've been stuck on this quite a while now.
Instead if this
while($row = $result-> fetch_assoc())
{
echo "<tr>";
echo "<td>".$row['Wagon_ID']."</td>";
echo "<td name='wagon'>".$row['EPC']."
<a href='delete.php' class='table-button'
id='".$row['id']."'>Delete</a>";
echo "<td>";
echo "</td>";
echo "</tr>";
}
Write this
while($row = $result-> fetch_assoc())
{
echo "<tr>";
echo "<td>".$row['Wagon_ID']."</td>";
echo "<td name='wagon'>".$row['EPC']."
<a href='delete.php?id=".$row['id']."' class='table-button'
id='".$row['id']."'>Delete</a>";
echo "<td>";
echo "</td>";
echo "</tr>";
}
It would be better if you used form with POST request or ajax instead of links.
If you really want to do it this way you can add query string to your href attribute.
$url = "delete.php?id=" . $row['id'];
<a href="<?php echo $url ?>" class='table-button'
id='".."'>Delete</a>";
You have to change the below line
$query = "DELETE FROM koppeling WHERE id='$id';";
to
$query = "DELETE FROM koppeling WHERE id='".$id."';";
so that id value will be properly populated in the query
Related
I want to retrieve the values from a database table and show them in a html table in a page.
I already searched for this but I couldn't find the answer, although this surely is something easy (this should be the basics of databases lol). I guess the terms I've searched are misleading.
The database table name is tickets, it has 6 fields right now (submission_id, formID, IP, name, email and message) but should have another field called ticket_number.
How can I get it to show all the values from the db in a html table like this:
<table border="1">
<tr>
<th>Submission ID</th>
<th>Form ID</th>
<th>IP</th>
<th>Name</th>
<th>E-mail</th>
<th>Message</th>
</tr>
<tr>
<td>123456789</td>
<td>12345</td>
<td>123.555.789</td>
<td>John Johnny</td>
<td>johnny#example.com</td>
<td>This is the message John sent you</td>
</tr>
</table>
And then all the other values below 'john'.
Example taken from W3Schools: PHP Select Data from MySQL
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
It's a good place to learn from!
Try this: (Completely Dynamic...)
<?php
$host = "localhost";
$user = "username_here";
$pass = "password_here";
$db_name = "database_name_here";
//create connection
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$connection = mysqli_connect($host, $user, $pass, $db_name);
//get results from database
$result = mysqli_query($connection, "SELECT * FROM products");
$all_property = array(); //declare an array for saving property
//showing property
echo '<table class="data-table">
<tr class="data-heading">'; //initialize table tag
while ($property = mysqli_fetch_field($result)) {
echo '<td>' . $property->name . '</td>'; //get field name for header
$all_property[] = $property->name; //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
foreach ($all_property as $item) {
echo '<td>' . $row[$item] . '</td>'; //get items using property value
}
echo '</tr>';
}
echo "</table>";
Here is an easy way to fetch data from a MySQL database using PDO.
define("DB_HOST", "localhost"); // Using Constants
define("DB_USER", "YourUsername");
define("DB_PASS", "YourPassword");
define("DB_NAME", "Yourdbname");
$dbc = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset-utf8mb4", DB_USER, DB_PASS);
$print = ""; // assign an empty string
$stmt = $dbc->query("SELECT * FROM tableName"); // fetch data
$stmt->setFetchMode(PDO::FETCH_OBJ);
$print .= '<table border="1px">';
$print .= '<tr><th>First name</th>';
$print .= '<th>Last name</th></tr>';
while ($names = $stmt->fetch()) { // loop and display data
$print .= '<tr>';
$print .= "<td>{$names->firstname}</td>";
$print .= "<td>{$names->lastname}</td>";
$print .= '</tr>';
}
$print .= "</table>";
echo $print;
Learn more about PHP and the MySQLi Library at PHP.net.
First, start a connection to the database. Do this by making all the string variables needed in order to connect, adjusting them to fit your environment, then creating a new connection object with new mysqli() and initializing it with the previously made variables as its parameters. Now, check the connection for errors and display a message whether any were found or not. Like this:
<?php
$servername = "localhost";
$username = "root";
$password = "yourPassword";
$database = "world";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $database);
echo "Connected successfully<br>";
?>
Next, make a variable that will hold the query as a string, in this case its a select statement with a limit of 100 records to keep the list small. Then, we can execute it by calling the mysqli::query() function from our connection object. Now, it's time to display some data. Start by opening up a <table> tag through echo, then fetch one row at a time in the form of a numerical array with mysqli::fetch_row() which can then be displayed with a simple for loop. mysqli::field_count should be self explanatory. Don't forget to use <td></td> for each value, and also to open and close each row with echo"<tr>" and echo"</tr>. Finally we close the table, and the connection as well with mysqli::close().
<?php
$query = "select * from city limit 100;";
$queryResult = $conn->query($query);
echo "<table>";
while ($queryRow = $queryResult->fetch_row()) {
echo "<tr>";
for($i = 0; $i < $queryResult->field_count; $i++){
echo "<td>$queryRow[$i]</td>";
}
echo "</tr>";
}
echo "</table>";
$conn->close();
?>
First, connect to the database:
$conn=mysql_connect("hostname","username","password");
mysql_select_db("databasename",$conn);
You can use this to display a single record:
For example, if the URL was /index.php?sequence=123, the code below would select from the table, where the sequence = 123.
<?php
$sql="SELECT * from table where sequence = '".$_GET["sequence"]."' ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$result=mysql_fetch_array($rs);
echo '<table>
<tr>
<td>Forename</td>
<td>Surname</td>
</tr>
<tr>
<td>'.$result["forename"].'</td>
<td>'.$result["surname"].'</td>
</tr>
</table>';
?>
Or, if you want to list all values that match the criteria in a table:
<?php
echo '<table>
<tr>
<td>Forename</td>
<td>Surname</td>
</tr>';
$sql="SELECT * from table where sequence = '".$_GET["sequence"]."' ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
echo '<tr>
<td>'.$result["forename"].'</td>
<td>'.$result["surname"].'</td>
</tr>';
}
echo '</table>';
?>
Surely a better solution would by dynamic so that it would work for any query without having to know the column names?
If so, try this (obviously the query should match your database):
// You'll need to put your db connection details in here.
$conn = new mysqli($server_hostname, $server_username, $server_password, $server_database);
// Run the query.
$result = $conn->query("SELECT * FROM table LIMIT 10");
// Get the result in to a more usable format.
$query = array();
while($query[] = mysqli_fetch_assoc($result));
array_pop($query);
// Output a dynamic table of the results with column headings.
echo '<table border="1">';
echo '<tr>';
foreach($query[0] as $key => $value) {
echo '<td>';
echo $key;
echo '</td>';
}
echo '</tr>';
foreach($query as $row) {
echo '<tr>';
foreach($row as $column) {
echo '<td>';
echo $column;
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
Taken from here: https://www.antropy.co.uk/blog/handy-php-snippets/
mysql_connect("localhost","root","");
mysql_select_db("database");
$query=mysql_query("select * from studenti");
$x=#mysql_num_rows($query);
echo "<a href='file.html'>back</a>";
echo "<table>";
$y=mysql_num_fields($query);
echo "<tr>";
for($i=0 ,$i<$y,$i++)
{
$values=mysql_field_name($query,$i);
echo "<th>$values</th>";
}
echo "</tr>";
while(list($p ,$n $your_table_list)=mysql_fetch_row($query))
{
print("<tr>\n".
"<td>$p</td>".
"</tr>/n");
}
?>
<?php
$mysql_hostname = "localhost";
$mysql_user = "ram";
$mysql_password = "ram";
$mysql_database = "mydb";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Oops some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Oops some thing went wrong");// we are now connected to database
$result = mysql_query("SELECT * FROM users"); // selecting data through mysql_query()
echo '<table border=1px>'; // opening table tag
echo'<th>No</th><th>Username</th><th>Password</th><th>Email</th>'; //table headers
while($data = mysql_fetch_array($result))
{
// we are running a while loop to print all the rows in a table
echo'<tr>'; // printing table row
echo '<td>'.$data['id'].'</td><td>'.$data['username'].'</td><td>'.$data['password'].'</td><td>'.$data['email'].'</td>'; // we are looping all data to be printed till last row in the table
echo'</tr>'; // closing table row
}
echo '</table>'; //closing table tag
?>
it would print the table like this
just read line by line so that you can understand it easily..
OOP Style :
At first connection with database.
<?php
class database
{
public $host = "localhost";
public $user = "root";
public $pass = "";
public $db = "db";
public $link;
public function __construct()
{
$this->connect();
}
private function connect()
{
$this->link = new mysqli($this->host, $this->user, $this->pass, $this->db);
return $this->link;
}
public function select($query)
{
$result = $this->link->query($query) or die($this->link->error.__LINE__);
if($result->num_rows>0)
{
return $result;
}
else
{
return false;
}
}
?>
Then :
<?php
$db = new database();
$query = "select * from data";
$result = $db->select($query);
echo "<table>";
echo "<tr>";
echo "<th>Name </th>";
echo "<th>Roll </th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> $row[name]</td>";
echo "<td> $row[roll]</td>";
echo "</tr>";
}
echo "</table>";
?>
I would like to ask if someone could help me with this problem.
I want to prefill the input and textbox from the SQL database. I have tried it many times and I didn't succeded.
This is the code:
<?php
$servername = "localhost";
$username = "********";
$password = "********";
$dbname = "kucharka";
$id = $_POST['id_recept'];
$conn = mysqli_connect($servername, $username, $password, $dbname);
$query = 'SELECT nazev, popis FROM recepty WHERE id = '.$id.'';
$result = mysqli_query($conn, $query);
if ($result) {
while( $row = mysqli_fetch_array($result) ){
echo "<li><label>Název</label></li>";
echo "<input class='blue' type='text' name='nazev' placeholder='Název'>".$row['nazev']."</input>";
echo "<li><label>Fotografie</label></li>";
echo "<div class='foto'>";
echo "<input type='file' id='real-file' name='foto[]' hidden='hidden' multiple='multiple'>";
echo "<button type='button' id='custom-button' class='blue_foto'>Stiskněte</button>";
echo "<span id='custom-text'>Žádná fotografie.</span>";
echo "</div>";
echo "<li><label>Druh</label></li>";
echo "<div class='select_custom'>";
echo "<select name='druh'>";
$conn = mysqli_connect($servername, $username, $password, $dbname);
$query = 'SELECT id, druh FROM druh';
$result = mysqli_query($conn, $query);
if ($result) {
while ($row = mysqli_fetch_array($result)) {
echo "<option value=".$row['id'].">".$row['druh']."</option>";
}
}
echo "</select>";
echo "</div>";
echo "<li><label>Popis</label></li>";
echo "<textarea name='text' placeholder='Popis'>".$row['popis']."</textarea>";
echo "<li><input type='submit' name='submit' class='orange_input' value='Potvrďte'></li>";
}
}
?>
Thank you in advance.
Ahhh! You have a While Loop INSIDE a While Loop!!
And both are processing mysqli_fetch_array($result) and therefore the inner loop destroys the $result used in the outer loop.
<?php
$servername = "localhost";
$username = "********";
$password = "********";
$dbname = "kucharka";
$id = $_POST['id_recept'];
$conn = mysqli_connect($servername, $username, $password, $dbname);
$query = 'SELECT nazev, popis FROM recepty WHERE id = '.$id.'';
$result = mysqli_query($conn, $query);
if ($result) {
while( $row = mysqli_fetch_array($result) ){
echo "<li><label>Název</label></li>";
echo "<input class='blue' type='text' name='nazev' placeholder='Název'>".$row['nazev']."</input>";
echo "<li><label>Fotografie</label></li>";
echo "<div class='foto'>";
echo "<input type='file' id='real-file' name='foto[]' hidden='hidden' multiple='multiple'>";
echo "<button type='button' id='custom-button' class='blue_foto'>Stiskněte</button>";
echo "<span id='custom-text'>Žádná fotografie.</span>";
echo "</div>";
echo "<li><label>Druh</label></li>";
echo "<div class='select_custom'>";
echo "<select name='druh'>";
// changed code here
// 1 you dont need to connect twice
//$conn = mysqli_connect($servername, $username, $password, $dbname);
$query = 'SELECT id, druh FROM druh';
// use different var here
// and then use it in the related function calls
$result1 = mysqli_query($conn, $query);
if ($result1) {
// and of course use a different var to hold the row data
// so you dont overwrite that also
while ($row1 = mysqli_fetch_array($result1)) {
echo "<option value=".$row1['id'].">".$row1['druh']."</option>";
}
}
echo "</select>";
echo "</div>";
echo "<li><label>Popis</label></li>";
echo "<textarea name='text' placeholder='Popis'>".$row['popis']."</textarea>";
echo "<li><input type='submit' name='submit' class='orange_input' value='Potvrďte'></li>";
}
}
Seperate issue, you dont appear to have a <form> and </form> tag in this code. Without that the data placed in input fields will never be transmitted as a form to the PHP script for processing
BIG NOTE
Your script is open to SQL Injection Attack.
Even if you are escaping inputs, its not safe!
You should consider using prepared parameterized statements in either the MYSQLI_ or PDO API's instead of concatenated values
So to prepare the relevant dangerous query
$query = 'SELECT nazev, popis FROM recepty WHERE id = ?';
$stmt = $conn->prepare($conn, $query);
$stmt->bind_param('i', $_POST['id_recept']);
$stmt->execute();
$result = $stmt->get_result();
. . .
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 2 years ago.
Hey guys i am trying to display a list off all users in my database. not sure how to get along with it. Getting confused with mysqli and sql..
my dadtabase connection file is :
class Dbconnect extends PDO
{
private $dbengine = 'mysql';
private $dbhost = 'localhost';
private $dbuser = 'root';
private $dbpassword = '';
private $dbname = 'test';
public $dbh = null;
function __construct()
{
try{
$this->dbh = new PDO("".$this->dbengine.":host=$this->dbhost; dbname=$this->dbname", $this->dbuser, $this->dbpassword);
$this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}
catch (PDOException $e){
$e->getMessage();
}
}
}
?>
I tired this on it but getting an error.
$sqlget = "SELECT * FROM user";
$sqldata = mysqli_query($dbh, $sqlget) or die('error users');
echo "<table>";
echo " <th>User Name </th> <th>Email</th> <th> Some Name</th>";
while($row = mysql_fetch_array($sqldata, MYSQL_ASSOC)) {
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['email'];
echo "</td><td>";
echo $row['some'];
echo "</td>";
}
echo "</table>"
?>
I have also created a table to insert the data on them..
User Name . Email . lastName
not sure how to proceed. especially as i just got into php.. help will be much appreciaited to come up with a php handling file for me.
and any further tips to delete or edit users...Thanks in Advance people..!!!!!!
You must use PDO functions:
$db = new PDO("".$dbengine.":host=$dbhost; dbname=$dbname", $dbuser, $dbpassword);
$q = "SELECT * FROM users";
$result = $db->query($q)->fetchall();
foreach ($result as $user) {
echo $user['name'];
}
Try this:
$dbh = new PDO('mysql:host=localhost;dbname=test;charset=utf8mb4', 'root', '');
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$sqlget = "SELECT * FROM user";
$sqldata = $dbh->query($sqlget) or die('error users');
echo "<table>";
echo " <th>User Name </th> <th>Email</th> <th> Some Name</th>";
while($row = $sqldata->fetch(PDO::FETCH_ASSOC)) {
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['email'];
echo "</td><td>";
echo $row['some'];
echo "</td>";
}
echo "</table>"
I am trying to use PHP in two different parts of a page. But when I try to load the page I receive the error: undefined variable $conn. Why am I getting this error?
<tbody>
<?php
$sql = "SELECT id, name, price FROM periperichicken";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>£" . $row["price"] . "</td>";
echo "<td><input type=\"text\" name=\"amount\"
class=\"amount-type\"
placeholder=\"Amount\"/></td>";
echo "<td>Add to cart</td>";
echo "</tr>";
}
}
mysqli_close($conn);
?>
</tbody>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pizza";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
You use $conn at the top of your script
$result = mysqli_query($conn, $sql);
Yet you define it further down
$conn = mysqli_connect($servername, $username, $password, $dbname);
You'll need to move that section up to the first before you can run any queries.
<?PHP
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pizza";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<tbody>
<?php
$sql = "SELECT id, name, price FROM periperichicken";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>£" . $row["price"] . "</td>";
echo "<td><input type=\"text\" name=\"amount\" class=\"amount-type\" placeholder=\"Amount\"/></td>";
echo "<td>Add to cart</td>";
echo "</tr>";
}
}
mysqli_close($conn);
?>
</tbody>
You're defining and connecting to the database after you try to use it. Move the connection part above the query. If this is the only place that you will be using the connection the page then this is fine. But if other pages or sections of the page will use the connection you should look at placing this in a common file that is included at the top of every page that uses it.
<tbody>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pizza";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, name, price FROM periperichicken";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>£" . $row["price"] . "</td>";
echo "<td><input type=\"text\" name=\"amount\" class=\"amount-type\" placeholder=\"Amount\"/></td>";
echo "<td>Add to cart</td>";
echo "</tr>";
}
}
mysqli_close($conn);
?>
</tbody>
I'd be pretty sure that you need to define $conn before you call it in the top part of the php code.
PHP is loaded and executed sequentially... You are defining $conn after using it. Try doing it before. I.e.: Move the second blob of PHP above the first one.
For more on PHP execution order, check out this question.
I am trying to retrieve information from my database depending on the ID a user types into my URL.
For example: If USER A went to www.exampleurl.com/index.php?id=1 it would echo out the user's information which has an ID of 1. Same thing if the id was 2, 3, etc. Users are entering their information via a form in a different file called submit.php.
Here is my code to retrieve data depending on ID :
<?php
$id = $_GET['id'];
//Variables for connecting to your database.
$hostname = "";
$username = "";
$dbname = "";
$password = "";
$usertable = "";
//Connecting to your database
$con = mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname, $con);
$query = "SELECT * FROM $usertable WHERE id = $id LIMIT 1";
$result = mysql_query($query, $con);
echo "Hello, " . $result['name'];
?>
Any ideas on if my SELECT request is wrong?
EDIT
Here is my code for showing the data altogether in a table. This works fine.
<?php
//Variables for connecting to your database.
$hostname = "";
$username = "";
$dbname = "";
$password = "!";
$usertable = "";
//Connecting to your database
$con = mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname, $con);
//Fetching from your database table.
$query = "SELECT * FROM $usertable";
$result = mysql_query($query, $con);
echo "<table border=1>
<tr>
<th> ID </th>
<th> Name </th>
<th> Age </th>
</tr>";
while($record = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['name'] . "</td>";
echo "<td>" . $record['age'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
→ Try This:
You should consider using PHP PDO as it is safer and a more object oriented approach:
$usertable = "";
$database = new PDO( 'mysql:host=localhost;dbname=DB_NAME', 'DB_USER_NAME', 'DB_USER_PASS' );
$statement = $database->prepare('SELECT * FROM $usertable');
$statement->execute();
$count = $statement->rowCount();
if( $count > 0 ) {
$R = $statement->fetchAll( PDO::FETCH_ASSOC );
for( $x = 0; $x < count($R); $x++ ) {
echo "<tr>";
echo "<td>" . $R[ $x ]['id'] . "</td>";
echo "<td>" . $R[ $x ]['name'] . "</td>";
echo "<td>" . $R[ $x ]['age'] . "</td>";
echo "</tr>";
}
}
else { echo "Error!"; }
you need to use mysql_fetch_assoc function for retrieve the results.
$result = mysql_fetch_assoc(mysql_query($query, $con));
echo "Hello, " . $result['name'];
You should be error checking your mysql_querys:
$query = "SELECT * FROM $usertable WHERE id = $id LIMIT 1";
$result = mysql_query($query, $con);
if(!result)
echo mysql_error();
You should also retrieve the results:
$array = mysql_fetch_assoc($result);
I'll consider some secure features like
Check if $_GET['id'] is set and if is int
Apply Mysql escape with mysql_escape_string() function