[PHP]Functions working with database fail..

Joined
Oct 9, 2008
Messages
572
Reaction score
14
Well, the thing is that I'm trying to make things easier for myself, and I figgured making a function to shortend the scripting, and here is my function:
PHP:
function con_mysql($con_db,$con_sql) {
         mysql_connect("localhost","root","root") or die(mysql_error());
         mysql_select_db($con_db);
         $result = mysql_query($con_sql);
         $row = mysql_fetch_array($result);
         }
and here is what I script:
PHP:
con_mysql("test","SELECT * testtable");

   print $row['title'];

And well, all information is correct but its simply not working. The 2 scripts are located in diffrent files, but I'm using include() and I've tested if the function "works", and it does, but when I try to print something out, it returns blank.

Help is very much appreciated :-)
 
Your function doesn't return anything
data should be returned with "return()"

then grabbing the data into a variable would look something like this
$var = my_function();

but why would you connect to the database each time you send it a query?
u only have to connect once.
 
Your function doesn't return anything
data should be returned with "return()"

then grabbing the data into a variable would look something like this
$var = my_function();

but why would you connect to the database each time you send it a query?
u only have to connect once.

Thank you for that :-)

And how should I use return here? Now it looks like this:
PHP:
function con_mysql($con_db,$con_sql) {
         mysql_select_db($con_db);
         $result = mysql_query($con_sql);
         $row = mysql_fetch_array($result);
         }
 
Hi i am not the best at PHP but i think what your wanting is to grab data from a MySQL database and pass it to a variable

Your code:
PHP:
function con_mysql($con_db,$con_sql) {
         mysql_select_db($con_db);
         $result = mysql_query($con_sql);
         $row = mysql_fetch_array($result);
         }

What your code should look like:
PHP:
function con_mysql($con_db,$con_sql) {
         mysql_select_db($con_db);
         $result = mysql_query($con_sql);
         $row = mysql_fetch_array($result);
         return $row;
         }

Then to pass that to another variable:
PHP:
$query_value = con_mysql("database name", "query");

~ Iceman
 
Hi i am not the best at PHP but i think what your wanting is to grab data from a MySQL database and pass it to a variable

Your code:
PHP:
function con_mysql($con_db,$con_sql) {
         mysql_select_db($con_db);
         $result = mysql_query($con_sql);
         $row = mysql_fetch_array($result);
         }

What your code should look like:
PHP:
function con_mysql($con_db,$con_sql) {
         mysql_select_db($con_db);
         $result = mysql_query($con_sql);
         $row = mysql_fetch_array($result);
         return $row;
         }

Then to pass that to another variable:
PHP:
$query_value = con_mysql("database name", "query");

~ Iceman

Wow, thank you very much!! :)

I also thought instead of using return, then I would add an extra variable to the function and make it print the variable. May be a little hard to explain, so heres what I thought before I saw your anwser:
PHP:
function con_mysql($con_db,$con_sq,$con_table) {
         mysql_select_db($con_db);
         $result = mysql_query($con_sql);
         $row = mysql_fetch_array($result);
         print $row[$con_table];
         }
But I am now using it as you posted, and thank you very very much for that!
 
my advise would be to stick with the return value because any way you still have to have the return.

use icemans code he posted:
PHP:
function con_mysql($con_db,$con_sql) {
         mysql_select_db($con_db);
         $result = mysql_query($con_sql);
         $row = mysql_fetch_array($result);
         return $row;
}
 
my advise would be to stick with the return value because any way you still have to have the return.

use icemans code he posted:
PHP:
function con_mysql($con_db,$con_sql) {
         mysql_select_db($con_db);
         $result = mysql_query($con_sql);
         $row = mysql_fetch_array($result);
         return $row;
}

Yes, that is also what I'm going with :-)

I'm sorry if I made myself unclear.
 
Try the following.
1) Catching errors:
PHP:
<?php
error_reporting(E_ALL);
?>
2) Echo mysql errors.
PHP:
<?php
con_mysql("bla", "bla", "bla") or die("warning: ". mysql_error());
?>
3)Echo all your vars, strings, etc.
PHP:
<?php
echo $crap;
echo $crap2;
echo $crap3;
?>

Good luck ;)
 
This should work. Its a little longer but in my opinion my preference is this.
PHP:
<?PHP
class test
{
	var $dbHost = "localhost";
	var $dbUser = "root";
	var $dbPass = "root";
	var $db = "database";
	var $dbQuery;
	
	function Connect()
	{
		$Con = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
		if($Con != true) {
			return false;
		} else {
			mysql_select_db($this->db);
		}
	}
	function grabInformation()
	{
		$query = mysql_query("".$this->dbQuery."") or die(mysql_error());
		if(mysql_num_rows > 1) {
			if($info = mysql_fetch_array($query)) {
				return $info;
			} else {
				die(mysql_error());
			}
		} else {
			return false;
		}
	}
}
$grabInfo = new test;
$grabInfo->dbQuery = "SELECT * FROM testtable";
$grabInfo->Connect();
$errorCheck = $grabInfo->grabInformation();
if($errorCheck != true) {
	echo "There was an error: ".$errorCheck;
} else {
	echo $errorCheck['title'];
}
?>
 
Last edited:
Yeah, i am new to PHP, i understand it but i haven't had time to write any complex scripts... I wrote an auto-emailer and that's about it...

~ Iceman
 
Your function doesn't return anything
data should be returned with "return()"

then grabbing the data into a variable would look something like this
$var = my_function();

but why would you connect to the database each time you send it a query?
u only have to connect once.

yep :) thats right.. u need to return somthing so for example

function con_mysql($con_db,$con_sql) {
mysql_select_db($con_db);
$result = mysql_query($con_sql);
$row = mysql_fetch_array($result);
return
$row;
}
 
Back