this is some code from a polling program I've written
(which needs beta testers, any takers?)
I put this is in an include file an use it everywhere (called connection.php)
<? function db_connect () {
## In the line below, insert: ("host", "user", "password"); ## $link = mysql_pconnect ("localhost", "root", "password");
## In the line below, insert ("your_database_name") ## if ($link && mysql_select_db ("poll")) { return ($link); } else { return (FALSE); } } ?>
'
then in all your php scripts that connect to that DB, use this
<? include "connection.php"; db_connect() or die("Cannot connect to database");
# Let's get the settings $query = "SELECT * FROM pollprefs";
#echo "<!--Query:".$query."-->\n";
$result = mysql_query ($query); if (!$result) die ("Query Error:\n<BR>\n\"$query\"");
$prefs = mysql_fetch_array($result);?> <!-- some misc code here --> <option value="default" <? if ($prefs["Ordering"] == "default") echo "SELECTED"; ?>>Default</option> <option value="votesasc" <? if ($prefs["Ordering"] == "votesasc") echo "SELECTED"; ?>>Votes [Ascending]</option> <option value="votesdesc" <? if ($prefs["Ordering"] == "votesdesc") echo "SELECTED"; ?>>Votes [Descending]</option> <option value="alphaasc" <? if ($prefs["Ordering"] == "alphaasc") echo "SELECTED"; ?>>Alphabetical [Ascending]</option> <option value="alphadesc" <? if ($prefs["Ordering"] == "alphadesc") echo "SELECTED"; ?>>Alphabetical [Descending]
'
What will you be using this for?
will you be retrieving single rows, ot multiple rows? if you'r using multiple rows, I'll show you code to loop through them.
But really the best source for this is php.net and HERE is info on mySQL DBs. They can explain this better than I can. But if you need examples or help, just ask.
Good Luck,
Mark Hensler
If there is no answer on Google, then there is no question.
Rob Pengelly posted this at 15:10 — 3rd December 2000.
This is another example to interact with a mysql database in PHP
<? //Connecting to the database $cnx = @mysql_connect('localhost') or die ("Could not connect: Can't Connect"); //selecting the database mysql_select_db('test_names') or die ("Selecting DB ERROR: ".mysql_error());
//Checking to see if the form has been filled out if($iname && $iemail) { //executing a query to add the name and email to the database mysql_query("INSERT INTO names SET Name='$iname', Email='$iemail'") or die ("INSERT QUERY ERROR:".mysql_error()); echo "Added";
} //Selecting and printing all the names/emails in the database $query = mysql_query("SELECT ID, Name, Email from names") or die ("SELECT QUERY ERROR:".mysql_error()); while($row = mysql_fetch_array($query)) { $id = $row["ID"]; $name = $row["Name"]; $email = $row["Email"]; echo "$id : $name : $email <br>"; } //closing connecting mysql_close($cnx); ?> <form action="<?echo $PHP_SELF?>"> <input type="text" name="iname"><br> <input type="text" name="iemail"><br> <input type="submit">
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.
Mark Hensler posted this at 05:33 — 3rd December 2000.
He has: 4,048 posts
Joined: Aug 2000
this is some code from a polling program I've written
(which needs beta testers, any takers?)
I put this is in an include file an use it everywhere (called connection.php)
<?
function db_connect () {
## In the line below, insert: ("host", "user", "password"); ##
$link = mysql_pconnect ("localhost", "root", "password");
## In the line below, insert ("your_database_name") ##
if ($link && mysql_select_db ("poll")) {
return ($link);
}
else {
return (FALSE);
}
}
?>
then in all your php scripts that connect to that DB, use this
<?
include "connection.php";
db_connect()
or die("Cannot connect to database");
# Let's get the settings
$query = "SELECT * FROM pollprefs";
#echo "<!--Query:".$query."-->\n";
$result = mysql_query ($query);
if (!$result)
die ("Query Error:\n<BR>\n\"$query\"");
$prefs = mysql_fetch_array($result);?>
<!-- some misc code here -->
<option value="default" <? if ($prefs["Ordering"] == "default") echo "SELECTED"; ?>>Default</option>
<option value="votesasc" <? if ($prefs["Ordering"] == "votesasc") echo "SELECTED"; ?>>Votes [Ascending]</option>
<option value="votesdesc" <? if ($prefs["Ordering"] == "votesdesc") echo "SELECTED"; ?>>Votes [Descending]</option>
<option value="alphaasc" <? if ($prefs["Ordering"] == "alphaasc") echo "SELECTED"; ?>>Alphabetical [Ascending]</option>
<option value="alphadesc" <? if ($prefs["Ordering"] == "alphadesc") echo "SELECTED"; ?>>Alphabetical [Descending]
What will you be using this for?
will you be retrieving single rows, ot multiple rows? if you'r using multiple rows, I'll show you code to loop through them.
But really the best source for this is php.net and HERE is info on mySQL DBs. They can explain this better than I can. But if you need examples or help, just ask.
Good Luck,
Mark Hensler
If there is no answer on Google, then there is no question.
Rob Pengelly posted this at 15:10 — 3rd December 2000.
They have: 850 posts
Joined: Jul 1999
This is another example to interact with a mysql database in PHP
<?
//Connecting to the database
$cnx = @mysql_connect('localhost') or die ("Could not connect: Can't Connect");
//selecting the database
mysql_select_db('test_names') or die ("Selecting DB ERROR: ".mysql_error());
//Checking to see if the form has been filled out
if($iname && $iemail)
{
//executing a query to add the name and email to the database
mysql_query("INSERT INTO names SET Name='$iname', Email='$iemail'") or die ("INSERT QUERY ERROR:".mysql_error());
echo "Added";
}
//Selecting and printing all the names/emails in the database
$query = mysql_query("SELECT ID, Name, Email from names") or die ("SELECT QUERY ERROR:".mysql_error());
while($row = mysql_fetch_array($query))
{
$id = $row["ID"];
$name = $row["Name"];
$email = $row["Email"];
echo "$id : $name : $email <br>";
}
//closing connecting
mysql_close($cnx);
?>
<form action="<?echo $PHP_SELF?>">
<input type="text" name="iname"><br>
<input type="text" name="iemail"><br>
<input type="submit">
output:
1 : James : [email protected]
2 : Rob : [email protected]
3 : Anthony : [email protected]
I suggest you go to http://www.webmasterbase.com and read the 10+ part series on PHP+MySQL.
[Edited by Rob Pengelly on Dec. 03, 2000 at 10:16 AM]
http://www.thehungersite.com - http://www.therainforestsite.com
http://www.ratemymullet.com - Beauty is only mullet deep.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.