Db - config.php help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Db - config.php help

Post 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
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post 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]";
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

It looks mostly correct.

But I'd change the last line to:
echo $setinfo["testfield"];
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post by doggy »

Nope it just gives me a blank page with nothing on it weird ...
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post by doggy »

Can no one help me out , no one at all
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Are you sure the database has information in it, and that there isnt an errror in connecting to the db?
User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

Post 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());
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post 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
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post 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);
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post 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();
?>
User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

Post by phpcoder »

Code: Select all

<?php 
function connect_db() &#123; 
$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; 
&#125; 
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
Last edited by phpcoder on Tue Jan 20, 2004 11:53 am, edited 1 time in total.
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post by doggy »

i don`t anderstand what u are saying
User avatar
phpcoder
Forum Contributor
Posts: 158
Joined: Sat Nov 02, 2002 1:18 pm
Location: Manchester, UK

Post 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
User avatar
doggy
Forum Commoner
Posts: 80
Joined: Tue Dec 09, 2003 5:01 am
Location: South Africa

Post by doggy »

yeah but how would i use this now with a select ... $sql = Select....
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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());
?>
Post Reply