...

View Full Version : Data from db to webpage using php



sircubesalot
04-24-2013, 08:49 PM
ok i have 2 problems first one is when i pull up the page with this coding on it it comes up as a blank page no errors just blank

2nd problem is i know this script wont make the name results alphabetical which is what i would like to do any input is greatly appreciated
thank you in advance to any help you can give


<html>
<head>
<title> Email List</head>
<style type="text/css">
table {
border: 2px solid red
background-color: #ffc;
}

th {
border-bottom: 5px solid#000;
}

td {
border bottom: 2px solid #666;
}
</style>
</head>
<body>

<h1> Email List </h1>

<?php
$dbcon = mysql_connect("localhost","dbs_dbs","password","dbs_forms") or die("Can not connect: " . mysql_error());
$sqlget = "SELECT * From playersemails"
$sqldata = mysql_query ($dbcon, $sqlget)or die("Can not connect: " . mysql_error());

echo "<table>";
echo "<tr><th>Players Name</th><th>Email address></th></tr>
while($row = mysql_fetch_array($sqldata, MYSQL_ASSOC)) {

echo "<tr><td>";
echo $row[`playersname`];
echo </td><td>
echo $row[`email`];
echo "</td></tr>;
}
echo "</table>";
?>
</body>
</html>

sircubesalot
04-24-2013, 09:14 PM
upon looking more nline i got this code working now the issue is i need the results to be alphabetical


<html>



<body>



<?php



$db = mysql_connect("localhost", "dbs_dbs","dbs4evr");



mysql_select_db("dbs_forms",$db);



$result = mysql_query("SELECT * FROM playersemails",$db);



echo "<table border=1>";



echo "<tr><td>Players Name</td><td>Email</tr>";



while ($myrow = mysql_fetch_row($result)) {



printf("<tr><td>%s</td><td>%s</td></tr>",

$myrow[1], $myrow[2], $myrow[3]);



}



echo "</table>";



?>



</body>





</html>

Old Pedant
04-24-2013, 09:18 PM
second problem is easy fix.



SELECT playersname, email FROM playersemails ORDER BY playersname


I *think* first problem is easy fix, too.
Look at the documentation:
http://www.php.net/manual/en/function.mysql-query.php

You have the order of your arguments to mysql_query backwards. The SQL is supposed to come first. And you can simply omit the connection.
SO:


$sqldata = mysql_query($sqlget)or die("query failed: " . mysql_error());

But do note that mysql_query is now obsolete, as that doc page tells you.

sircubesalot
04-24-2013, 09:30 PM
is there a particular place i should put
SELECT playersname, email FROM playersemails ORDER BY playersname in the 2nd code

Old Pedant
04-24-2013, 09:58 PM
Replace

$result = mysql_query("SELECT * FROM playersemails",$db);

with


$sql = "SELECT playersname, email FROM playersemails ORDER BY playersname";
$result = mysql_query( $sql,$db) or die( "query failed: " . mysql_error() );

You don't have to put the query into a separate PHP variable, but it's a really good idea to do so; it can make later debugging much easier. And you should ALWAYS add the or die just in case.

And you should always avoid using SELECT * unless you truly will need *ALL* the fields from the table. In this case, since you are only using two fields, you should definitely specify them in the SELECT. It can make a huge performance difference especially with big tables.

Fou-Lu
04-24-2013, 10:04 PM
That's simply a change to the query you had to specify an ORDER BY to make it alphabetical.
Your first problem was parser error:


$sqlget = "SELECT * From playersemails"

You're missing a semi-colon. No error would have been displayed since the error_reporting is either set low, or display_errors directive is disabled.

These are also errors:


echo $row[`email`];

PHP will attempt to execute a command line application called email (PHP is set by default to interpret accent grave as shortform system() piping).
There are a few more places where you are missing quotes and semi-colons as well. You'll need to correct all of these before it would pass the parser.
And that is why you're first one produced nothing but white output :)

Old Pedant
04-24-2013, 11:13 PM
Ahhh...I didn't catch those were ` instead of ' !!!

No wonder nothing worked!

Well, as I keep saying, I don't code in PHP. (But I would have mentioned the wrong quoting if I'd noticed it.)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum