ckerr27 Posted April 20, 2012 Share Posted April 20, 2012 Hi I am trying to display data from the table "event" in my database, I use the code below but it will not work and I cannot figure out why. CAn anyone help? CODE: <?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="event"; // Table name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $result = my_sql_query("SELECT * FROM event WHERE eventid = '1'"); while($row = mysql_fetch_array($result)) { $eventname= $row["eventname"]; $eventdate= $row["eventdate"]; echo "<b><u>Event Name:</b></u> $eventname" echo "<b><u>Event Date:</b></u> $eventdate<br>"; } ?> DISPLAY: Event Name: $eventname echo "Event Date: $eventdate "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/261344-display-data-from-database-confusedhelp-please/ Share on other sites More sharing options...
DavidAM Posted April 20, 2012 Share Posted April 20, 2012 The only error I see in the code as posted, is a missing semi-colon on the first echo. However, the example display you showed indicates that you used a single-quote to open the first echo, and did not close that string. (by the way, please use [ code] tags or [ php] tags when posting code). The example display looks like your code actually is: echo '<b><u>Event Name:</b></u> $eventname echo "<b><u>Event Date:</b></u> $eventdate<br>"; } ?>' Note that PHP will not interpret variables inside of single-quoted strings. That block should be: echo "<b><u>Event Name:</b></u> $eventname"; echo "<b><u>Event Date:</b></u> $eventdate<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/261344-display-data-from-database-confusedhelp-please/#findComment-1339224 Share on other sites More sharing options...
ckerr27 Posted April 20, 2012 Author Share Posted April 20, 2012 Thanks but sorry its still not working, the code looks correct to me I cannot work out why it is not displaying the info, the database and table names are all correct. Quote Link to comment https://forums.phpfreaks.com/topic/261344-display-data-from-database-confusedhelp-please/#findComment-1339225 Share on other sites More sharing options...
DavidAM Posted April 20, 2012 Share Posted April 20, 2012 Well, we'd like to help more, but I don't know what you are saying. the code looks correct to me Did you change the code as I suggested? Or was it already that way? Is that your actual code, because the code you showed should produce a syntax error, not the output you showed. Can you cut and paste your code, exactly as is, using the code tags (it's the button with the hash-mark -- octothorpe? -- just above the smileys with the blinking eyes)? Can you also cut and paste the HTML (from "View Source") of the browser (again, using code tags) so we can see what is happening? Quote Link to comment https://forums.phpfreaks.com/topic/261344-display-data-from-database-confusedhelp-please/#findComment-1339230 Share on other sites More sharing options...
ckerr27 Posted April 21, 2012 Author Share Posted April 21, 2012 I hop i have used the correct code tags! This is the complete file: #<html> #<head> #<link rel="stylesheet" type ="text/css" href="anything2.css" #</head> #<body> #<div id="container"> #<div id="header"><img src="imagesb.jpg" alt="Cool Image" align="left"> #<img src="images.jpg" alt="Cool Image" align="right"><center><b><font size="6.5"><br><br>Future Events</b></center></font> #</div> #<div id="leftnav"><center> #<br><br> #<input type= "button" style="width:120px;" value="Home" onClick="window.location= 'adminhome.php' "> #<br><br> #<input type= "button" style="width:120px;" value="Personal Details" onClick="window.location= 'adpersonaldetails.php' "> #<br><br> #<input type= "button" style="width:120px;" value="Club Details" onClick="window.location= 'adclubdetails.php' "> #<br><br> #<input type= "button" style="width:120px;" value="News" onClick="window.location= 'adnews.php' "> #<br><br> #<input type= "button" style="width:120px;" value="FAQ" onClick="window.location= 'adfaq.php' "> #<br><br> #<input type= "button" style="width:120px;" value="Wall" onClick="window.location= 'adwall.php' "> #<br><br> #<input type= "button" style="width:120px;" value="About Us" onClick="window.location= 'adabout.php' "> #</div> #<div id="body"> #<?php #$host="localhost"; // Host name #$username="root"; // Mysql username #$password=""; // Mysql password #$db_name="test"; // Database name #$tbl_name="event"; // Table name #mysql_connect("$host", "$username", "$password")or die("cannot connect"); #mysql_select_db("$db_name")or die("cannot select DB"); #$result = my_sql_query("SELECT * FROM event WHERE eventid = '1'"); #while($row = mysql_fetch_array($result)) { # $eventname= $row["eventname"]; # $eventdate= $row["eventdate"]; # # echo "<b><u>Event Name:</b></u> $eventname"; # echo "<b><u>Event Date:</b></u> $eventdate<br>"; # } #?> #<div id="footer">This is the footer</div> #</body> #</html> Quote Link to comment https://forums.phpfreaks.com/topic/261344-display-data-from-database-confusedhelp-please/#findComment-1339307 Share on other sites More sharing options...
DavidAM Posted April 21, 2012 Share Posted April 21, 2012 "Code tags" in a post are like HTML markup, but with square-brackets. So for bold instead of <B></B> (as you would do in HTML), you type [ b]something[ /b] (I had to put a space after the open brackets there so it would display, take the space out) which comes out like something. For code, you use code tags: [ code]code here[ /code] (again, remove the spaces). I mentioned the "#" because, above the edit box are some buttons for formatting posts; the "#" is the picture on the button that puts code tags into the edit area. Also, there is a Preview button below the edit area (next to the Post button) that will let you see your post before you send it, so you can see if the markup is correct. Anyway, on to the problem I see some HTML markup problems in that code. I'm not sure if these could be causing you problem or not. But you should fix these problems and then see what happens: <head> <link rel="stylesheet" type ="text/css" href="anything2.css" </head> That LINK tag is not closed, so the closing HEAD tag is ignored and it's closing bracket closes the LINK. <div id="container"> ... <div id="body"> There are no closing tags for these two DIVs Other than the fact that the HEAD tag is left open, I don't think these could be causing the problem. Did you look at the HTML source in your browser? That may give us a hint as to what is happening. Quote Link to comment https://forums.phpfreaks.com/topic/261344-display-data-from-database-confusedhelp-please/#findComment-1339345 Share on other sites More sharing options...
Barand Posted April 21, 2012 Share Posted April 21, 2012 Where is "my_sql_query()" defined? Quote Link to comment https://forums.phpfreaks.com/topic/261344-display-data-from-database-confusedhelp-please/#findComment-1339346 Share on other sites More sharing options...
Drummin Posted April 21, 2012 Share Posted April 21, 2012 Never seen my_sql_query. A far as I know it should be mysql_query unless this is yet another coding style. Quote Link to comment https://forums.phpfreaks.com/topic/261344-display-data-from-database-confusedhelp-please/#findComment-1339348 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.