davef Posted January 31, 2011 Share Posted January 31, 2011 i'm trying to get a php database to output/display on a html webpage and have no results from cutting and pasting code from online tutorials i'm saving to a file with a htm extension i believe i have the connection data right: $db_host = 'server'; $db_user = 'username'; $db_pwd = 'password'; $database = 'database'; $table = 'table'; if (!mysql_connect($server,$username,$password)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); $result = mysql_query("SELECT * FROM table") or die(mysql_error()); Can someone please help me? send code? tell me what i'm doing wrong? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/ Share on other sites More sharing options...
kenrbnsn Posted January 31, 2011 Share Posted January 31, 2011 Save the file with an extension of ".php" Ken Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167601 Share on other sites More sharing options...
crmamx Posted January 31, 2011 Share Posted January 31, 2011 See the post: Help with select/where clause using. I spent hours and hours on the same problem and it turned out to be mostly a ; Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167614 Share on other sites More sharing options...
JD* Posted January 31, 2011 Share Posted January 31, 2011 Also, from the code above, you're executing a SQL statement, but not doing anything with the results. Try putting in a looping condition like this (After your current last line): for($i=0; $i < mysql_num_rows($result); $i++) { echo 'row '.$i.': '.mysql_result($result, $i, "FIELD_NAME").'<br />'; } Just replace the FIELD_NAME with the name of a field from your database Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167616 Share on other sites More sharing options...
litebearer Posted January 31, 2011 Share Posted January 31, 2011 just in case we have the urge to write some code for you, please show your db table structure Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167631 Share on other sites More sharing options...
davef Posted January 31, 2011 Author Share Posted January 31, 2011 database name- mx table name- contact1 5 fields in order: firstname1 lastname1 phonenumber1 email1 website1 Thanks! (just in case) Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167648 Share on other sites More sharing options...
davef Posted January 31, 2011 Author Share Posted January 31, 2011 the rest of the code i've been trying (the latest) is $num=mysql_numrows($result); $i=0; while ($i < $num) { $firstname1=mysql_result($result,$i,"firstname1"); $lastname1=mysql_result($result,$i,"lastname1"); $phonenumber1=mysql_result($result,$i,"phonenumber1"); $email1=mysql_result($result,$i,"email1"); $website1=mysql_result($result,$i,"website1"); echo "<b>$firstname1$lastname1</b><br>Phone: $phonenumber1<br>E-mail: $email1<br>Web: $website1<br><hr><br>"; $i++; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167651 Share on other sites More sharing options...
davef Posted January 31, 2011 Author Share Posted January 31, 2011 when i save it as .php i get Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /hermes/web04/b1964/david/display.php on line 61 yes, i also have spent hourrrrrrsssss and i have the maddening feeling that it is something like a ; Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167654 Share on other sites More sharing options...
litebearer Posted January 31, 2011 Share Posted January 31, 2011 try... <?PHP /* you might consider putting the connect code in a separate file then just include it */ /* that way you can easily reuse the code in other scripts */ $db_host = 'server'; $db_user = 'username'; $db_pwd = 'password'; $database = 'mx'; if (!mysql_connect($server,$username,$password)){ die("Can't connect to database"); } if (!mysql_select_db($database)){ die("Can't select database"); } $result = mysql_query("SELECT * FROM contact1") or die(mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows>0) { while($row=mysql_fetch_array($result)) { echo "First name: " . $row['firstname1'] . "<br>"; echo "Last name: " . $row['lastname1'] . "<br>"; echo "Phone: " . $row['phonenumber1'] . "<br>"; echo "Email: " . $row['email1'] . "<br>"; echo "Website: " . $row['website1'] . "<br>"; echo "<hr>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167657 Share on other sites More sharing options...
davef Posted January 31, 2011 Author Share Posted January 31, 2011 litebearer saving that code as .php I get: Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /hermes/web04/b1964/violet/display2.php on line 11 here's the code with the server, user name , password xxxxxxxxxed out <?php $db_host = 'violet.bizlandmysql.com'; $db_user = 'dravef'; $db_pwd = '10801080'; $database = 'mx'; $table = 'contact1'; if (!mysql_connect($violet.bizlandmysql.com,$dravef,$10801080)) die("Can't connect to database"); if (!mysql_select_db($mx)) die("Can't select database"); <?PHP $db_host = 'xxx'; $db_user = 'xxxx'; $db_pwd = 'xxxxx'; $database = 'mx'; if (!mysql_connect($xxxx,$xx,$xxx)) die("Can't connect to database"); } if (!mysql_select_db($mx)){ die("Can't select database"); } $result = mysql_query("SELECT * FROM contact1") or die(mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows>0) { while($row=mysql_fetch_array($result)) { echo "First name: " . $row['firstname1'] . "<br>"; echo "Last name: " . $row['lastname1'] . "<br>"; echo "Phone: " . $row['phonenumber1'] . "<br>"; echo "Email: " . $row['email1'] . "<br>"; echo "Website: " . $row['website1'] . "<br>"; echo "<hr>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167662 Share on other sites More sharing options...
trq Posted January 31, 2011 Share Posted January 31, 2011 Please use tags when posting code. Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167664 Share on other sites More sharing options...
litebearer Posted January 31, 2011 Share Posted January 31, 2011 Try this. Save it as test000.php then 'run' it <?PHP $db_host = 'xxx'; $db_user = 'xxxx'; $db_pwd = 'xxxxx'; $database = 'mx'; mysql_connect($db_host,$db_user,$db_pwd) or die("Can't connect to database"); mysql_select_db($database) or die("Can't select database"); $result = mysql_query("SELECT * FROM contact1") or die(mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows>0) { while($row=mysql_fetch_array($result)) { echo "First name: " . $row['firstname1'] . "<br>"; echo "Last name: " . $row['lastname1'] . "<br>"; echo "Phone: " . $row['phonenumber1'] . "<br>"; echo "Email: " . $row['email1'] . "<br>"; echo "Website: " . $row['website1'] . "<br>"; echo "<hr>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167674 Share on other sites More sharing options...
davef Posted January 31, 2011 Author Share Posted January 31, 2011 Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /hermes/web04/b1964/violet/test000.php on line 6 <?PHP $db_host = 'xxxx'; $db_user = 'xxx'; $db_pwd = 'xxx'; $database = 'mx'; mysql_connect($xxxxx,$xxx,$xxxxxx) or die("Can't connect to database"); mysql_select_db($mx) or die("Can't select database"); $result = mysql_query("SELECT * FROM contact1") or die(mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows>0) { while($row=mysql_fetch_array($result)) { echo "First name: " . $row['firstname1'] . "<br>"; echo "Last name: " . $row['lastname1'] . "<br>"; echo "Phone: " . $row['phonenumber1'] . "<br>"; echo "Email: " . $row['email1'] . "<br>"; echo "Website: " . $row['website1'] . "<br>"; echo "<hr>"; } } ?> thanx Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167680 Share on other sites More sharing options...
suma237 Posted January 31, 2011 Share Posted January 31, 2011 $db_host = 'xxxx'; $db_user = 'xxx'; $db_pwd = 'xxx'; $database = 'mx'; mysql_connect($xxxxx,$xxx,$xxxxxx) or die("Can't connect to database"); //change the below code as mysql_connect($db_host,$db_user,$db_pwd) or die("Can't connect to database"); Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167700 Share on other sites More sharing options...
davef Posted January 31, 2011 Author Share Posted January 31, 2011 thanks when i insert db_ before the host ,username and password, db_xxxxxx below, (that's what you recommended i think) i get: Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'hostcom' (1) in /hermes/web04/b1964/david/suma.php on line 6 Can't connect to database Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167702 Share on other sites More sharing options...
suma237 Posted January 31, 2011 Share Posted January 31, 2011 check the host name,it's mysql connectivity error Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167704 Share on other sites More sharing options...
davef Posted January 31, 2011 Author Share Posted January 31, 2011 yes, but there wasn't a problem connecting with the host before i put the db_ in front of the host address Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167705 Share on other sites More sharing options...
litebearer Posted January 31, 2011 Share Posted January 31, 2011 Just to be sure we are clear - in reality you are putting the actual values (ie user name, password database name) in the variables rather than xxx's this $db_user = "nick" NOT $db_user = "xxx" Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167707 Share on other sites More sharing options...
kenrbnsn Posted January 31, 2011 Share Posted January 31, 2011 Please post the code you have now between tags. Ken Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1167820 Share on other sites More sharing options...
davef Posted January 31, 2011 Author Share Posted January 31, 2011 Right, here's the code in its entirety, with just a bit of scrambling on the host, username, etc I was putting in the actual info and taking out the xxxxx. THANKS for your efforts. I don't feel so bad about my difficulties. Why is it so hard? Php is a lot more complex than html. Is there a good way to learn it? book? [code=php:0] <?PHP $db_host = 'david.hostsmysql.com'; $db_user = 'username'; $db_pwd = 'ddddddddd'; $database = 'mx'; mysql_connect($db_david.hostsmysql.com,$db_username,$db_username) or die("Can't connect to database"); mysql_select_db($mx) or die("Can't select database"); $result = mysql_query("SELECT * FROM contact1") or die(mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows>0) { while($row=mysql_fetch_array($result)) { echo "First name: " . $row['firstname1'] . "<br>"; echo "Last name: " . $row['lastname1'] . "<br>"; echo "Phone: " . $row['phonenumber1'] . "<br>"; echo "Email: " . $row['email1'] . "<br>"; echo "Website: " . $row['website1'] . "<br>"; echo "<hr>"; } } ?> [code] and here is the error message Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'hostsmysqlcom' (1) in /hermes/web04/b1964/david/suma.php on line 6 Can't connect to database[/code] Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1168095 Share on other sites More sharing options...
davef Posted January 31, 2011 Author Share Posted January 31, 2011 here's another version with its error message and the error message taken from the screen when i "run"/view the file.I am sure that i am viewing the same file coded here. [code=php:0] <?PHP $db_host = 'david.hostsmysql.com'; $db_user = 'username'; $db_pwd = 'dddddddd'; $database = 'mx'; mysql_connect($david.hostsmysql.com,$usern,$dddddd) or die("Can't connect to database"); mysql_select_db($mx) or die("Can't select database"); $result = mysql_query("SELECT * FROM contact1") or die(mysql_error()); $num_rows = mysql_num_rows($result); if($num_rows>0) { while($row=mysql_fetch_array($result)) { echo "First name: " . $row['firstname1'] . "<br>"; echo "Last name: " . $row['lastname1'] . "<br>"; echo "Phone: " . $row['phonenumber1'] . "<br>"; echo "Email: " . $row['email1'] . "<br>"; echo "Website: " . $row['website1'] . "<br>"; echo "<hr>"; } } ?> [/code] Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /hermes/web04/b1964/david/test000.php on line 6 Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1168100 Share on other sites More sharing options...
Xdega Posted February 1, 2011 Share Posted February 1, 2011 Well one thing I instantly see on line 6 is that you have: $dddddd That is an undefined variable. I think what you mean to use is: $db_pwd Edit: In fact your entire "mysql_connect" is wrong. Instead of $david.hostsmysql.com, $usern..... You should be using: mysql_connect($db_host,$db_user,$db_pwd) or die("Can't connect to database"); I think you are blindly copy/pasting code before understanding how it functions. May I recommend: http://www.w3schools.com/php/default.asp for a good guide on the very basics of PHP. In specific, how string variables work: http://www.w3schools.com/php/php_variables.asp Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1168120 Share on other sites More sharing options...
davef Posted February 1, 2011 Author Share Posted February 1, 2011 Thanks. I am putting in the actual server, username and password in the code, just not on what I post here. And after putting quotes around the database name the dot / . in it is now recognized properly, yielding the same error message in both attempts at coding. Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_VARIABLE or '$' in /hermes/web04/b1964/david/test000.php on line 6 Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_VARIABLE or '$' in /hermes/web04/b1964/david/suma.php on line 6 Although these coding attempts were provided by kind folks on these forums, you're right, I was cutting and pasting (it was an effective way to learn html), but only after failing to get the tutorials-- I did go through the one you suggested-- to understand them or to get their examples to work. Still, with what I've picked up in the forums, I will go back to the tutorials and/or get a book, especially since I can bring questions from them to here. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1168153 Share on other sites More sharing options...
trq Posted February 1, 2011 Share Posted February 1, 2011 One of these variables exist. $david.hostsmysql.com, $usern, $dddddd You also cannot have . in a variable name as it is the concatenation operator. What this is trying to do is join the undefined variable $david to the undefined constants hostsmysql and com. Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1168163 Share on other sites More sharing options...
davef Posted February 1, 2011 Author Share Posted February 1, 2011 well, the quotes did help with that. is the following "line 6"? mysql_select_db($mx) or die("Can't select database"); Quote Link to comment https://forums.phpfreaks.com/topic/226181-output-php-database-to-html-webpage/#findComment-1168168 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.