Forum Moderators: coopster
I'm extremely new to php and to databases and I'm attempting to learn as I go I guess. What I've done so far is made a mysql database with 3 records/rows and 10 columns.
I've used PHP to connect to the database and I think thats been a success. I used this code that I found:
<?php
$dbhost = "(ip I got from tech support because localhost did not work";
$dbname = "jncafaac_schedules";
$dbuser = "jncafaac_villene";
$dbpasswd = "mypassword";
$db_connect_id = mysql_connect($dbhost, $dbuser, $dbpasswd);
if($db_connect_id)
{
print "connected<br>";
$dbselect = mysql_select_db($dbname);
if(!$dbselect)
{
print "Database not selected <br>";
}
else
{
print "Database selected <br>";
}
mysql_close($db_connect_id);
}
else
{
print "not Connected <br>";
}
?>
This returns the following:
connected
Database selected
So I'm assuming that worked.
Next I have this:
<?php
// Request the visitors from all the records
$result = @mysql_query("SELECT visitor FROM jncafaac_schedules.touch");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() ."</p>");
exit();
}
// Displays the text of each visitor in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo("<p>" . $row["visitor"] ."</p>");
}
?>
and I get this error:
Error performing query: SELECT command denied to user: '@localhost' for table 'touch'
If I change the code so that the SELECT command reads:
$result = @mysql_query("SELECT visitor FROM touch");
Then I get this error:
Error performing query: No Database Selected
Any ideas?
All I want to do now is display the information I have in the datbase. I can't seem to figure out how. I've tried playing around with the mysql_query but I don't know how to display any information. I think if I can get some of it to work, I can start to learn on my own by example.
<?php
$db_connect_id = mysql_connect($dbhost, $dbuser, $dbpasswd) or die(mysql_error());// this is for learning, in ready program you will write die("Could not connect to database");
mysql_select_db($dbname, $db_connect_id) or die(mysql_error());$result = mysql_query("SELECT visitor FROM touch") or die(mysql_error()); //you have already chosen a db with mysql_select_db()
if(mysql_num_rows($result))
{
while($row = mysql_fetch_array)
{
echo "Visitor: ".$row['visitor']."<br>";
}
}
however the error "SELECT command denied" means, that user localhost cannot do that query. You must grant the user that option:
GRANT SELECT on localhost;
Michal Cibor
Let me show you what code I currently have:
<?php
$dbhost = "127.0.0.1";
$dbname = "jncafaac_schedules";
$dbuser = "jncafaac_villene";
$dbpasswd = "george";
$db_connect_id = mysql_connect($dbhost, $dbuser, $dbpasswd);
if($db_connect_id)
{
print "connected<br>";
$dbselect = mysql_select_db($dbname);
if(!$dbselect)
{
print "Database not selected <br>";
}
else
{
print "Database selected - sweet<br>";
}
mysql_close($db_connect_id);
}
else
{
print "not Connected <br>";
}
?>
<?php
// Request the visitors from all the records
$result = @mysql_query("SELECT visitor FROM touch");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() ."</p>");
exit();
}
// Displays the text of each visitor in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo("<p>" . $row["visitor"] ."</p>");
}
?>
<?php$dbhost = "127.0.0.1";
$dbname = "jncafaac_schedules";
$dbuser = "jncafaac_villene";
$dbpasswd = "george";$db_connect_id = mysql_connect($dbhost, $dbuser, $dbpasswd) or die(mysql_error());
$dbselect = mysql_select_db($dbname) or die(mysql_error());
print "Database selected - sweet<br>";
?><?php
// Request the visitors from all the records
if(!($result = mysql_query("SELECT visitor FROM touch"))
{
echo("<p>Error performing query: " . mysql_error() ."</p>");
exit();
}
// Displays the text of each visitor in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo("<p>" . $row["visitor"] ."</p>");
}
//here only you can close connection to db, but I still don't recommend it
mysql_close($db_connect_id);
?>
And if you have been granted all then it should work fine. You didn't write what error did you encounter at this time.
Best regards
Michal Cibor