Nairb Posted April 6, 2003 Share Posted April 6, 2003 how do I call variables from a table? Quote Link to comment Share on other sites More sharing options...
Nairb Posted April 6, 2003 Author Share Posted April 6, 2003 errr, with PHP.... i have a table called \"news\" with \"user\" \"article\" and \"text\" how would I call the news? Quote Link to comment Share on other sites More sharing options...
Avalanche Posted April 6, 2003 Share Posted April 6, 2003 [php:1:585fc11ea9]<?php $conn=mysql_connect (\"localhost\", \"DBUSERNAME\", \"DBPASSWORD\") or die (\'I cannot connect to the database because: \' . mysql_error()); mysql_select_db (\"DBNAME\",$conn); $result = mysql_query(\"SELECT * FROM news\",$conn); printf(\" News: %s<hr>n\", mysql_result($result,\'\',\"article\")); printf(\" %s<hr>n\", mysql_result($result,\'\',\"text\")); printf(\" Posted By: %sn\", mysql_result($result,\'\',\"user\")); mysql_close($conn); ?>[/php:1:585fc11ea9] First thing you do is connect (the $conn=mysql_connect etcetera line). Then, you have to open the database (the mysql_select_db etcetera line). Then, you have to open the table (the $result = mysql_query etcetera line). Then, you have to print it out (the printf etcetera line). I believe that the %s is where it will display the data in the table (the name of the data you want to display is the last variable in the line, where as for the first one I put article). Should work, and someone like Kriek or Shiv(restofnamehere) could explain it better. Quote Link to comment Share on other sites More sharing options...
Nairb Posted April 6, 2003 Author Share Posted April 6, 2003 OMG THANK YOU SOOOOOOOOOOOOOOO MUCH I\'ve been working on this (self taught) for a month... couldnt get it... Thanks! EDIT: how would I get it to repeat, to display all of the news articles? Quote Link to comment Share on other sites More sharing options...
DocSeuss Posted April 6, 2003 Share Posted April 6, 2003 I personaly would do it like this while($result = mysql_query("SELECT * FROM news",$conn)) { echo "News: $result[\'article\']<br>"; echo "$result[\'text\']<br>"; echo "Posted By: $result[\'user\']<br><br>"; } Quote Link to comment Share on other sites More sharing options...
Nairb Posted April 6, 2003 Author Share Posted April 6, 2003 THat didnt work Quote Link to comment Share on other sites More sharing options...
DocSeuss Posted April 6, 2003 Share Posted April 6, 2003 let me try that again $result = mysql_query(\"SELECT * FROM news\", $conn)) while($row = mysql_fetch_array($result)) { echo \"News: $row[\'article\']<br>\"; echo \"$row[\'text\']<br>\"; echo \"Posted By: $row[\'user\']<br><br>\"; } my mistake should proof read before I post. Quote Link to comment Share on other sites More sharing options...
Nairb Posted April 6, 2003 Author Share Posted April 6, 2003 I get the error: Parse error: parse error, unexpected T_WHILE in /usr/local/psa/home/vhosts/gameplay4u.com/httpdocs/nairb/gp4u/admin/userlist.php on line 8 Quote Link to comment Share on other sites More sharing options...
C4mpt3R? Posted April 6, 2003 Share Posted April 6, 2003 you also could do it like this: for this you have to add a row called \"ID\" --> primary key <?php $db=mysql_connect(\'localhost\',\'DBuser\',\'DBpassword\' ); $db or die(\'DataBase Error\'); mysql_select_db(\'DataBase\',$db) or die(\'DataBase Error\'); $sql=\"SELECT * FROM news ORDER BY ID\"; $result=mysql_query($sql,$db); if ($result) { while($row = mysql_fetch_array($result)) { echo \" <table border=\'0\'><tr> <td> \".$row[\"user\"].\"</td> <td> \".$row[\"article\"].\" </td> <td> \".$row[\"text\"].\" </td></tr> </table>\"; } } mysql_close($db); ?> hope it will work like this too yours C4mpt3R? Quote Link to comment Share on other sites More sharing options...
C4mpt3R? Posted April 6, 2003 Share Posted April 6, 2003 i have just forgotten to say that you have to set auto_increment to ID!!! Quote Link to comment Share on other sites More sharing options...
Nairb Posted April 6, 2003 Author Share Posted April 6, 2003 thanks Quote Link to comment Share on other sites More sharing options...
Nairb Posted April 6, 2003 Author Share Posted April 6, 2003 ok, now for submitting news into the database? for submitnews.php: <? $conn=mysql_connect ("localhost", "nairb", "mypw") or die (\'I cannot connect to the database because: \' . mysql_error()); mysql_select_db ("nairb",$conn); $sql = "SELECT * FROM news"; $result=mysql_query($sql,$conn); ?> <BODY BGCOLOR=#8FB2D1 LEFTMARGIN=0 TOPMARGIN=5 MARGINWIDTH=0 MARGINHEIGHT=0> <TABLE WIDTH=600 BORDER=0 CELLPADDING=4 CELLSPACING=1 align="center" bgcolor="#000000"> <TR> <TD bgcolor="#B9CFE2"> <TABLE BORDER=0 CELLPADDING=4 CELLSPACING=0 align="center" bgcolor="#000000" width="100%"> <TR align="top"> <TD bgcolor="#B9CFE2" align="top"> <font color="#5880A4" size=2 face="arial"> <form action="addnews.php"> <input type="hidden" name="article" value="<?php $result[article]; ?>"> Username: </td><TD bgcolor="#B9CFE2" align="top"><input type="text" name="user"><br> </td></tr> <TR align="top"> <TD bgcolor="#B9CFE2" align="top"> <font color="#5880A4" size=2 face="arial"> News Id: </td><TD bgcolor="#B9CFE2" align="top"><?php echo "<b>".$result["article"]."</b>"; ?><br> </td></tr> <TR align="top"> <TD bgcolor="#B9CFE2" align="top"> <font color="#5880A4" size=2 face="arial"> News Text: </td><TD bgcolor="#B9CFE2" align="top"><textarea cols="40" rows="5" name="text">Insert the news here.</textarea><br> <input type="submit" value="submit"> </form> and for addnews.php: <BODY BGCOLOR=#8FB2D1 LEFTMARGIN=0 TOPMARGIN=5 MARGINWIDTH=0 MARGINHEIGHT=0> <TABLE WIDTH=600 BORDER=0 CELLPADDING=4 CELLSPACING=1 align="center" bgcolor="#000000"> <TR> <TD bgcolor="#B9CFE2"> <font color="#5880A4" size=2 face="arial"> <?php $conn=mysql_connect ("localhost", "nairb", "mypw") or die (\'I cannot connect to the database because: \' . mysql_error()); mysql_select_db ("nairb",$conn); $result = mysql_query("INSERT INTO `news` (`user`, `article`, `text`) VALUES (\'$user\', \'$article\', \'$text\');",$conn); mysql_close($conn); ?> it works good(adding the news), but i want the article id to increase everytime...? Quote Link to comment Share on other sites More sharing options...
Nairb Posted April 7, 2003 Author Share Posted April 7, 2003 I got that working What I\'m trying to do now, is set up a user database... I have the tables and everything. I\'m trying to make a userlist.php that displays all the users I have: <?php $conn=mysql_connect ("localhost", "nairb", "mypw") or die (\'I cannot connect to the database because: \' . mysql_error()); mysql_select_db ("nairb",$conn); $sql="SELECT * FROM users"; $result=mysql_query($sql,$conn); if ($result) { while($row = mysql_fetch_array($result)) { echo " <table border=\'0\'><tr> <td>Username: <b>".$row["username"]."</b></td> </tr> <tr> <td>Email: <b>".$row["email"]."</b></td> </tr> <tr> <td>Age: <b>".$row["age"]."</b></td> </tr> <tr> <td>Gender: <b>$gender</b></td> </tr></table><br>"; } } mysql_close($conn); ?> How do I get it to show the gender? I have it so when you register, it inputs 1 into the database for male, and a 2 for female... Thanks EDIT: I figured it out, thanks anyways Quote Link to comment Share on other sites More sharing options...
Nairb Posted April 7, 2003 Author Share Posted April 7, 2003 Lol, sorry again... Ok, now I\'m trying to make it so that the users cant use a name already in use... here\'s what I have: $checkusr = mysql_query("SELECT FROM `user` WHERE username=`$HTTP_POST_VARS[\'username\']); $checkusrres = mysql_result($checkusr); if (isset($checkusrres) { echo "The username $HTTP_POST_VARS[\'username\'] is already taken."; exit; //terminate exec. } else { echo "Registration sucessful, $HTTP_POST_VARS[\'username\']!"; } Damn programming.. the logic is easy, the scripting is hard :\'( Any help is welcome, please Quote Link to comment Share on other sites More sharing options...
Avalanche Posted April 8, 2003 Share Posted April 8, 2003 If you are going to make a membership tutorial you should try the Creating a Membership System Tutorial. That has it so that you can\'t have the same username or email, but you can edit it to whatever you want. That is, in my opinion one of the best tutorials on the site, but I haven\'t really tried many others. Quote Link to comment 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.