Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
Another PHP/MySQL Problem
Old 02-24-2004, 12:21 PM Another PHP/MySQL Problem
Wenslock's Avatar
Webmaster Talker

Posts: 746
Location: Lincoln, UK
Trades: 0
Ok. This time my problem's to do with inputting data into the database.

I was wondering how I would go about doing that. I'm using a form that's ready for the database, but as of yet there's no database link.

What would the PHP/MySQL scripting be? It's making me go a little crazy!!!
Wenslock is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-24-2004, 02:17 PM
Lippy's Avatar
LipWeb.Net

Posts: 270
Trades: 0
Database connection 101 in PHP:

PHP Code:

<? 
//  Config file to connect database

//  connection to database
function make_seed() 
{
    list(
$usec$sec) = explode(' 'microtime());
    return (float) 
$sec + ((float) $usec 100000);
}
srand(make_seed());

$dbcnx = @mysql_connect("localhost""User_name""Pass_word");
  if (!
$dbcnx)
  {
    echo( 
"<p>Unable to connect to database server, please retry.  If error contunies to occur please email the 
    webmaster</p>"
);
    
    exit();
  }
?>
Thats to connect to the database server.
Now to connect to a specfic database.

PHP Code:
<?
      
if (! @mysql_select_db("Database_name") )
      {
        echo(
"<p>Error connecting to database please try again.  If error continues to occur please contact the webmaster.</p>");
        exit();
      }
?>
Now to run a select query on that table
PHP Code:
<?

$result 
mysql_query("SELECT rowname1, rowname2 FROM table_name WHERE rowname='value'");

if (!
$result)
{
        echo(
"<p>Error connecting to database please try again.  If error continues to occur please contact the webmaster.</p>");
        exit();
}

?>
Feel free to ask questions.
__________________

Please login or register to view this content. Registration is FREE
"Less Lip More Service"
Providing Quality Hosting at Fair Prices
AIM: LipWebNet MSN: daniel[at]lipweb.net Yahoo: LipWebNet ICQ: 265771633
Lippy is offline
Reply With Quote
View Public Profile Visit Lippy's homepage!
 
Old 02-25-2004, 06:24 AM
Wenslock's Avatar
Webmaster Talker

Posts: 746
Location: Lincoln, UK
Trades: 0
OK. here's what I was able to scratch up.

<?
$username="usr_name";
$password="pword";
$database="d_base";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM table";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$date=mysql_result($result,$i,"date");
$shortinfo=mysql_result($result,$i,"shortinfo");
$maininfo=mysql_result($result,$i,"maininfo");
$link=mysql_result($result,$i,"link");


echo "<TD><TABLE border=1 bordercolor=FFFFFF cellpadding=0 cellspacing=0 width=100%><TR><TD class=greytitles width=33%>
<b>$date</TD></TR><TR><TD colspan=2 class=whitetitles>$shortinfo<br>$maininfo<br>$link </TR></TD></TABLE></TD>";

++$i;
}

?>

I just need this one to show the last three records. (ahh! @ News page!) The importing to the database is my real trouble at the moment. Any ideas Lippy?
__________________
------------------------------------------------------------

Please login or register to view this content. Registration is FREE
- Black Prism professional web design services
------------------------------------------------------------
Wenslock is offline
Reply With Quote
View Public Profile
 
Old 02-25-2004, 09:43 AM
Wenslock's Avatar
Webmaster Talker

Posts: 746
Location: Lincoln, UK
Trades: 0
OK. database sorted. One more TINY TINY thing. ;-) when it comes to inputting data from a HTML form, how would I go about inserting the information onto the database when it has more than one column? (ie News, date, extra_information)

I know I can use :

mysql_query("INSERT INTO news (title) values ('$title');"
); for example, but that would only input one variable from the form.
__________________
------------------------------------------------------------

Please login or register to view this content. Registration is FREE
- Black Prism professional web design services
------------------------------------------------------------
Wenslock is offline
Reply With Quote
View Public Profile
 
Old 02-25-2004, 10:03 AM
Lippy's Avatar
LipWeb.Net

Posts: 270
Trades: 0
you are making me look into old code.

PHP Code:
<?

$newrecord 
mysql_query("INSERT into tbl_name SET field1='$value1', field2='$value2', field3='value3'");

if (!
$newrecord)
{
     echo(
"Unable to locate query at this time");
     exit();
}

?>
Hope this helps.
__________________

Please login or register to view this content. Registration is FREE
"Less Lip More Service"
Providing Quality Hosting at Fair Prices
AIM: LipWebNet MSN: daniel[at]lipweb.net Yahoo: LipWebNet ICQ: 265771633
Lippy is offline
Reply With Quote
View Public Profile Visit Lippy's homepage!
 
Old 02-25-2004, 10:07 AM
Wenslock's Avatar
Webmaster Talker

Posts: 746
Location: Lincoln, UK
Trades: 0
Sorry Lippy for making you look It's me being old skool again

S'ok now! I've cracked it I think Thanks for your time and effort
__________________
------------------------------------------------------------

Please login or register to view this content. Registration is FREE
- Black Prism professional web design services
------------------------------------------------------------
Wenslock is offline
Reply With Quote
View Public Profile
 
Old 02-25-2004, 10:18 AM
Lippy's Avatar
LipWeb.Net

Posts: 270
Trades: 0
A pleasure, was purely joking about having to look in old code, love looking back at my past work and being able to say "Why the heck did I do it that way when its so much better doing it this way!"
__________________

Please login or register to view this content. Registration is FREE
"Less Lip More Service"
Providing Quality Hosting at Fair Prices
AIM: LipWebNet MSN: daniel[at]lipweb.net Yahoo: LipWebNet ICQ: 265771633
Lippy is offline
Reply With Quote
View Public Profile Visit Lippy's homepage!
 
Reply     « Reply to Another PHP/MySQL Problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.83050 seconds with 12 queries