PDA

View Full Version : show existing data from mysql problem!



angst
06-22-2005, 08:30 PM
Hello,

I'm trying to use this code:



<?php
$link = mysql_connect('localhost', 'traumati_six', '******');
mysql_select_db ("traumati_six");

$result = mysql_query('SELECT name FROM accounts');
if (!$result) {
die('Could not query:' . mysql_error());
}
echo mysql_result($email); // outputs email address

mysql_close($link);
?>



but i keep getting this error:
Could not query:No database selected


can anyone tell me what i've done wrong here??

thanks in advance for your site!
-SiX

delinear
06-22-2005, 10:22 PM
Well, the most likely cause judging from that error message is that either the login details are incorrect or the database isn't called traumati_six. Try specifically outputting a failure from mysql_connect and mysql_select_db to find out where it's failing:

$link = mysql_connect('localhost', 'traumati_six', '******') or die('Login details incorrect');
$db_selected = mysql_select_db('traumati_six', $link) or die('Database name incorrect');

angst
06-22-2005, 11:32 PM
ok, your right, i fixed the db name, now it connects,




<?php
$link = mysql_connect('localhost', 'traumati_six', 'six') or die('Login details incorrect');
$db_selected = mysql_select_db('test', $link) or die('Database name incorrect');

$result = mysql_query('SELECT * FROM accounts');
if (!$result) {
die('Could not query:' . mysql_error());
}
echo mysql_result($email); // outputs email address

mysql_close($link);
?>



but now i'm getting this error:



Warning: Wrong parameter count for mysql_result() in /home/traumati/public_html/six/conntemp.php on line 9

line 9 is:
echo mysql_result($email); // outputs email address

now i know the name and case is correct, "email"

angst
06-23-2005, 12:04 AM
ok, i found what i was doing wrong,




<?php
$link = mysql_connect('localhost', 'traumati_six', 'six') or die('Login details incorrect');
$db_selected = mysql_select_db('test', $link) or die('Database name incorrect');

$result = mysql_query('SELECT * FROM accounts');
#if (!$result) {
# die('Could not query:' . mysql_error());
#}
echo mysql_result($result, 0); // outputs email address
print "<br />";
echo mysql_result($result, 1); // outputs email address

mysql_close($link);
?>



but this only calls the first row,
so i have two questions,

1. how to i call other rows? like username,

2. how do i loop through the data to show all the data in the table??


thanks again for your time!
-SiX

Kurashu
06-23-2005, 12:21 AM
while($row = mysql_fetch_assoc($result))
{
//echo whatever you want here
}

angst
06-23-2005, 12:26 AM
if I use that code, it goes into a never ending loop, and just shows the same data over and over again

and how to i show other data from other columns? for some reason it only shows data from the first one?

-SiX

Kid Charming
06-23-2005, 01:04 AM
There's something else wrong if mysql_fetch_assoc() is overlooping. Once your mysql_fetch_assoc() will return FALSE once it's gone through the whole result set, breaking your while loop. Without seeing the code, though, it's hard to say what's causing it.