Jump to content


Photo

Calling? (newb)


  • Please log in to reply
14 replies to this topic

#1 Nairb

Nairb
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationCanada

Posted 06 April 2003 - 12:11 AM

how do I call variables from a table?

#2 Nairb

Nairb
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationCanada

Posted 06 April 2003 - 01:22 AM

errr, with PHP....

i have a table called \"news\"
with \"user\" \"article\" and \"text\"
how would I call the news?

#3 Avalanche

Avalanche
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts

Posted 06 April 2003 - 03:12 AM

[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.
DEFINE("YOU","Spending Too Much Time On The Computer");

#4 Nairb

Nairb
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationCanada

Posted 06 April 2003 - 06:49 AM

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?

#5 DocSeuss

DocSeuss
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts
  • LocationTexas, USA

Posted 06 April 2003 - 07:37 AM

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

} 


"Unlimited budget? Great, for $5,000,0000 we will run your ad banner for one year"  ----Rasmus Lerdorf

#6 Nairb

Nairb
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationCanada

Posted 06 April 2003 - 08:02 AM

THat didnt work :(

#7 DocSeuss

DocSeuss
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts
  • LocationTexas, USA

Posted 06 April 2003 - 08:15 AM

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.
"Unlimited budget? Great, for $5,000,0000 we will run your ad banner for one year"  ----Rasmus Lerdorf

#8 Nairb

Nairb
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationCanada

Posted 06 April 2003 - 04:10 PM

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

#9 C4mpt3R?

C4mpt3R?
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 06 April 2003 - 04:18 PM

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>&nbsp;\".$row[\"user\"].\"</td>
<td>&nbsp;\".$row[\"article\"].\"&nbsp;</td>
<td>&nbsp;\".$row[\"text\"].\"&nbsp;</td></tr>
</table>\";
}
}

mysql_close($db);

?>


hope it will work like this too
yours C4mpt3R?
.NET --> sux the biggest [br]open source --> just rocks

#10 C4mpt3R?

C4mpt3R?
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 06 April 2003 - 04:20 PM

i have just forgotten to say that you have to set auto_increment to ID!!!
.NET --> sux the biggest [br]open source --> just rocks

#11 Nairb

Nairb
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationCanada

Posted 06 April 2003 - 05:05 PM

thanks :)

#12 Nairb

Nairb
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationCanada

Posted 06 April 2003 - 05:23 PM

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

#13 Nairb

Nairb
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationCanada

Posted 07 April 2003 - 12:35 AM

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

#14 Nairb

Nairb
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationCanada

Posted 07 April 2003 - 01:21 AM

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

#15 Avalanche

Avalanche
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts

Posted 08 April 2003 - 12:13 AM

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.
DEFINE("YOU","Spending Too Much Time On The Computer");




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users