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 (http://webcheatsheet.com/php/connect_mysql_database.php?print=Y). In both cases, I sucessfully connected to mysql server but haven't succeded connecting to the database to retreive information from a table. I spend 3 days on it but I didn't find the solution. My guess is that it has something to do with the user, host and password I enter. I think that I don't use a username and a password so I enter "localhost" for the host, "root" for the user and "" for the password. I tried the command "SELECT * FROM mysql.user;" to get that information and I saw many users, hosts and passwords and I don't know which on is the right one nor which one I am currently using. A few of those passwords were probably created by me a month ago and now, i don't even remember how.
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é, in the following code from (http://webcheatsheet.com/php/connect_mysql_database.php?print=Y), I need to know why I can't retrieve information(I always get Could not select examples message) and If I correctelly entered the user, password and host?
<?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:
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 !!
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 (http://webcheatsheet.com/php/connect_mysql_database.php?print=Y). In both cases, I sucessfully connected to mysql server but haven't succeded connecting to the database to retreive information from a table. I spend 3 days on it but I didn't find the solution. My guess is that it has something to do with the user, host and password I enter. I think that I don't use a username and a password so I enter "localhost" for the host, "root" for the user and "" for the password. I tried the command "SELECT * FROM mysql.user;" to get that information and I saw many users, hosts and passwords and I don't know which on is the right one nor which one I am currently using. A few of those passwords were probably created by me a month ago and now, i don't even remember how.
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é, in the following code from (http://webcheatsheet.com/php/connect_mysql_database.php?print=Y), I need to know why I can't retrieve information(I always get Could not select examples message) and If I correctelly entered the user, password and host?
<?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:
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 !!