Error trying to connect to DB

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
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Error trying to connect to DB

Post by 4Boredom »

Warning: main(/home/i4boredo/marlins/db.php): failed to open stream: No such file or directory in /home/i4boredo/public_html/marlins/processo.php on line 3

Fatal error: main(): Failed opening required '/home/i4boredo/marlins/db.php' (include_path='.:/usr/local/lib/php') in /home/i4boredo/public_html/marlins/processo.php on line 3


db.php

Code: Select all

I have vars up here with pws that are corrent but ive taken out for privacy reasons

#connection info
$connection = mysql_connect("$dbhost","$dbusername","$dbpasswd")  
    or die ("Couldn't connect to server."); 
$db = mysql_select_db("$database_name", $connection) 
    or die("Couldn't select database."); 
?>

Code: Select all

<?
ob_start();
require_once("/home/i4boredo/marlins/db.php");


$Name = $_POST['Name'];
$G = $_POST['G'];
$AB = $_POST['AB'];
$Runs = $_POST['Runs'];
$Hits = $_POST['Hits'];
$B2 = $_POST['2B'];
$B3 = $_POST['3B'];
$HR = $_POST['HR'];
$RBI = $_POST['RBI'];
$BB = $_POST['BB'];
$SO = $_POST['SO'];
$CS = $_POST['CS'];
$HBP = $_POST['HBP'];
$SAC = $_POST['SAC'];

$sql = mysql_query("UPDATE `offensive_stats` SET G='{$G}',AB='{$AB}',Runs='{$Runs}',Hits = '{$Hits}',2B = '{$B2}',3B = '{$B3}',HR = '{$HR}',RBI = '{$RBI}',BB = '{$BB}',SO = '{$SO}',CS = '{$CS}',HBP = '{$HBP}',SAC = '{$SAC}' WHERE Name = '{$Name}'");


if(!$sql){ 
    $error.=  'There has been an error posting your comment. Please contact the webmaster.'; 
} else { 
    echo 'Stat Added!';
} 

?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Check your filename spelling (and case) and make sure the file is actually in the specified path. That first error is what is killing your app. It is telling you that it cannot open the included file because the file, according to PHP, does not exist.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Re: Error trying to connect to DB

Post by ok »

4Boredom wrote:Warning: main(/home/i4boredo/marlins/db.php): failed to open stream: No such file or directory in /home/i4boredo/public_html/marlins/processo.php on line 3

Fatal error: main(): Failed opening required '/home/i4boredo/marlins/db.php' (include_path='.:/usr/local/lib/php') in /home/i4boredo/public_html/marlins/processo.php on line 3
Check file permissions if you work under linux. Make sure that PHP/your web server has access to the file.
4Boredom wrote: db.php

Code: Select all

I have vars up here with pws that are corrent but ive taken out for privacy reasons

#connection info
$connection = mysql_connect("$dbhost","$dbusername","$dbpasswd")  
    or die ("Couldn't connect to server."); 
$db = mysql_select_db("$database_name", $connection) 
    or die("Couldn't select database."); 
?>
You don't need to put quotes on vars. And, if you open only one connection to the DB, you don't need $connection:

Code: Select all

#connection info
mysql_connect($dbhost, $dbusername, $dbpasswd) or die ("Couldn't connect to server."); 
$db = mysql_select_db($database_name) or die("Couldn't select database."); 
?>
You can write processo.php in an easy way:

Code: Select all

<?
ob_start();
require_once("/home/i4boredo/marlins/db.php");

extract($_POST);

$sql = mysql_query("UPDATE `offensive_stats` SET G='{$G}',AB='{$AB}',Runs='{$Runs}',Hits = '{$Hits}',2B = '{$B2}',3B = '{$B3}',HR = '{$HR}',RBI = '{$RBI}',BB = '{$BB}',SO = '{$SO}',CS = '{$CS}',HBP = '{$HBP}',SAC = '{$SAC}' WHERE Name = '{$Name}'");

if(!$sql){ 
    $error =  "There has been an error posting your comment. Please contact the webmaster."; 
} else { 
    echo "Stat Added!";
} 

?>
P.S
I'm not sure that you can add {} in a quoted string: $a = "My name is: {$name}".
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: Error trying to connect to DB

