Trying to load a HTML form with data from my sql database. I use Zampp phpmyadmin
<?php
$user = 'root';
$password = '';
$database="client";
mysql_connect(localhost,client);
@mysql_select_db($client) or die( "Unable to select database");
echo $query = "SELECT * $title,$service_number,$firstName,$LastName,$dob,$address,$postcode,$tel_number,$mob_number,$email,$partner_id ";
$result = mysql_query($query);
mysql_close();
?>
What do I have wrong?
mysql_
functions are deprecated. Use PDO
or mysqli_
@
; use error handlers (ie. mysql_error
)Sample use of mysqli_
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
printf ("%s (%s)
", $row["Name"], $row["CountryCode"]);
}
/* free result set */
mysqli_free_result($result);
}
/* close connection */
mysqli_close($link);
?>
Your query isn't structured well. You need to specify the table name from which the records should be selected. For example, you should be doing something like:
SELECT * FROM `tblname`
That will select all fields for each record in the table. You can specify specific fields by including them (without the *
wildcard) as follows:
SELECT `title`, `service_number` FROM `tblname`
You should be advised though that the mysql_*
set of functions are deprecated now, and you should be using either PDO or MySQLi.
You should also be sure to escape the variables that you're passing in. Your code is wide open to SQL injection in its current form.
You're query is all wrong. Proper SELECT
query is such:
SELECT fields FROM table;
SELECT * FROM table;
You are using PHP variables in your query ($title,$service_number...
) which, in the code provided, does not exist. You need to fix your query.
Note: it's fields
OR *
, not both. Also, don't use MySQL_*, it has been deprecated as of PHP 5.5. Use MySQLi_* or PDO
this should get you going
<?php
$dsn = 'mysql:host=localhost;dbname=client';
$username = 'root';
$password = '';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$dbh = new PDO($dsn, $username, $password, $options);
$sql = $dbh->prepare("SELECT title,service_number,FirstName,LastName,dob,address,postcode,tel_number,mob_number,email,partner_id FROM client_table");
$sql->execute();
$results = $sql->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $r){
var_dump($r);
}