Page 1 of 1
Db - config.php help
Posted: Tue Jan 20, 2004 12:14 am
by doggy
I wanne make a basic config.php with db username and password and the database and so on ... and i wanne then use "Select" statment but i am sort of new to php .. could some one please help me out or show me a url of a toturliel that can help me out or any thing .. please
Thanks
Posted: Tue Jan 20, 2004 1:17 am
by doggy
I got this now but its not working and i have no idea what it is not working can any one please help me out with the problim i would really be great full
// create connection; substitute your own information
$conn = mysql_connect("localhost","root","root");
// select database; substitute your own database name
$db = mysql_select_db("phpatlas", $conn);
$sql = "SELECT id, testfield FROM test";
$result = mysql_query($sql,$conn);
$setinfo = mysql_fetch_row($result);
echo "$setinfo[testfield]";
Posted: Tue Jan 20, 2004 1:25 am
by microthick
It looks mostly correct.
But I'd change the last line to:
echo $setinfo["testfield"];
Posted: Tue Jan 20, 2004 1:29 am
by doggy
Nope it just gives me a blank page with nothing on it weird ...
Posted: Tue Jan 20, 2004 4:10 am
by doggy
Can no one help me out , no one at all
Posted: Tue Jan 20, 2004 10:44 am
by Roja
Are you sure the database has information in it, and that there isnt an errror in connecting to the db?
Posted: Tue Jan 20, 2004 11:14 am
by phpcoder
try this may b u have some problem in conecting to ur database
Code: Select all
mysql_connect ($host, $uname, $pass) or die ('I cannot connect to the database because: ' . mysql_error());
Posted: Tue Jan 20, 2004 11:21 am
by doggy
There is information in the database ... this script just gives me a blank page and i cant anderstand why.
// create connection; substitute your own information
$conn = mysql_connect ("localhost", "root", "root") or die ('I cannot connect to the database because: ' . mysql_error());
// select database; substitute your own database name
$db = mysql_select_db("phpatlas", $conn);
$sql = "SELECT id, testfield FROM test";
$result = mysql_query($sql,$conn);
$setinfo = mysql_fetch_row($result);
$testfield = $setinfo["testfield"];
echo "$testfield";
database base
id testfield
1 213123
Posted: Tue Jan 20, 2004 11:28 am
by doggy
I found this from a irc help and it works so i am going to use this now ... thanks guys.
mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("phpatlas");
$result = mysql_query("SELECT id, testfield FROM test");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}
mysql_free_result($result);
Posted: Tue Jan 20, 2004 11:36 am
by doggy
Amm i found this script also and i was wondering how would i use this now for a select statemet , please help ...
<?php
function connect_db() {
$db_server = 'localhost';
$db_user = 'root';
$db_password = 'root';
$db_database = 'phpatlas';
$dbi = mysql_connect($db_server, $db_user, $db_password) or die(mysql_error());
mysql_select_db($db_database);
return $dbi;
}
if (!isset($dbi)) $dbi = connect_db();
?>
Posted: Tue Jan 20, 2004 11:40 am
by phpcoder
Code: Select all
<?php
function connect_db() {
$db_server = 'localhost';
$db_user = 'root';
$db_password = 'root';
$db_database = 'phpatlas';
$dbi = mysql_connect($db_server, $db_user, $db_password) or die(mysql_error());
mysql_select_db($db_database);
return $dbi;
}
if (!isset($dbi))
$dbi = connect_db();
$sql = "SELECT id, testfield FROM test";
$result = mysql_query($sql,$dbi);
$setinfo = mysql_fetch_row($result);
//rest sameas u did
Posted: Tue Jan 20, 2004 11:43 am
by doggy
i don`t anderstand what u are saying
Posted: Tue Jan 20, 2004 11:47 am
by phpcoder
doggy wrote:Amm i found this script also and i was wondering how would i use this now for a select statemet , please help ...
<?php
function connect_db() {
$db_server = 'localhost';
$db_user = 'root';
$db_password = 'root';
$db_database = 'phpatlas';
$dbi = mysql_connect($db_server, $db_user, $db_password) or die(mysql_error());
mysql_select_db($db_database);
return $dbi;
}
if (!isset($dbi)) $dbi = connect_db();
?>
just put this function in ur file and call this function and then apply any query that u want. i think u asked i have this function how u would use its for select statement
Posted: Tue Jan 20, 2004 11:50 am
by doggy
yeah but how would i use this now with a select ... $sql = Select....
Posted: Tue Jan 20, 2004 2:52 pm
by DuFF
It seems like you're having a lot of trouble. You should go back and read about the proper way to connect to a database and how exactly to use and execute SQL expressions.
http://www.devshed.com/c/a/MySQL/Beginn ... -Tutorial/
http://www.php.net/manual/en/ref.mysql.php (Scroll down to the examples)
Here is how I make a database connection:
Code: Select all
<?php
@mysql_connect($host,$user,$password) or die("Unable to connect to MySQL database: " . mysql_error());
@mysql_select_db($this->db) or die( "Unable to select MySQL database.");
$query = "SELECT * FROM table WHERE foo='$bar'";
$result = mysql_query($query) or die("MySQL query failed: " . mysql_error());
?>