PDA

View Full Version : sql stuff



VistaBoy
09-08-2008, 03:10 AM
okay i have tryed to make this for over 4 hours and still do not get how to do it so here i am

what i want to do is make a function that can make my sql stuff like this


$userq = select("*", "users", array('aid' => $this->cookies[aid], 'username' => $this->cookies[username], 'password' => $this->cookies[password], 'sesid' => $this->cookies[sid]));


$groupq = select("*", "groups", "group = '$this->user[group]'");

select(the fiealds or the *, what table , a array or the normal for WHERE);

thank you your help if you can give it.

Agnostic Bear
09-08-2008, 07:30 PM
I've made it so it's just array('field' => 'value') not normal, it'll save time in the long run if you ever need to add anything. This should be near enough what you're looking for, took me a few minutes to whip it up.


<?php
function selectSql( $what, $from, $where = null )
{
$what = ( $what === '*' ) ? '*' : '`' . $what . '`';
$query = 'SELECT ' . $what . ' FROM `' . $from . '` ';
if( is_array( $where ) )
{
foreach( $where as $key => $value )
{
$query .= '`' . $key . '` = \'' . $value . '\'';
}
}
return mysql_query( $query );
}

?>

You'll have to clean the input yourself.

VistaBoy
10-08-2008, 02:23 AM
Yo thank you but the bit i do not get is how to make array('field' => 'value','field2' => 'value2','field3' => 'value3') and that would be field = 'value' AND field2 = 'value2' AND field3 = 'value3' and so on.

VistaBoy
10-08-2008, 11:37 PM
Sorry for two post but would some thing like this do it?



<?php
function select( $type = '*', $from, $where = null )
{
$count = count($where);
$i = 1;
$query = 'SELECT ' . $what . ' FROM `' . $from . '` ';
if( is_array( $where ) )
{
foreach( $where as $key => $value )
{
$query .= ' `' . $key . '` = \'' . $value . '\'';
if($count > $i)
{
$query .= ' AND';
$i++;
}
}
}
else
{
die('error in sql missing array');
}
return mysql_query( $query );
}

?>

Agnostic Bear
11-08-2008, 10:53 PM
I've made it so it's just array('field' => 'value') not normal, it'll save time in the long run if you ever need to add anything. This should be near enough what you're looking for, took me a few minutes to whip it up.


<?php
function selectSql( $what, $from, $where = null )
{
$what = ( $what === '*' ) ? '*' : '`' . $what . '`';
$query = 'SELECT ' . $what . ' FROM `' . $from . '` ';
if( is_array( $where ) )
{
foreach( $where as $key => $value )
{
$query .= '`' . $key . '` = \'' . $value . '\'';
}
}
return mysql_query( $query );
}

?>You'll have to clean the input yourself.]

Oops sorry, forgot the AND part, but no that wouldn't do it, however this would:


<?php
function selectSql( $what, $from, $where = null )
{
$what = ( $what === '*' ) ? '*' : '`' . $what . '`';
$query = 'SELECT ' . $what . ' FROM `' . $from . '` ';
if( is_array( $where ) )
{
foreach( $where as $key => $value )
{
$query .= '`' . $key . '` = \'' . $value . '\' AND ';
}
$query = preg_replace( '#AND $#', '', $query );
}
return mysql_query( $query );
}

?>

VistaBoy
11-08-2008, 11:49 PM
Cool thanks as i want to learn from this how dose


$query = preg_replace( '#AND $#', '', $query );

like in how dose it know to remove the AND at the end??

Agnostic Bear
12-08-2008, 12:36 AM
Cool thanks as i want to learn from this how dose


$query = preg_replace( '#AND $#', '', $query );

like in how dose it know to remove the AND at the end??


the $ = end of string, so it will only remove it if it matches "AND " then the end of the string.

VistaBoy
13-08-2008, 11:11 AM
Umm ye i got an issue with it okay it only dose it with in classes funny?

okay this is my code


$getkeyq = $this->db->select('*', 'keys', array('keycode' => $keycode));
$getkeyf = $this->db->fetch_array($getkeyq);
$getscriptq = $this->db->select('*', 'scripts', array('script' => $script));
$getscriptf = $this->db->fetch_array($getscriptq);

if($this->db->num_rows($getkeyq) == '1' && $this->db->num_rows($getscriptq) == '1' ) {and if i input the right data it still saying its not right like it not finding any rows but there is but when i tack out the mysql_query bit in my data_layer and echo the $getkeyf to see what it shows it shows "Resource id #8" and not a prober query???

VistaBoy
13-08-2008, 01:14 PM
Sorry for two post but i fixed the error it was mt clean it was to go it sriped all the contents.

Want to hide these adverts? Register an account for free!