[PHP + MySQL] Query Function Problem
Hello, currently writing a script.
I have a problem, though.
See, I want a quick way to load information from the database, so I tried this, but it didn't work, any ideas?
PHP Code:
function query($sqlQuery)
{
MySQLConnect(); // Works
$sqlReturn=mysql_query($sqlQuery);
return $sqlReturn;
}
and then use like:
PHP Code:
echo query("SELECT `column` FROM `table` WHERE `column` = 'columnname'");
Thanks for contributions!
Re: [PHP + MySQL] Query Function Problem
You'll have to fetch the data: PHP: mysql_fetch_assoc - Manual
In other words:
PHP Code:
while ($row = mysql_fetch_assoc($result))
{
echo $row["column"];
}
And if the query will only select one row, replace while by if.
Re: [PHP + MySQL] Query Function Problem
But how would I turn that into a function?
This doesn't work completely:
PHP Code:
function query($table, $id, $colval, $column)
{
connect();
$sql=mysql_query("SELECT * FROM $table WHERE $id='$colval'");
while ($row=mysql_fetch_assoc($sql))
{
return $row["$column"];
}
}
edit: added the mysql_query in $sql, removed 1 problem - 4 more left :wink:
:frown:
Re: [PHP + MySQL] Query Function Problem
PHP Code:
function query($table, $id, $colval, $column)
{
connect();
$query = mysql_query("SELECT * FROM ".$table." WHERE $id='".$colval."'");
for ($i = 0; $row = mysql_fetch_assoc($query); $i++)
{
$output[$i] = $row;
}
return $output;
}
Usage:
PHP Code:
$rows = query(...);
if ($rows[1]['column'] ==$rows[0]['id'] )
{
...
}
Re: [PHP + MySQL] Query Function Problem
I've actually found a nice one, which works like query($query).
sauce:
PHP Code:
/* MySQL Query Function */
function query($query) {
connect();
if(!$query) return "";
$result = mysql_query($query) or die("MySQL Error : " . mysql_error() . "\n<br />In Query <code>$query</code>");
if(mysql_num_rows($result) == 1) { //If there is just one result, return it.
$arr = mysql_fetch_assoc($result);
if(count($arr) == 1) { //If there is just one result...
$item = array_values($arr);
return stripslashes($item[0]); // Creates the effect of 'getOne()'
} else {
foreach($arr as $key => $value) $arr[$key] = stripslashes($value);
return $arr; // Makes the 'query()' effect
}
} else {
$arr = array();
$primary_key = false;
while ($all = mysql_fetch_row($result)) {
if(count($all) == 1) array_push($arr,$all[0]);
elseif(count($all) == 2) {
if(!$primary_key) { //Happens only one time
$meta = mysql_fetch_field($result,0); //If the first element is a primary key, make
$primary_key = $meta->primary_key; // the result an associative array.
}
//Make it an Associative Array if there is a primary_key in the given data.
if($primary_key) $arr[$all[0]] = $all[1];
else break;
}
else break;
}
if($arr) {
//Do a stripslashes() on each element
foreach($arr as $key => $value) $arr[$key] = stripslashes($value);
return $arr; // For 'getAll()'
} else { //If nothing matches...
mysql_data_seek($result,0); //Reset the Query.
return $result; // This results in 'getSqlHandle()' effect
}
}
}
Re: [PHP + MySQL] Query Function Problem
ehh..
PHP Code:
<?
$db = ....
$query=$db->Execute("select bla from pla");
$query2 = $query ->fetchrow();
print_r($query2[0]);
?>
dont forget to set db!
half of credits to Daevius :)