Jump to content

[SOLVED] bad PHP? cant conect to DB


ryanpratt

Recommended Posts

I'm trying to conect to my server using this script.

 

Any idea what im doing wrong?

 

This is killing me.

 

<?php

 

$dbhost = 'localhost';

$dbuser = 'user';

$dbpass = 'password';

$dbname = 'database_name';

 

function conn()

{

connect to server');

$c = 'new mysqli("$dbhost", "$dbuser", "$dbpass")';

 

 

 

/* check connection */

if (mysqli_connect_errno()) {

printf("Connect failed: %s\n", mysqli_connect_error());

exit();

}

return $c;

}

function conndb()

{

mysql_select_db('$dbname');

}

function connend($c)

{

//OCILogoff($c);

//mysql_close($c);

$c->close();

}

 

?>

Link to comment
Share on other sites

$c = 'new mysqli("$dbhost", "$dbuser", "$dbpass")';

 

You cannot use these variables like this.  You have to pass them through.  Change this line to:

 

function conn($dbhost, $dbuser, $dbpass)

 

AND:

 

function conndb($dbname)

Link to comment
Share on other sites

like this?

 

im using mysql

 

<?php

$dbhost = 'internal-db.s35770.gridserver.com';
$dbuser = '******';
$dbpass = '*****';
$dbname = 'db35770_LI';

function conn ($dbhost, $dbuser, $dbpass)
{

//return OCILogon("ticketdb", "ticket","sandm");
//return mysql_connect($dbhost", "$dbuser", "$dbpass") or die('Cannot connect to server');
$c = 'new mysqli("$dbhost", "$dbuser", "$dbpass", "$dbname")';
//$c = new mysqli("localhost", "baka_reg", "33574817", "baka_mediamasons");


/* check connection */
if (mysqli_connect_errno()) {
	printf("Connect failed: %s\n", mysqli_connect_error());
	exit();
}
return $c;
}
function conndb ($dbname)
{
mysql_select_db('$dbname');
}
function connend($c)
{
//OCILogoff($c);
//mysql_close($c);
$c->close();
}

?>

 

Still no go?

 

(edited by kenrbnsn to remove username/password and to change the

tags to


tags)

Link to comment
Share on other sites

Then why do you have the mysql part commented out?

 

You need to use one or the other.  Also for errors use this instead:

 

or die(mysql_error());

 

Side note:  Those credentials look real, if they are, use asterisks instead.

Link to comment
Share on other sites

Sorry for the hassle. Im trying to conect to a querey some one else wrote for me. And thought i can and have done simple connections and quereys on many apps i have never done it the way this person has done it. So im just trying to fix this the rest of my code works. Any help would be grate.

Link to comment
Share on other sites

Try this: (change credentials, didn't want to post them again...)

 

$dbhost = 'host';
$dbuser = 'user';
$dbpass = 'pass';
$dbname = 'name';

function conn ($dbhost, $dbuser, $dbpass)
{
   $this->connection = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
}
function conndb ($dbname)
{
   $db_connect = mysql_select_db($dbname) or die(mysql_error());
}
function connend()
{
   mysql_close($this->connection);
}

?>

Link to comment
Share on other sites

Thanks a lot for helping me out MAQ

 

but i still get errors

 


Warning: Missing argument 1 for conn() in /nfs/c01/h03/mnt/35770/domains/mediamasons.com/html/li/connection.php on line 8

 

Warning: Missing argument 2 for conn() in /nfs/c01/h03/mnt/35770/domains/mediamasons.com/html/li/connection.php on line 8

 

Warning: Missing argument 3 for conn() in /nfs/c01/h03/mnt/35770/domains/mediamasons.com/html/li/connection.php on line 8

 

Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /nfs/c01/h03/mnt/35770/domains/mediamasons.com/html/li/connection.php on line 10

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)


Link to comment
Share on other sites

First of all, did you read the error messages and attempt to figure out why your code caused those errors? Your call to the function does not contain any parameters.

 

You would need to post the code that corresponds to the errors (less any actual username/password) to get any specific help with what is wrong with it.

 

