I have some query call and I want to replace it into one but I don't know it is possible. My database looks like:
My database calls:
$minbet = $mysql->query('SELECT `value` FROM `jackpot1_info` WHERE `name`="minbet"')->fetch_assoc();
$minbet = $minbet['value'];
$maxbet = $mysql->query('SELECT `value` FROM `jackpot1_info` WHERE `name`="maxbet"')->fetch_assoc();
$maxbet = $maxbet['value'];
$maxitems = $mysql->query('SELECT `value` FROM `jackpot1_info` WHERE `name`="maxitems"')->fetch_assoc();
$maxitems = $maxitems['value'];
$maxitemsinpot = $mysql->query('SELECT `value` FROM `jackpot1_info` WHERE `name`="maxitemsinpot"')->fetch_assoc();
$maxitemsinpot = $maxitemsinpot['value'];
It's simple, just select name and value in a single query and you will have each name and value pair in a separate row:
$select = $mysql->query('
SELECT `name`, `value`
FROM `jackpot1_info`
WHERE `name` IN ("minbet","maxbet","maxitemsinpot","maxitems")
');
while ($result = $mysql->fetch_assoc())
{
var_dump($result);
}
Output would look like:
array(2) {
'name' =>
string(12884901894) "minbet"
'value' =>
int(123)
}
array(2) {
'name' =>
string(12884901894) "maxbet"
'value' =>
int(456)
}
...
I'm sure you can do the rest.
Use IN instead of = for one query.
$select = $mysql->query('SELECT name,value FROM `jackpot1_info` WHERE `name` IN ("minbet","maxbet","maxitemsinpot","maxitems")');
$result = $mysql->fetch_assoc();
Related
i have problem using active record, if i have active record like this
$data_array = array(
'Code Employee' => 'AC01',
'Status Employee' => 'Y'
);
$this->db->where($data_array);
$this->db->get('masteremp');
echo $this->db->last_query();
//Output query will be
SELECT * FROM masteremp WHERE `Code` `Employee` = 'AC01' AND `Status` `Employee` = 'Y'
i try edit DB_query_builder and i add this on line 2430
if(!empty($conditions)){
for($index = 0; $index < sizeof($conditions); $index++){
if (strpos($conditions[$index], '` `') !== false) {
$conditions[$index] = str_replace('` `', ' ', $conditions[$index]);
}
}
}
do u have method for field with space? i think my method is not efficiency.. thanks..
IN codeigniter you can escape values and identifiers.Try once like this..
Replace
$this->db->where($data_array);
With
$this->db->where($data_array,TRUE);//second parameter as TRUE
Then
//Output query will be
SELECT * FROM masteremp WHERE `Code Employee` = 'AC01' AND `Status Employee` = 'Y'
I have 2 tables structured like this:
CREATE TABLE `exp_ws_gk_text` (
`WGT_RID` INT(9) NOT NULL AUTO_INCREMENT,
`WGT_PRG_CODE` VARCHAR(5) NOT NULL,
`WGT_TEXT` VARCHAR(4000) NOT NULL,
INDEX `PrimaryKey` (`WGT_RID`)
)
CREATE TABLE `exp_ws_gk_state` (
`WGS_RID` INT(9) NOT NULL AUTO_INCREMENT,
`WGS_TYPE` INT(9) NOT NULL,
`WGS_STATE` VARCHAR(3) NOT NULL,
`WGS_WGT_RID` INT(9) NOT NULL,
INDEX `PrimaryKey` (`WGS_RID`),
INDEX `SecondaryKey` (`WGS_TYPE`, `WGS_STATE`)
)
This is how I query the data to be used in a site:
SELECT a.wgt_rid id, a.wgt_text text, a.wgt_prg_code prg_code,
b.wgs_state state, b.wgs_type type,
FROM exp_ws_gk_text a, exp_ws_gk_state b
WHERE a.wgt_rid = b.wgs_wgt_rid
I present the record with all the data returned. Now, when I want to edit or append a record, how would I save the data correctly if the data got sent from a form with these parameters:
$id = intval($_REQUEST['id']);
$prg_code = $_REQUEST['prg_code'];
$state = $_REQUEST['state'];
$crs_type = $_REQUEST['type'];
$text = $_REQUEST['text'];
An insert:
$sql = "BEGIN; ".
" INSERT INTO exp_ws_gk_text (WGT_PRG_CODE, WGT_TEXT) ".
" VALUES('".$prg_code."', '".$text."'); " .
" INSERT INTO exp_ws_gk_state (WGS_STATE, WGS_TYPE, WGS_WGT_RID) ".
" VALUES('".$state."', ".$course_type.", LAST_INSERT_ID()); ".
"COMMIT;";
$mysql_query($sql);
$sql = "SELECT WGS_WGT_RID id FROM exp_ws_gk_text WHERE WGS_RID = ".mysql_insert_id();
$result = mysql_query($sql);
$r = mysql_fetch_array($result, MYSQL_ASSOC);
echo json_encode(array(
'id' => $r["id"],
'prg_code' => $prg_code,
'state' => $state,
'type' => $type,
'text' => $text
));
A delete:
DELETE a, b FROM exp_ws_gk_text a
JOIN exp_ws_gk_state b ON a.WGT_RID = b.wgs_wgt_rid
WHERE a.WGT_RID = :id
$sql = "UPDATE exp_ws_gk_text a, exp_ws_gk_state b
SET a.WGT_PRG_CODE = ".$prg_code.", a.WGT_TEXT = ".$text.", b.WGS_STATE = ".$state.", b.WGS_TYPE = ".$crs_type."
WHERE
a.WGT_RID = ".$id." AND a.WGT_RID = b.wgs_wgt_rid";
I need to access data from 2 different rows returned in the same array
Here is a picture
I need to access first_name and last_name
Here is my script
<?php
ini_set('display_errors', 1);
include_once('db.php'); // database class
$dbase['host'] = 'asdf';
$dbase['user'] = 'asdf';
$dbase['pass'] = 'asdf';
$dbase['name'] = 'asdf';
$db = new DB($dbase);
// SELECT `meta_value` FROM `wp_usermeta` WHERE `meta_key`='password'
$result = $db->query("SELECT * FROM `wp_usermeta` WHERE `meta_key`='user_email'");
while ($row = $result->fetch_array()) {
echo $row['meta_value'];
}
?>
Any help on this issue would be appreciated greatly!
Try this query..
SELECT
wp1.meta_value AS first_name,
wp2.meta_value AS last_name
FROM
wp_usermeta wp1
INNER JOIN
wp_usermeta wp2
ON ( wp1.user_id = wp2.user_id )
WHERE 1
AND wp1.meta_key = "first_name"
AND wp2.meta_key = "last_name";
GROUP BY wp1.user_id;
You're already looping over these rows, just inspect the meta_key.
$fname="";
$lname="";
while ($row = $result->fetch_array())
{
if ($row['meta_key'] == "first_name")
{
$fname = $row['meta_value'];
}
else if ($row['meta_key'] == "last_name")
{
$lname = $row['meta_value'];
}
}
echo $fname . " " . $lname;
lastly, your sql is incorrect:
SELECT * FROM `wp_usermeta` WHERE `user_id` IN (SELECT `user_id` FROM `wp_usermeta` WHERE `meta_key` = 'user_email' AND `meta_value` = "$user_email_passed_in")
In this case the MySQL query that you're doing is wrong.
It might be
$result = $db->query("SELECT * FROM `wp_usermeta` WHERE (`meta_key`='first_name' OR `meta_key`='last_name')");
In this case all rows matching 'first_name' OR 'last_name' in 'meta_key' field will be returned.
I think you'll have to distinguish these fields using also user_id field as discriminant.
Best regards
When I run the second query to select departments it is returning my list of departments which is like 20 records, and then return thousands of blank enteries to the page freezes in a browser what did I do wrong?
mysql_select_db("ita", $con);
$results = mysql_query("SELECT * FROM `Colleges`");
$colleges = array();
while($row = mysql_fetch_assoc($results))
{
$colleges[] = array("Name" => $row['Name'], "Value" => $row['Value'], "ID" => $row['CollegeID']);
}
$collegecount = count($colleges);
$depts = array();
$result1 = mysql_query("SELECT * FROM `Departments` WHERE uid = `$uid`");
while($row = mysql_fetch_assoc($result1))
{
$depts = array("Name" => $row['Name'], "Value" => $row['Value'], "ID" => $row['CollegeID']);
}
$result = mysql_query("SELECT * FROM `users` WHERE wmuid = '$uid'");
while($row = mysql_fetch_assoc($result)){
//POPULATE FORM FIELDS FROM DB
You have backticks around your $uid. I'm not sure what exactly that will do since I doubt you have a column with the same name as whatevers in $uid, but it's probably going to cause some weird undefined behavior. Change your ` to ' Like:
SELECT * FROM `Departments` WHERE `uid` = '$uid'
Edit
Or if the column uid stores a numerical type remove the quotes altogether like:
SELECT * FROM `Departments` WHERE `uid` = $uid
need to return an array like this for example:
array(30 => 'Mercedes Benz 310 ',26 => 'Lamborghini Murcielago')
I have a database set up something like this:
CREATE TABLE cars (
id bigint(20) NOT NULL auto_increment,
`car_name` tinyint(2) NOT NULL default '0',
owner varchar(20) NOT NULL default ''
PRIMARY KEY (id)
) ENGINE=MyISAM;
The id need to be the array key.
So I tried to use foreach, but I have still not quite understood how it works.
$q = "select `id`, `car_name` from `cars` where `owner`='$username'";
$result = $conn->query($q);
unset($q);
if( !$result){
return array(0 => 'error');
}
$garage = $result->fetch_assoc();
$car_id = $garage["id"];
$car_name = $garage["car_name"];
foreach( $car_name as $key => $car_id ){
...
}
You aren't far off. Something like this should give you the kind of array you're looking for.
$q = "select `id`, `car_name` from `cars` where `owner`='$username'";
$result = $conn->query($q);
unset($q);
if( !$result){
return array(0 => 'error');
}
while($row = mysql_fetch_array($result)){
$garage[$row['id']] = $row['car_name'];
}
return $garage;