[PHP]Functions working with database fail..
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 Code:
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 Code:
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 :-)
Re: [PHP]Functions working with database fail..
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.
Re: [PHP]Functions working with database fail..
Quote:
Originally Posted by
shadow-xx
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 Code:
function con_mysql($con_db,$con_sql) {
mysql_select_db($con_db);
$result = mysql_query($con_sql);
$row = mysql_fetch_array($result);
}
Re: [PHP]Functions working with database fail..
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 Code:
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 Code:
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 Code:
$query_value = con_mysql("database name", "query");
~ Iceman
Re: [PHP]Functions working with database fail..
Quote:
Originally Posted by
iceman4154
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 Code:
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 Code:
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 Code:
$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 Code:
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!
Re: [PHP]Functions working with database fail..
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 Code:
function con_mysql($con_db,$con_sql) {
mysql_select_db($con_db);
$result = mysql_query($con_sql);
$row = mysql_fetch_array($result);
return $row;
}
Re: [PHP]Functions working with database fail..
Quote:
Originally Posted by
holthelper
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 Code:
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.
Re: [PHP]Functions working with database fail..
Try the following.
1) Catching errors:
PHP Code:
<?php
error_reporting(E_ALL);
?>
2) Echo mysql errors.
PHP Code:
<?php
con_mysql("bla", "bla", "bla") or die("warning: ". mysql_error());
?>
3)Echo all your vars, strings, etc.
PHP Code:
<?php
echo $crap;
echo $crap2;
echo $crap3;
?>
Good luck ;)
Re: [PHP]Functions working with database fail..
This should work. Its a little longer but in my opinion my preference is this.
PHP Code:
<?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'];
}
?>
Re: [PHP]Functions working with database fail..
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
Re: [PHP]Functions working with database fail..
Quote:
Originally Posted by
shadow-xx
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;
}