Post by volka »

ok wrote:P.S
I'm not sure that you can add {} in a quoted string: $a = "My name is: {$name}".
You can

Code: Select all

<?php
$x = 1;
$y = 'abc';

$a = "{$x} ... {$y}";
echo $a, "<br />\n";
?>
But you don't need to in this simple case, see see http://de2.php.net/language.types.strin ... ng.complex
While at it, please also read http://de2.php.net/mysql_real_escape_string and http://en.wikipedia.org/wiki/SQL_injection
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Re: Error trying to connect to DB

Post by ok »

4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

Yea my permissions were set wrong.. I had set teh forum permission right but not each file.

Not it locates the DB.... but it isnt processing like it should http://4boredom.com/marlins/input.php.

offensive_stats does nothing

Whats the code to get it to relay output?
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

a good idea would be to break the mysql_query and the actual query into diffrent variables. for example:

$sql = QUERY;
$query = mysql_query($sql);

Therefore you can use echo $sql to see if that is the info you want entered into the database.
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

This posts.... ($sql)Stat Added!

So is it doin anything? Shouldnt it say stat added twice?

Code: Select all

$sql = ("UPDATE `offensive_stats` SET G='{$G}',AB='{$AB}',Runs='{$Runs}',Hits = '{$Hits}',2B = '{$B2}',3B = '{$B3}',HR = '{$HR}',RBI = '{$RBI}',BB = '{$BB}',SO = '{$SO}',CS = '{$CS}',HBP = '{$HBP}',SAC = '{$SAC}' WHERE Name = '{$Name}'");
$query = mysql_query($sql); 

echo '($sql)';


if(!$sql){ 
    $error.=  'There has been an error posting your comment. Please contact the webmaster.'; 
} else { 
    echo 'Stat Added!';
}
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

I tried simplifying the top part of code... same feedback on output page...and no post in db :(

Code: Select all

$sql = ("UPDATE `offensive_stats` 
				(`G`, `AB`, `Runs`, `Hits`, `2B`, `3B`, `HR`, `RBI`, `BB`, `SO`, `CS`, `HBP`, `SAC`) VALUES
				(`$G`, `$AB`, `$Runs`, `$Hits`, `$2B`, `$3B`, `$HR`, `$RBI`, `$BB`, `$SO`, `$CS`, `$HBP`, `$SAC`) WHERE Name = '($Name)'");


$query = mysql_query($sql); 

echo '($sql)';
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

There's no need for parentheses arround string literals $a=("xyz"); -> $a="xyz"; It doesn't harm but it's just mambo jambo that clouds the important things.
There's no need for curly braces in double-quoted strings when you have plain/scalar variables and no speacial signs around it "... '{$xyz}' ..." -> "... $xyz ...". imho too many magic/control characters make code less readable and I'd leave them out in this case (in case of doubt put them in).
Variable substitution does not take place in single-quoted strings, echo '($sql)' will print ($sql), use double-quotes instead.
You don't want to check if there's still something in the string variable, you want to check the return value of mysql_query, if(!$sql) -> if(!$query)
=>

Code: Select all

$sql = "UPDATE `offensive_stats` SET G='$G',AB='$AB',Runs='$Runs',Hits = '$Hits',2B = '$B2',3B = '$B3',HR = '$HR',RBI = '$RBI',BB = '$BB',SO = '$SO',CS = '$CS',HBP = '$HBP',SAC = '$SAC' WHERE Name = '$Name'";
$query = mysql_query($sql);

echo "($sql)";

if(!$query)
	$error.=  'There has been an error posting your comment. Please contact the webmaster.';
else 
	echo 'Stat Added!';
but try

Code: Select all

$sql = "UPDATE
		`offensive_stats`
	SET
		G='$G', AB='$AB', Runs='$Runs', Hits='$Hits',
		2B='$B2', 3B='$B3', HR='$HR', RBI='$RBI',
		BB='$BB', SO='$SO', CS='$CS', HBP='$HBP',
		SAC='$SAC'
	WHERE
		Name='$Name'";

echo '<div>Debug: ', htmlentities($sql), "</div>\n";;
$query = mysql_query($sql) or die(mysql_error());
I hope you already did
Post Reply