SQL connect

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Aaron
Forum Commoner
Posts: 82
Joined: Sun May 12, 2002 2:51 pm

SQL connect

Post 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)
ozmodiar
Forum Commoner
Posts: 35
Joined: Sat Jun 01, 2002 6:29 am
Location: Dublin, Ireland

Post 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);
?>
?>
Aaron
Forum Commoner
Posts: 82
Joined: Sun May 12, 2002 2:51 pm

Post by Aaron »

Thx, I was hoping for something smaller :?
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post 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?
Aaron
Forum Commoner
Posts: 82
Joined: Sun May 12, 2002 2:51 pm

Post 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);
?>
Aaron
Forum Commoner
Posts: 82
Joined: Sun May 12, 2002 2:51 pm

Post 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
Last edited by Aaron on Sat Jun 01, 2002 6:55 pm, edited 1 time in total.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

is it just me, or did you give us your password
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

go, find his ip-address ;)
Aaron
Forum Commoner
Posts: 82
Joined: Sun May 12, 2002 2:51 pm

Post by Aaron »

It wasnt the right password, for the db i use, that was my offline db....so it was no use :)
Post Reply