Hi,
I would please like to know how I can retreive sql database table using php? I tried an exemple from a book and an exemple code from a website ([url]http://webcheatsheet.com/php/connect_mysql_database.php?print=Y
Additional information:
Before connecting to mysql I went to the bash application and entered the following parameters:
myuser=’user’
mypass=’passe’
mydb=’namr’
myq=”Quers”
alias mysql=/usr/local/mysql/bin/mysql
mysql –user=root mysql
So from the following, I would like to know if I should enter
$username = “user”;
$password = “passe”;
$hostname = “localhost”;
in my case ? I also tried it but it still didn’t solve the situation.
In resumé, from the following code from ([url]http://webcheatsheet.com/php/connect_mysql_database.php?print=Y
[code=php]<?php
$username = “root”;
$password = “”;
$hostname = “localhost”;
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die(“Unable to connect to MySQL”);
echo “Connected to MySQL<br>”;
//select a database to work with
$selected = mysql_select_db(“examples”,$dbhandle)
or die(“Could not select examples”);
//execute the SQL query and return records
$result = mysql_query(“SELECT id, model,year FROM cars”);
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo “ID:”.$row{‘id’}.” Name:”.$row{‘model’}.”Year: “. //display the results
$row{‘year’}.”<br>”;
}
//close the connection
mysql_close($dbhandle);
?>
To create ‘examples’ database on your MySQL server I run the following script:
[CODE]CREATE DATABASE `examples`;
USE `examples`;
CREATE TABLE `cars` (
`id` int UNIQUE NOT NULL,
`name` varchar(40),
`year` varchar(50),
PRIMARY KEY(id)
);
INSERT INTO cars VALUES(1,’Mercedes’,’2000′);
INSERT INTO cars VALUES(2,’BMW’,’2004′);
INSERT INTO cars VALUES(3,’Audi’,’2001′);
Thank you very much !!