Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
Sky
Forum Commoner
Posts: 71 Joined: Wed Oct 02, 2002 4:00 pm
Post
by Sky » Fri Oct 18, 2002 11:00 am
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);
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Fri Oct 18, 2002 11:13 am
and the question is?
Sky
Forum Commoner
Posts: 71 Joined: Wed Oct 02, 2002 4:00 pm
Post
by Sky » Fri Oct 18, 2002 11:19 am
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
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Fri Oct 18, 2002 1:47 pm
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
Sky
Forum Commoner
Posts: 71 Joined: Wed Oct 02, 2002 4:00 pm
Post
by Sky » Fri Oct 18, 2002 5:59 pm
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);
}
Sky
Forum Commoner
Posts: 71 Joined: Wed Oct 02, 2002 4:00 pm
Post
by Sky » Sat Oct 19, 2002 1:57 pm
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sat Oct 19, 2002 3:13 pm
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
Sky
Forum Commoner
Posts: 71 Joined: Wed Oct 02, 2002 4:00 pm
Post
by Sky » Sat Oct 19, 2002 4:46 pm
*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);
}
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sat Oct 19, 2002 7:30 pm
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
Sky
Forum Commoner
Posts: 71 Joined: Wed Oct 02, 2002 4:00 pm
Post
by Sky » Sun Oct 20, 2002 10:57 pm
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">';
Sky
Forum Commoner
Posts: 71 Joined: Wed Oct 02, 2002 4:00 pm
Post
by Sky » Wed Oct 23, 2002 1:00 pm
Bumpie....
Sky
Forum Commoner
Posts: 71 Joined: Wed Oct 02, 2002 4:00 pm
Post
by Sky » Sat Nov 02, 2002 6:51 pm
*slams to the front*(bump)
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sun Nov 03, 2002 4:21 am
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.