Is there a reason you are not learning php, developing php code, and debugging php code on a local development system? Doing this on a live server takes longer and has security implications since code that is not complete and tested often has security holes that would allow a hacker to take advantage of your server.

Link to comment
Share on other sites

I have, but due to my limeted experience i have not been able to fix this myself. I have spent the last 6 hours trying to fix this since i receved it but to no avail. I am trying to learn php but am not even in the learning curve yet.

 

I was given this script by some one i hired on ODESK and told to just put in my information and then i would be off to the races. That does not seam to be the case thought.

Link to comment
Share on other sites

main page

<script src="js/globalscripts.js"></script>
<script src="js/start1.js"></script>
<body style="font-size:69%">
<font face="verdana">
<?php include ('connection.php'); ?>
<?php
$c = conn();
$query = "SELECT tool_clasification_type_id FROM tool_clasification_type ORDER BY tool_clasification_type_id";
if ($stmt = $c->prepare($query))
{
$stmt->execute();
$stmt->bind_result($tool_clasification_type_id);
while ($stmt->fetch())
{
	echo "<span id='counting".$tool_clasification_type_id."' style='display:none;'></span>";
	$countx++;
}
$stmt->close();
echo "<span style='display:none' id='countVal'>".$countx."</span>";
}
?>
<span style="display:none" id="selectedVal"></span>
<?php
/*
$query = "SELECT tool_clasification.tool_clasification_order, tool_clasification.tool_clasification_name, tool_clasification.tool_clasification_id, tool_clasification_type.tool_clasification_type, tool_clasification_type.tool_clasification_type_order
FROM (
tool_clasification
LEFT JOIN tool_clasification_type ON tool_clasification_type.tool_clasification_id = tool_clasification.tool_clasification_id
)
ORDER BY tool_clasification.tool_clasification_order ASC , tool_clasification_type.tool_clasification_type_order ASC";
*/
$query = "SELECT tool_clasification_id, tool_clasification_name FROM tool_clasification ORDER BY tool_clasification_id ASC";
if ($stmt = $c->prepare($query))
{
$arrCateg = array();
$stmt->execute();
//$stmt->bind_result($tool_clasification_id, $tool_clasification_name, $tool_clasification_order, $tool_clasification_type, $tool_clasification_type_order);
$stmt->bind_result($tool_clasification_id, $tool_clasification_name);
while ($stmt->fetch())
{
	array_push($arrCateg, $tool_clasification_name, $tool_clasification_id);
}
$stmt->close();
if(count($arrCateg)/2>0)
{
	for($i=0;$i<count($arrCateg)/2;$i++)
	{
		if($i==0)
		{
			$prev = $arrCateg[($i*2)];
			echo "<span style='background-image:url(tool_btn_left.gif);height:24px;'><span style='background-image:url(tool_btn.gif);height:24px;' onclick='selectChoice(".$arrCateg[($i*2)+1].");'>   <span style='position:relative;top:5px;'>".$arrCateg[($i*2)]."</span>   </span></span>";
			echo "<span id='".$arrCateg[($i*2)]."' style='display:none;'></span>";

		}
		else if($i!=0 && $prev!=$arrCateg[($i*2)])
		{
			echo "<span style='background-image:url(tool_btn.gif);height:24px;' onclick='selectChoice(".$arrCateg[($i*2)+1].");'>   <span style='position:relative;top:5px;'>".$arrCateg[($i*2)]."</span>   </span>";
			$prev = $arrCateg[($i*2)];
			echo "<span id='".$arrCateg[($i*2)]."' style='display:none;'></span>";

		}
		else
		{
			//do nothing, not a unique name
			$prev = $arrCateg[($i*2)];
		}
		echo " ";
	}
}
else
{
	//no data
	echo "There are currently no classifications";
}

}
?>
<br>
<span style='display:none;' id='loadingscreen'><img src='images/ajax-loader.gif'></span>
<span id='detail_categ'></span>
<br>
<span id='initial'>
<?php
$query = "SELECT li_classification.classification_id, li_classification.classification_name, tool.tool_id, tool.tool_name, tool.tool_color, tool.tool_zone, tool.tool_cost, tool.tool_maintenance, tool.tool_slope, tool.tool_soil, tool.tool_climate, li_classification.classification_color
FROM (tool
LEFT JOIN li_classification ON li_classification.classification_id=tool.tool_LI_classification_id)
ORDER BY li_classification.classification_order ASC, tool.tool_order ASC";
if ($stmt = $c->prepare($query))
{
$arrInitial = array();
$stmt->execute();
$stmt->bind_result($classification_id, $classification_name, $tool_id, $tool_name, $tool_color, $tool_zone, $tool_cost, $tool_maintenance, $tool_slope, $tool_soil, $tool_climate, $clasification_color);
while ($stmt->fetch())
{
	array_push($arrInitial, $classification_id, $classification_name, $tool_id, $tool_name, $tool_color, $tool_zone, $tool_cost, $tool_maintenance, $tool_slope, $tool_soil, $tool_climate, $clasification_color);
}
$stmt->close();
if(count($arrInitial)/12>0)
{
	for($i=0;$i<count($arrInitial)/12;$i++)
	{
		if($i==0)
		{
			$prev = $arrInitial[($i*12)];
			echo "<span onclick='toggle(\"".$arrInitial[($i*12)]."\");'><img src='arrow_open.gif' id='toggleimg".$arrInitial[($i*12)]."'></src><b>".$arrInitial[($i*12)+1]."</b></span>";
			echo "<br>";
			echo "<span id='toggle".$arrInitial[($i*12)]."'>";
			echo "<span>"."<img style='height:10px;width=10px;background-color:".$arrInitial[($i*12)+4]."'>".$arrInitial[($i*12)+3]."</img>"."</span>";
			echo "<br>";
			echo "<span id='togretain".$arrInitial[($i*12)]."'></span>";
			$countuniq++;
		}
		else if($i!=0 && $prev==$arrInitial[($i*12)])
		{
			echo "<span>"."<img style='height:10px;width=10px;background-color:".$arrInitial[($i*12)+4]."'></img>".$arrInitial[($i*12)+3]."</span>";
			echo "<br>";
			$prev = $arrInitial[($i*12)];
		}
		else
		{
			echo "</span>";
			echo "<span onclick='toggle(\"".$arrInitial[($i*12)]."\");'><img src='arrow_open.gif' id='toggleimg".$arrInitial[($i*12)]."'></src><b>".$arrInitial[($i*12)+1]."</b></span>";
			echo "<br>";
			echo "<span id='toggle".$arrInitial[($i*12)]."'>";
			echo "<span>"."<img style='height:10px;width=10px;background-color:".$arrInitial[($i*12)+4]."'></img>".$arrInitial[($i*12)+3]."</span>";
			echo "<br>";
			echo "<span id='togretain".$arrInitial[($i*12)]."'></span>";
			$prev = $arrInitial[($i*12)];
			$countuniq++;
		}
	}
	echo "<span id='togretainall' style='display:none;'>".$countuniq."</span>";
	echo "</span>";
}
}
connend($c);
?>
<span>
</font>
</body>

Link to comment
Share on other sites

I'M doing that. Am i doing something wrong with this method?

 

<?php

$dbhost = 'internal-db.s35770.gridserver.com';
$dbuser = '---------------';
$dbpass = '---------------';
$dbname = 'db35770_LI';

function conn ($dbhost, $dbuser, $dbpass)
{
   $this->connection = mysqli_connect($dbhost, $dbuser, $dbpass) or die(mysqli_error());
}
function conndb ($dbname)
{
   $db_connect = mysqli_select_db($dbname) or die(mysqli_error());
}
function connend()
{
   mysqli_close($this->connection);
}

?>

Link to comment
Share on other sites

Gevans posted the line of code (which is probably mentioned in the full text of the error message) and what it needs to be changed to -

You need to pass your variables when you call the conn() functions

 

so 

<?php
$c = conn(); //wrong

$c = conn($dbhost, $dbuser, $dbpass); //right
?>

 

The same goes for the other functions that take arguments!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.