Jump to content

Recommended Posts

[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.

Link to comment
https://forums.phpfreaks.com/topic/307-calling-newb/#findComment-977
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/307-calling-newb/#findComment-985
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/307-calling-newb/#findComment-993
Share on other sites

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...?

Link to comment
https://forums.phpfreaks.com/topic/307-calling-newb/#findComment-996
Share on other sites

I got that working :D

 

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

Link to comment
https://forums.phpfreaks.com/topic/307-calling-newb/#findComment-1014
Share on other sites

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 :)

Link to comment
https://forums.phpfreaks.com/topic/307-calling-newb/#findComment-1016
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/307-calling-newb/#findComment-1038
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.