Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie question: List from DB

Status
Not open for further replies.

pinkerton2002

Programmer
Jan 11, 2005
15
NO
This is my code:
<?php
$sql="SELECT * FROM gjest";
$query=mysql_query($sql);
while($datas=mysql_fetch_array('$query')){ ?>
Navn:<?php
echo $datas["navn"]."<br>"; ?>
Hjemmeside:<?php
echo $datas["hjemmeside"]."<br>"; ?>
Dato:<?php
echo $datas["tid"]."<br>"; ?>
Melding:<?php
echo $datas["melding"]."<br>";
}?>

My error is this:


Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
 
replace :

while($datas=mysql_fetch_array('$query'))

with:

while($datas=mysql_fetch_row('$query'))

 
Thank you, but I still got the same error. And what is the difference between using ' ' and " "?
 
You need to declare and fetch the array from the dbase

//this is the statement to get all the info
$sql = mysql_query("SELECT * FROM gjest");

// go through each row in the result set and display data from the table
while ($row = mysql_fetch_array($sql)) {

// give a name to the fields i.e. $row['name of column in dbase table']
$navn = $row['navn'];
$hjemmeside = $row['hjemmeside'];
$tid = $row['tid'];
$melding = $row['melding'];

//build formatted results
echo "$navn, $hjemmeside, $tid, $melding<br>";
}
hope this helps!!!

Reality is built on a foundation of dreams.
 
Actually, you should make it:
Code:
while($datas=mysql_fetch_row($query))
When you are referencing variables, you do not put them in quotes. You put strings in quotes. Also, looking at your code... you would be better of staying in php mode rather than switching back to html just to print one work. Why just not echo it all out?
 
Thank you, but no, It doesent help me.. I still get the same error-message:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
Now I've tried to echo them, but it still doesnt work...
 
<?php
include ("conn.php");
//this is the statement to get all the info
$sql = mysql_query("SELECT * FROM gjest");

// go through each row in the result set and display data from the table
while ($row = mysql_fetch_array($sql)) {

// give a name to the fields i.e. $row['name of column in dbase table']
$navn = $row['navn'];
$hjemmeside = $row['hjemmeside'];
$tid = $row['tid'];
$melding = $row['melding'];

//build formatted results
echo "$navn, $hjemmeside, $tid, $melding<br>";
}?>

This is now my code, but it still doesnt work
 
<?php
include ("conn.php");
//this is the statement to get all the info
$sql = mysql_query("SELECT * FROM gjest");

// go through each row in the result set and display data from the table
while ($row = mysql_fetch_array($sql)) {

// give a name to the fields i.e. $row['name of column in dbase table']
$navn = $row['navn'];
$hjemmeside = $row['hjemmeside'];
$tid = $row['tid'];
$melding = $row['melding'];

//build formatted results
echo "$navn, $hjemmeside, $tid, $melding<br>";
}?>

This is now my code(Thank you Overyde), but it still doesnt work
 
And the names i.e. $navn = $row['navn'] is the actual name of the column of the table?

Connection is definitely working?

weird...

Reality is built on a foundation of dreams.
 
The names of the fields in the DB (gjest) is:
id-integer autoincrement
navn Varchar(35)
hjemmeside Varchar(35)
tid Varchar(35)
melding (text)
The connection works on another page, so it should be working here as well

 
Is there a way to be absolutely positive 100% sure that the connection to the db is working here as well?
 
You don't know what's going on because you don't perform any error checking in your code. Add something like this to every mysql command:
Code:
$sql = mysql_query("SELECT * FROM gjest") or die('Error: ' . mysql_error());
 
Thank you, this helped me alot!! The connection was there, but I had forgotten to select the database..Thanx all.. That was really a nwbie-error!!
 
GREAT!!! Stars all round!!!

Reality is built on a foundation of dreams.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top