Page 1 of 1
Simple mySQL query function? How?
Posted: Fri Oct 18, 2002 11:00 am
by Sky
I have pay hosting... but i was curios (also helping a friend) and signed up to tripod.lycos.co.uk's service. I want to make a simple query function, and this is what lycos provided-->
$dbhost = "localhost";
$dbuser = "***";
$dbpass = "***";
$db = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbuser . "_uk_db", $db);
$request = "SELECT * FROM test WHERE id=" . $id;
$result = mysql_query ($request,$db);
$test = mysql_fetch_object($result);
mysql_free_result($result);

Posted: Fri Oct 18, 2002 11:13 am
by volka
and the question is?
Posted: Fri Oct 18, 2002 11:19 am
by Sky
Well, I guess I sortof forgot that didn't I. Well, I would like to put it in a 'classic' function, like function sqlquery($col, $table, $id) and maybe make the output not only text/etc make it a variable/array (How does the one that tripod gave me work? It says to put this..
echo $test->a;
echo "<BR>";
echo $test->b;
echo "<BR>";
echo $test->c;
So $test->c; indicates the c part of the array? so i could go $test[c]?
As obviously indicated i'm a little of a newb so

Posted: Fri Oct 18, 2002 1:47 pm
by volka
- there are several mysql_fetch_...-functions
- mysql_fetch_array
- mysql_fetch_assoc
- mysql_fetch_field
- mysql_fetch_lengths
- mysql_fetch_object
- mysql_fetch_row
- mysql_result
mysql_fetch_object returns an object that represents the current row.
Accessing the fields is done by $obj->fieldname which will return the content of the specific field.
Read the manual entries about the returned values
http://www.php.net/manual/en/ref.mysql.php
tutorials are available i.e. at
http://www.phpcomplete.com/section_content.php?id=3
I would like to put it in a 'classic' function

Posted: Fri Oct 18, 2002 5:59 pm
by Sky
by classic i want to make a function IE:
Code: Select all
global($test);
global($id); (this right? how do i make vars global?)
function sqlquery($col, $table, $id)
{
$dbhost = "";
$dbuser = "***";
$dbpass = "***";
$db = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbuser . "_uk_db", $db);
$request = "SELECT " . $col . " FROM " . $table . " WHERE id=" . $id;
$result = mysql_query ($request,$db);
$test = mysql_fetch_object($result);
mysql_free_result($result);
}
Posted: Sat Oct 19, 2002 1:57 pm
by Sky
Posted: Sat Oct 19, 2002 3:13 pm
by volka
global($test);
global($id); (this right? how do i make vars global?)
has to be in the function where you want those variables to be treated as global.
But your're passing
$id as parameter and
$test should be the return value, so there's no need for
global
Posted: Sat Oct 19, 2002 4:46 pm
by Sky
*eyes water from too much code and instruction intake (php.net)*
O ya. well i was reffering to id as index.php?id=bla so i need to take it ou.... so would this be right? and I want $test to be accessable outside the function. so is this how i would declare it global?
Code: Select all
function sqlquery($col, $table, $id = $id)
{
global $test;
$dbhost = "";
$dbuser = "***";
$dbpass = "***";
$db = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbuser . "_uk_db", $db);
$request = "SELECT " . $col . " FROM " . $table . " WHERE id=" . $id;
$result = mysql_query ($request,$db);
$test = mysql_fetch_object($result);
mysql_free_result($result);
}
Posted: Sat Oct 19, 2002 7:30 pm
by volka
try
Code: Select all
function sqlquery($col, $table, $id)
{
$dbhost = "";
$dbuser = "***";
$dbpass = "***";
$db = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbuser . '_uk_db', $db);
$request = 'SELECT ' . $col . ' FROM ' . $table . ' WHERE id=' . $id;
$result = mysql_query ($request,$db);
return mysql_fetch_object($result);
}
// i.e.
$test = sqlquery('*', 'myTable', $id); // $id may be $_GETї'id']
that will only return the first row's content as object.
Otherwise you should return the result-set identifier
Code: Select all
function sqlquery($col, $table, $id)
{
$dbhost = "";
$dbuser = "***";
$dbpass = "***";
$db = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbuser . '_uk_db', $db);
$request = 'SELECT ' . $col . ' FROM ' . $table . ' WHERE id=' . $id;
return mysql_query ($request,$db);
}
// i.e.
$result = sqlquery('*', 'myTable', $id); // $id may be $_GETї'id']
echo '<table border="1">';
while($row = mysql_fetch_row($result))
{
echo '<tr>';
foreach($row as $field)
echo '<td>', $field, '</td>';
echo '</tr>';
}
echo '<table border="1">';
That function will reconnect the database for each query

Posted: Sun Oct 20, 2002 10:57 pm
by Sky
So what should i do? is connecting every time a bad thing if i don't run many queries?
and what's this about?3
Code: Select all
$result = sqlquery('*', 'myTable', $id); // $id may be $_GETї'id']
echo '<table border="1">';
while($row = mysql_fetch_row($result))
{
echo '<tr>';
foreach($row as $field)
echo '<td>', $field, '</td>';
echo '</tr>';
}
echo '<table border="1">';
Posted: Wed Oct 23, 2002 1:00 pm
by Sky
Bumpie....
Still need help...
Posted: Sat Nov 02, 2002 6:51 pm
by Sky
*slams to the front*(bump)
Posted: Sun Nov 03, 2002 4:21 am
by volka
return mysql_query ($request,$db);
the result identifier will be returned. That's the value that is used with mysql_fetch_.... to retrieve the records. Now
while($row = mysql_fetch_row($result))
will retrieve each record, assign it to $row and perform the loop-body.