182x Posted November 18, 2006 Share Posted November 18, 2006 Hey guys, the following code is to help me display a list of data from a database but for some reason the data is not being displayed. Any suggestions? Thanks[code]$sql = "SELECT * FROM acc";$query = mysql_query($sql); while ( $row = mysql_fetch_array($query)) { $data = "".$row["data"].""; echo "$data <br><br>"; } [/code] Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/ Share on other sites More sharing options...
trq Posted November 18, 2006 Share Posted November 18, 2006 Your query could be failing for all we know, you never check it before trying to use it. Try[code]<?php$sql = "SELECT * FROM acc";if ($result = mysql_query($sql)) { if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { echo $row['data']."<br><br>"; } } else { echo "no results found"; }} else { echo "query failed || ".mysql_error();}?>[/code] Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126506 Share on other sites More sharing options...
182x Posted November 18, 2006 Author Share Posted November 18, 2006 Thanks for the replay, using your code above doesn't display any error messages and on screen it leaves the space where the data should be displayed but no data is actually displayed on the screen.Any suggestions? Thanks. Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126642 Share on other sites More sharing options...
Adika Posted November 18, 2006 Share Posted November 18, 2006 Hi. Try this little change of your code:[code]$sql = "SELECT * FROM acc";$query = mysql_query($sql); while ( $row = mysql_fetch_array($query)) { $data = ".$row['data']."; echo $data." <br><br>"; }[/code]It should work now. Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126644 Share on other sites More sharing options...
182x Posted November 18, 2006 Author Share Posted November 18, 2006 When I use the above code I recieve the following error:Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126653 Share on other sites More sharing options...
Adika Posted November 18, 2006 Share Posted November 18, 2006 Sorry. Here is the correct code:[code]$sql = "SELECT * FROM acc";$query = mysql_query($sql); while ( $row = mysql_fetch_array($query)) { $data = $row['data']; echo $data." <br><br>"; }[/code] Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126657 Share on other sites More sharing options...
182x Posted November 18, 2006 Author Share Posted November 18, 2006 Thanks again for tour reply, I am still having the problem that the data isn't actually being displayed although there is space being left for it. I have checked the database and there should be 34 records being displayed.Any more ideas? and thanks again. Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126669 Share on other sites More sharing options...
Adika Posted November 18, 2006 Share Posted November 18, 2006 Well, I am out of ideas, but I will help you on this, if somebody other don't post some reply.Is your table name acc?Is your data which will show on the page is on the row named data? Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126675 Share on other sites More sharing options...
182x Posted November 18, 2006 Author Share Posted November 18, 2006 Hi sorry i have only been using PHP for 3 days. The table name is acc however im not sure what you mean by your second question. Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126682 Share on other sites More sharing options...
Adika Posted November 18, 2006 Share Posted November 18, 2006 Can you please copy the table creation query you use for creating the acc table, here?What I mean is, you have somewhere in your script a query which starts with "CREATE TABLE acc ("Now, copy this query here. It starts with "CREATE TABLE acc (" and ending with "; Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126685 Share on other sites More sharing options...
taith Posted November 18, 2006 Share Posted November 18, 2006 try and[code]print_r($row);[/code]nothings being displayed probably becausea) nothing is thereb) spelling mistake Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126688 Share on other sites More sharing options...
182x Posted November 18, 2006 Author Share Posted November 18, 2006 Here is the sql: (the table currently has over 30 records in it)[code]DROP TABLE IF EXISTS acc;CREATE TABLE acc ( traId int(19) NOT NULL auto_increment, tri varchar(29) NOT NULL default '', trao float(19) NOT NULL default '0', rId int(10) NOT NULL default '0', PRIMARY KEY (traId));[/code] Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126690 Share on other sites More sharing options...
Adika Posted November 18, 2006 Share Posted November 18, 2006 Well, there is a problem! ;DYou cannot do this:$data = $row['[color=red]data[/color]'];because, there is no [color=blue]data[/color] field in your database.You have: tri, trao and rId fields. Which row from this 3 holds the data you want to display? Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126696 Share on other sites More sharing options...
182x Posted November 18, 2006 Author Share Posted November 18, 2006 I would like to display all 3 of the rows. Does that cause problems? Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126704 Share on other sites More sharing options...
Adika Posted November 18, 2006 Share Posted November 18, 2006 Then you need to do this:[code]$sql = "SELECT * FROM acc";$query = mysql_query($sql); while ( $row = mysql_fetch_array($query)) { $data = $row['tri']; $data2 = $row['trao']; $data3 = $row['rId']; echo $data." <br><br>"; echo $data2." <br><br>"; echo $data3." <br><br>"; }[/code] Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126710 Share on other sites More sharing options...
182x Posted November 18, 2006 Author Share Posted November 18, 2006 That woks now thanks so much for your help :)I have one more question for formatting purposes if I were to put data like that into a table then records get inserted or removed from the database how would the table know ot resize?and thanks again!!! Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126714 Share on other sites More sharing options...
Adika Posted November 18, 2006 Share Posted November 18, 2006 When you put data into table, you use INSERT for inserting new data, which will resize the table. If you use UPDATE for inserting data, it will update the fields you set in the query and it will not resize the table. When updating the table, all the rows data will be deleted and a new data will be inserted. Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126722 Share on other sites More sharing options...
182x Posted November 18, 2006 Author Share Posted November 18, 2006 Sorry for the newbie questions but does that work with a regular HTML table so it will resize when data is inderted and deleted? Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126733 Share on other sites More sharing options...
taith Posted November 18, 2006 Share Posted November 18, 2006 yes Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126738 Share on other sites More sharing options...
Adika Posted November 18, 2006 Share Posted November 18, 2006 Yes.When you are creating the HTML table, you connect to the database and get out the results as you did in your script.Before the while loop, you create your table's header, the upper rows which hold the name's.<table border="1" width="658" height="246"> <tr> <td width="99" height="56">My data name1</td> <td width="79" height="56">My data name2</td> <td width="74" height="56">My data name3</td> </tr>Then you use the while loop, and in while loop you write the table codes:while($row=mysql_fetch_array($result) { echo "<tr><td>".$row['tri']."</td><td>".$row['trao']."</td><td>".$row['rId']."</td></tr>";}echo "</table>";After the while loop, you write the </table> which is the ending tag for table.When you have 4 data in your table, 4 row will be written on site. When you have 7 data in your table, 7 rows will be written on your site. So, the HTML table will allways expand or shrink, dependent on how many results you have. And he is doing that dynamically. :) Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126743 Share on other sites More sharing options...
182x Posted November 18, 2006 Author Share Posted November 18, 2006 Thats awesome thanks sooo much :)Will this technique work will older versions of PHP such as 3 and 4? Also can you guys recommend any good PHP books? Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126756 Share on other sites More sharing options...
Adika Posted November 18, 2006 Share Posted November 18, 2006 I don't know if it will work on older php version's, but I think that the most web host companies have installed php version > 4.1.0 and this script will work on versions > 4.1.0 .About php books: I don't know any good php book, because I learn php thrue internet, but somebody will maybe help you with that question. Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126763 Share on other sites More sharing options...
182x Posted November 18, 2006 Author Share Posted November 18, 2006 Thanks, its just I have to get this to run on a version of PHP that still uses $HTTP_SESSION_VARS rather than $_SESSION. Is that version 4.x or would that be a lower version? Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126767 Share on other sites More sharing options...
Adika Posted November 18, 2006 Share Posted November 18, 2006 That is the lower version. Php version 4.0.x is using $HTTP_SESSION_VARS too. Only versions from 4.1.0 is using $_SESSION. Link to comment https://forums.phpfreaks.com/topic/27658-display-code-problem/#findComment-126770 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.