Page 1 of 1

SQL connect

Posted: Sat Jun 01, 2002 1:39 pm
by Aaron
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 8)

Posted: Sat Jun 01, 2002 2:31 pm
by ozmodiar
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);
?>
?>

Posted: Sat Jun 01, 2002 2:38 pm
by Aaron
Thx, I was hoping for something smaller :?

Posted: Sat Jun 01, 2002 3:04 pm
by MattF

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);
?>
Is that better?

Posted: Sat Jun 01, 2002 3:09 pm
by Aaron
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);
?>

Posted: Sat Jun 01, 2002 4:55 pm
by Aaron
<?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);
?>
Works so nm :p

Posted: Sat Jun 01, 2002 5:00 pm
by hob_goblin
is it just me, or did you give us your password

Posted: Sat Jun 01, 2002 6:09 pm
by volka
go, find his ip-address ;)

Posted: Sat Jun 01, 2002 6:58 pm
by Aaron
It wasnt the right password, for the db i use, that was my offline db....so it was no use :)