why only first letter of column coming from mysql database in php? - mysql

my database is
CREATE TABLE `mytable` (
`id` int(10) AUTO_INCREMENT,
`name` varchar(50),
`description` varchar(255),
`visible` varchar(10),
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET utf8;
and php code is
$display = query("SELECT * FROM mytable ORDER BY id ASC");
foreach($display as $row) {
echo $row['id'];
echo $row['name'];
echo $row['description'];
}
what is wrong in my code ? data is not displayed and when it display only first letter of field is displayed. All the configuration and connection settings are fine. Pls help

var_dump($row);

Looks like it's the same pitfall I fell into once :)
Don't you have only one row in your table?
If this query() function is kinda too smart one, determining return type by returned data, it can be a reason.
Make it return nested array, not one row.
And define result type explicitly, not automatically based on returned data. Add a parameter to indicate what kind of result you want.
However, such a function is very good approach. Only a few people have an idea of devising such a function instead of constant hassle with numerous API functions.
but if you expect just one row, then
$row = query("SELECT * FROM mytable ORDER BY id ASC");
echo $row['id'];
echo $row['name'];
echo $row['description'];

I had the this same problem using CodeIgniter.
The problem for me was that I was trying to echo out the array value using an associative key when it was just a numerically indexed array. I'm surprised it worked at all.
So my array was built like so:
$result = $this->ci->db->get('role');
if($result->num_rows() > 0)
{
foreach($result->result_array() as $row)
{
$roles[$row['id']] = $row['name'];
}
}
When I tried to use the array like so:
foreach($roles as $role)
{
echo $role['name'];
}
It would only print out the first letter of each value.
When I changed the array to be built like this:
$roles[$row['id']] = array('name' => $row['name']);
Everything worked as expected.
Hope this helps someone.

Related

Convert datetimes from MySQL table to ISO8601 to create JSON feed in WordPress for use with FullCalendar

I've written an action in WordPress that grabs the rows from a table and encodes them in JSON format, so I can use them with the FullCalendar javascript event calendar.
The date fields from the table need to be formatted ISO8601.
In other words, when the DB renders the date/time: 2017-08-06 10:22:20, I need it converted after the query to: 2017-08-06T10:22:20 for the date fields in the query.
I'm not concerned about timezone offsets.
My function:
add_action( 'getmyevents', 'get_my_events' );
function get_my_events( $atts = [], $content = null ) {
// Use WordPress database functions
global $wpdb;
// List of events will be stored in JSON format
$json = array();
// Query retrieves list of events
$mytable = $wpdb->prefix . "my_events";
$myids = $wpdb->get_results("SELECT * FROM " . $mytable );
// sending the encoded result to success page
echo json_encode( $myids, JSON_UNESCAPED_SLASHES );
// return JSON
return $json;
}
Can someone give me a quick, direct way to convert the datetime strings in the query to ISO8601?
Maybe you can try something like this.
Although I don't know the name of your column. Uncomment the print_r to get the column name.
foreach ($myids as $key => $row) {
// print_r($row);
$date_reformatted = strtotime($row->date_col);
$myids[$key]->date_col = date( 'c', $date_reformatted );
}
It isn't the ideal answer I was looking for, but I did come up with a working solution. Mark's suggestion about filtering during the query gave me the clue I needed for it.
add_action( 'getmyevents', 'get_my_events' );
function get_my_events( $atts = [], $content = null ) {
global $wpdb;
// Values sent via ajax to calendar from my_events table
// List of events
$json = array();
// Query that retrieves events
$mytable = $wpdb->prefix . "my_events";
$myids = $wpdb->get_results( 'SELECT id, title, url, DATE_FORMAT( start, "%Y-%m-%d\T%H:%i:%s" ) as start, DATE_FORMAT( end, "%Y-%m-%d\T%H:%i:%s" ) as end, allDay FROM ' . $mytable );
// sending the encoded result to success page
echo json_encode( $myids, JSON_UNESCAPED_SLASHES );
// return JSON
return $json;
}
However, if someone else can come up with an answer that doesn't require me to specify columns by name, that would be great. Even better would be not formatting within the query at all, but rather formatting afterward. I always like to minimize processor use by MySQL as much as possible.

I'm getting the wrong data when using doctrine in symfony

I'm trying to build a SQL query using doctrine. Here's my code snippet:
/**
* #Route("/db", name="user_skill_testing")
*/
public function dbTest()
{
//all skills for user
$userName = "Jelle";
$user = $this->getDoctrine()
->getRepository('AppBundle:User')
->findOneByFirstname($userName);
echo "userId: ".$user->getId()."<br />";
$userSkills = $this->getDoctrine()
->getRepository('AppBundle:Userskill')->findById($user->getId());
$proficiencies = array();
foreach ($userSkills as $userSkill) {
array_push($proficiencies, $userSkill);
echo $userSkill->getId();
echo "-";
echo $userSkill->getProficiency()->getId();
echo "<br />";
}
var_dump($userSkills);
$html = "<html><body>".$user->getFirstname()."<br /><br />"."</body></html>";
return new Response($html);
}
It returns the following webpage(screenshot):
When I look at the queries it ran...:
...and rerun them...:
...I get a very different result. :(
I have no idea why, can anyone help me out?
thank you!
Edit: using this code to build the query reproduces the same result.
The problem lies with your PK on UserSkill. When doctrine Hydrates, it will assume entities with the same primary key, are the same entity, ignoring rows with the same PK if one has already been hydrated.
Since your Userskill::$id is not unique, only the first one will be hydrated, subsequent rows with the same id, will get a reference to the same entity.
To solve this, you need to create a compound key, consisting of the id and the proficiencyId.
In you use-case though, this will make things unpractical. So I would just replace your manual id on Userskill with an actual one-to-many relation from Userto Userskill...
Having MySql generate the many-to-many relationship helped doctrine generate the correct entities.

Set a value to null, when calling Zend_Db::update() and insert()

My question is the exact same as How to Set a Value to NULL when using Zend_Db
However, the solution given in that question is not working for me. My code looks like the following. I call updateOper on the Model class when update is clicked on the front end. Inside updateOper, I call another function trimData() where I first trim all whitespace and then I also check that if some of the fields are coming in empty or '' I want to set them to default values or NULL values. Therefore I am using new Zend_db_expr('null') and new Zend_db_expr('default') .
The code is as follows:
private function trimData(&$data ) {
//Trim whitespace characters from incoming data.
foreach($data as $key => $val)
{
$data[$key] = trim($val);
if($data['notes'] == '') {
error_log("set notes to null/default value");
$data['notes'] = new Zend_db_expr('DEFAULT');
}
}
}
public function updateOper($data, $id)
{
$result = 0;
$tData = $this->trimData($data);
error_log("going to add data as ".print_r($data, true));
$where = $this->getAdapter()->quoteInto('id = ?', $id);
$result = $this->update($data, $where);
return $result;
}
The error_log statement prints the $data array as follows:
[id] => 10
[name] => alpha
[notes] => DEFAULT
As a result, the notes column has value ='DEFAULT' instead of picking the default value given in the table definition.
I have been trying to figure out what is wrong, but have not been able to find a solution.
I would really appreciate your help.
Thanks so much!
Your $data['notes'] is being changed to the __toString() value of the Zend_Db_Expr instead of preserving the actual object.
Maybe the reference is clogging things up. Else you may need to move the expression declaration into the actual update query.

Selecting MYSQL rows with same field names and adding a prefix

I'm trying to make a mysql query to select several tables and LEFT join them, however they all have same columns names 'user' etc. I want to rename all the fields in this manner . so I tried the following query
SELECT mod_backup_accounts . * AS account . * , mod_backup_subscriptions . *
FROM `mod_backup_accounts`
LEFT JOIN `mod_backup_subscriptions` ON `mod_backup_accounts`.subscription_id = `mod_backup_subscriptions`.package_id
However the mod_backup_accounts . * AS account . * makes it fail, is there a way to do this? so it would be names as account.
You cannot supply a shorthand to alias columns you must do it explicitly for each column name. In general anyway, it is typically recommended to name all columns explicitly in the SELECT list rather than using SELECT *, since it allows you to deterministically specify the column order, and protects you against accidentally pulling in a large BLOB later on if one ever gets added to the table ( or any other schema changes ).
SELECT
mod_backup_accounts.user AS account_user,
mod_backup_subscriptions.user AS subscription_user,
...
...
FROM
mod_backup_accounts
LEFT JOIN `mod_backup_subscriptions` ON `mod_backup_accounts`.subscription_id = `mod_backup_subscriptions`.package_id
I totally understand your problem about duplicated field names.
I needed that too until I coded my own function to solve it. If you are using PHP you can use it, or code yours in the language you are using for if you have this following facilities.
The trick here is that mysql_field_table() returns the table name and mysql_field_name() the field for each row in the result if it's got with mysql_num_fields() so you can mix them in a new array.
You can also modify the function to only add the "column." prefix when the field name is duplicated.
Regards,
function mysql_rows_with_columns($query) {
$result = mysql_query($query);
if (!$result) return false; // mysql_error() could be used outside
$fields = mysql_num_fields($result);
$rows = array();
while ($row = mysql_fetch_row($result)) {
$newRow = array();
for ($i=0; $i<$fields; $i++) {
$table = mysql_field_table($result, $i);
$name = mysql_field_name($result, $i);
$newRow[$table . "." . $name] = $row[$i];
}
$rows[] = $newRow;
}
mysql_free_result($result);
return $rows;
}

Adding 2 drop lists to associated table

I am having an issue trying to add the records using 2 drop lists.
I have a table called Urls which holds the details of url. I have a table called category populates a drop list, I have another table called publishers which populates another drop list.
$query = 'INSERT INTO url_associations (url_id, url_category_id, approved, url_publisher_id) VALUES ';
foreach ($_POST['types'] as $v){
$query .= "($uid, $v, 'Y', $k), ";
}
$query = substr ($query, 0, -2); // Chop off the last comma and space.
$result = #mysql_query ($query); // Run the query.
if (mysql_affected_rows() == count($_POST['types'])) { // Query ran OK.
echo '<p><b>Thank you for your submission!</b></p>';
$_POST = array(); // Reset values.
} else { // If second query did not run OK.
The code above allows me to addd data using the categories drop list but when I try to add the url_publisher_id as 'posters' as $k I keep getting errors in my parsing. If anyone can understand what I am trying to achieve your help would be welcomed
If the value of your $k variable is anything other than an integer or float you'll get an error because it needs quotes around it when you're building the SQL INSERT statement:
$query .= "($uid, $v, 'Y', '$k'), ";
Note: There are some major security problems in your example. If you put user input from $_POST into your SQL without escaping it you're giving the user the ability to run whatever SQL commands they want to run on your database.
I have added an extra array foreach ($_POST[posters] as $k)
//so it reads
'foreach ($_POST[types] as $v)
foreach ($_POST[posters] as $k) {`
and it has executed perfectly.
Thanks for your help.
Sean