SQL connect
Moderator: General Moderators
SQL connect
I need a small bit of script that will let a page connect to my db and then select the table and id...what is that code 
This should do the trick. just change the variables at the top and the query at the bottom.
Code: Select all
<?php
<?php
$dbhost = 'yourhost';
$dbusername = 'yourusername';
$dbuserpassword = 'yourpassword';
$default_dbname = 'yourdb';
$MYSQL_ERRNO = ' ';
$MYSQL_ERROR = ' ';
function db_connect()
{
global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;
$link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
if(!$link_id)
{
$MYSQL_ERRNO = 0;
$MYSQL_ERROR = "Connection failed to the host $dbhost.";
return 0;
}
else if(empty($dbname) && !mysql_select_db($default_dbname))
{
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
}
else return $link_id;
}
$link_id=db_connect();
$query = mysql_query("SELECT * FROM table WHERE id='$id'", $link_id);
?>
?>Code: Select all
<?php
$db = mysql_connect("localhost","username","password");
mysql_select_db("database");
$result = mysql_query("SELECT * FROM table");
while($row= mysql_fetch_array($result))
{
echo "$rowї0]<br />";
}
mysql_close($db);
?>Alot, thx.
Im going to be using as link like this :
Connect.php?table=unz&id=2.
would it be possible to just put <?=$table?> in??
EG
<?php
$db = mysql_connect("localhost","username","password");
mysql_select_db("database");
$result = mysql_query("SELECT * FROM <?=$table?>");
while(<?=$table?>= mysql_fetch_array($result))
{
echo "<?=$table?>[0]<br />";
}
mysql_close($db);
?>
Im going to be using as link like this :
Connect.php?table=unz&id=2.
would it be possible to just put <?=$table?> in??
EG
<?php
$db = mysql_connect("localhost","username","password");
mysql_select_db("database");
$result = mysql_query("SELECT * FROM <?=$table?>");
while(<?=$table?>= mysql_fetch_array($result))
{
echo "<?=$table?>[0]<br />";
}
mysql_close($db);
?>
Works so nm :p<?php
$db = mysql_connect("localhost","unknownz","password");
mysql_select_db("unknownz");
$result = mysql_query("SELECT * FROM $table");
while($row = mysql_fetch_array($result))
{
echo "$row[0]<br />";
}
mysql_close($db);
?>
Last edited by Aaron on Sat Jun 01, 2002 6:55 pm, edited 1 time in total.
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact: