Jump to content

output php database to html webpage


davef

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

 

 

Link to comment
Share on other sites

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 ;

Link to comment
Share on other sites

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>";
}
}
?>


Link to comment
Share on other sites

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>";

 

 

 

}

}

?>

 

Link to comment
Share on other sites

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>";
}
}
?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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");

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.