/    Sign up×
Community /Pin to ProfileBookmark

How to retreive sql database using PHP?

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[/url]) . 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é, from the following code from ([url]http://webcheatsheet.com/php/connect_mysql_database.php?print=Y[/url]), 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?

[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);
?>[/code]

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′);[/CODE]

Thank you very much !!

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 16.2013 — You may want to check some return values, then see if PHP and/or MySQL can tell you anything interesting, e.g.:
[code=php]
//execute the SQL query and return records
$result = mysql_query("SELECT id, model,year FROM cars");
if($result == false) {
throw new Exception("Query failed: ".PHP_EOL.mysql_error());
}
[/code]
Copy linkTweet thisAlerts:
@progcoder2authorAug 18.2013 — Thx, I retried it with your advice but I still have the same problem . The problem is before that istruction, it's when I try to conect to the database:
$selected = mysql_select_db("examples",$dbhandle)


or die("Could not select examples"); [/QUOTE]

It doesn't connect and never recognize database examples. I always get Could not select examples. Why?
Copy linkTweet thisAlerts:
@NogDogAug 18.2013 — Try using mysql_error() with that error message, too, to see what the DB is complaining about.
Copy linkTweet thisAlerts:
@progcoder2authorAug 19.2013 — Try using mysql_error() with that error message, too, to see what the DB is complaining about.[/QUOTE]

It doesn't recognise database exemples when I do mysql error(). but in mysql when I type show tables, exemples is there. Why?
Copy linkTweet thisAlerts:
@NogDogAug 19.2013 — "exemples" or "examples"? ?

Does your script log in with a different database user than whatever you use to manually look at the database? If so, they might have different permissions with regard to what they have access to.
Copy linkTweet thisAlerts:
@olidevApr 19.2018 — For that you will have to establish the connection with your website. To connect mysql database with PHP website, create a db_connection.php and add the following code in it:
``<i>
</i>&lt;?php

function OpenCon()
{
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "1234";
$db = "example";


$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %sn". $conn -&gt; error);


return $conn;
}

function CloseCon($conn)
{
$conn -&gt; close();
}

?&gt;<i>
</i>
`</CODE>

Next, check your connection by creating index.php file and adding following code in it:

<CODE>
`<i>
</i>&lt;?php
include 'db_connection.php';

$conn = OpenCon();

echo "Connected Successfully";

CloseCon($conn);

?&gt;<i>
</i>
``


Source: [Connect with MySQL with PHP](https://www.cloudways.com/blog/connect-mysql-with-php/)
Copy linkTweet thisAlerts:
@ginerjmApr 19.2018 — You DO realize that this topic is 5 years old?
Copy linkTweet thisAlerts:
@rootApr 21.2018 — The moderation tools for this forum software do not extend to being able to lock a subject or do anything meaningful in the terms of the meaning "Moderation", this forum software does not give anything in the realms of coming anywhere near moderation tools. Only people with the tools are Admins.

So I am unable to lock these types of posts that pop up that are not relevant.
×

Success!

Help @progcoder2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...