MySQL mysql_connect new_link

I want to share a database connection access problem I had last week, while working with a custom PHP script inside WordPress. I had created this WordPress Page Template containing some PHP code that needs access to the database. The problem was that the database connection for my custom script overwrote the WordPress database connection that was previously establish, causing certain parts of WordPress to not display properly.

It took me a while to figure out that it was the newer database connection of my custom PHP script that was causing the previously established WordPress database connection to disappear. Hence, certain parts of the WordPress page were not displayed. Little did I know, that the fix was quite simple. So, here’s a sample of my mysql_connect code. Prior to this line, I’ve already set the variables.

Mysql_connect

$db=mysql_connect($host,$username,$password);

The Fix

Simply add a fourth parameter called new_link and set it to TRUE.

$db=mysql_connect($host,$username,$password,TRUE);

What this does is basically telling mysql_connect to establish a new connection, while keeping the older mysql_connect connection around, in case we need to access it at a later time. It’s amazing how one little switch in a command can make a huge difference to this seemingly simple code. Anyways, adding a fourth parameter and setting it to TRUE was the solution.

CodeIgniter Form_Dropdown

I had a little problem using CodeIgniter’s form_dropdown the other day. Form_dropdown produces an output similar to HTML’s form select option. I was trying to retrieve data queried from a database and display the result in a form. Well, after several test and trials, I finally got the script to work. Here’s how I did it.

Form_Dropdown

The form_dropdown function typically has 3 options. The first option is the fieldname. The second option is the option data usually laid out in an array. The third is the selected data.

form_dropdown(‘name’, array(1?=>’one’,2?=>’two’,3?=>’three’),1);

This produces:

<select name=’name’>
<option value=”1? selected=”selected”>One</option>
<option value=”2?>Two</option>
<option value=”3?>Three</option>
</select>

CodeIgniter

In CodeIgniter, I have a simple script that stores bookmarks. I categorize my bookmarks using tags. When adding bookmarks, I call on the get_dropdown_tags function in Models to retrieve all the tags and display them in array that I can use with form_dropdown. Here are my CodeIgniter entries.

Controller:

$data['tags'] = $this-&gt;links_model-&gt;get_dropdown_tags();

Models:

function get_dropdown_tags() {
  $tags = $this-&gt;db-&gt;query('select distinct tag from links');
  $dropdowns = $tags-&gt;result();
  foreach ($dropdowns as $dropdown) {
    $dropdownlist[$dropdown-&gt;tag] = $dropdown-&gt;tag;
  }
  $finaldropdown = $dropdownlist;
  return $finaldropdown;
}

Views:

In views, I’m simply calling the form_dropdown function using the $tags array passed on by the controller. It’s a pretty neat way to recall data from a database and outputting them in form_dropdown.

<?=form_dropdown(‘tag’,$tags);?>

Finally, a dropdown list that works.

CodeIgniter: Two Ways of Writing Arrays

There are two ways of writing arrays in PHP. We will use CodeIgniter in this example. Since CodeIgniter is a MVC framework, we will look at code in models. Assuming that a form is being submitted, and data is saved to the database. In this example, we will use a function called ‘add_entry.’

The add entry function is empty at the moment.

function add_entry() {
}

We will now add post data to our function. In this example, we will use the ‘url’ and ‘anchor’ fields. To sanitize, we set both values to TRUE. We assign it to a variable called $data in an array.

function add_entry() {
$data->url = $this->input->post(‘url’,true);
$data->anchor = $this->input->post(‘anchor’,true);
}

We will now insert data to a database table called ‘bookmarks.’

function add_entry() {
$data->url = $this->input->post(‘url’,true);
$data->anchor = $this->input->post(‘anchor’,true);
$this->db->insert(‘bookmarks’, $data);
}

The other way of writing an array is this:

function add_entry() {
$data = array(
‘url’ => $this->input->post(‘url’,true),
‘anchor’ => $this->input->post(‘anchor’,true));
$this->db->insert(‘bookmarks’, $data);
}

Essentially, both are the same. Somehow, I prefer the second. It seems cleaner somehow. It seems like, I only have to deal with a single variable called $data. What’s your preferred method?