Page 1 of 1

Codeigniter doesn't let me execute query

Posted: Mon Jul 06, 2009 8:55 am
by Sindarin
I was assigned to setup a new page in a site using the codeigniter framework however because I don't know how to use it and also because the code is badly messed up I wanted to inject my own coding ways.
I am trying to do a simple query in the database (not using the framework way).

first I include my db script to connect,

Code: Select all

<?php
 
/* 
 
DATABASE CONNECT
 
*/
 
$db_host = "localhost";
$db_username = "xxx";
$db_password = "xxx";
$db_database = "xxx";
 
$db_connection = mysql_connect($db_host,$db_username,$db_password);
 
if (!$db_connection)
  {
  die("<font color='red'>Error</font>: Could not connect to database: " . mysql_error());
  }
 
mysql_query("SET NAMES 'utf8'");
 
$db_database=mysql_select_db($db_database, $db_connection);
 
if (!$db_database)
  {
  die("<font color='red'>Error</font>: Could not connect to the particular database: " . mysql_error());
  }
 
?>
Then in header.php

Code: Select all

$vrs_query="SELECT * FROM cars";
$vrs_result=mysql_query($vrs_query);
    
while ($row = mysql_fetch_row($vrs_result))
{
$get_title=$row['cars_title'];
echo $get_title;
$site_title=$site_title." | ".$get_title;
}
I get the warning:
A PHP Error was encountered

Severity: Warning

Message: mysql_fetch_row(): supplied argument is not a valid MySQL result resource

Filename: views/header.php
Any help with this?

Re: Codeigniter doesn't let me execute query

Posted: Mon Jul 06, 2009 8:59 am
by VladSun
I don't think it's CI related in any way.

Let's see what is happening ;)

Code: Select all

$vrs_query="SELECT * FROM cars";
$vrs_result=mysql_query($vrs_query);
echo mysql_error();

Re: Codeigniter doesn't let me execute query

Posted: Tue Jul 07, 2009 6:13 am
by Sindarin
The same thing happens, Code Igniter displays all my errors with error_reporting(E_ALL); I can't see the true error, I just get the generic error message...

Re: Codeigniter doesn't let me execute query

Posted: Tue Jul 07, 2009 6:20 am
by VladSun

Code: Select all

$vrs_query="SELECT * FROM cars";
$vrs_result=mysql_query($vrs_query);
die(mysql_error());
?

Re: Codeigniter doesn't let me execute query

Posted: Tue Jul 07, 2009 6:34 am
by Sindarin
That made my page white, no error.

However this worked, meaning no error occured so I am gonna go and guess they need mysql queries all lowercase,

Code: Select all

$vrs_query="select * from cars where cars_id=1";
$vrs_result=mysql_query($vrs_query);
    
while ($row = mysql_fetch_row($vrs_result))
{
$get_title=$row['cars_title'];
echo $get_title;
$site_title=$site_title." | ".$get_title;
}

Now I get:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: cars_title

Filename: views/header.php
Great, now I'll have to find out how to initialize arrays in Codeigniter or something like that I guess.

Re: Codeigniter doesn't let me execute query

Posted: Tue Jul 07, 2009 6:42 am
by VladSun
It's the usual code again - nothing to do with CI...

And if you are going to work with CI, you'd better do it the CI way:

Code: Select all

$query = $this->db->query('SELECT * FROM cars');
 
foreach ($query->result() as $row)
{
    echo $row->cars_title;
}
 
echo 'Total Results: ' . $query->num_rows();
or

Code: Select all

$query = $this->db->get('cars');
 
foreach ($query->result() as $row)
{
    echo $row->cars_title;
}

Re: Codeigniter doesn't let me execute query

Posted: Tue Jul 07, 2009 6:49 am
by Sindarin
Oh well this seems to work thanks.

But eww, isn't there a way to do it a more non-codeigniter way? :roll:

Re: Codeigniter doesn't let me execute query

Posted: Tue Jul 07, 2009 7:13 am
by VladSun
Sindarin wrote:But eww, isn't there a way to do it a more non-codeigniter way? :roll:
sure, you can - just fix your code ;)
mysql_fetch_row — Get a result row as an enumerated array
Description
array mysql_fetch_row ( resource $result )

Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.
You can, but I would advice you not to do it - I suppose that the rest of the project is using actively the CI framework, so mixing your code with its code might be a PITA in the future.
Just imagine that the owners decide to move to PostgreSQL instead of MySQL :)

Re: Codeigniter doesn't let me execute query

Posted: Tue Jul 07, 2009 7:32 am
by Sindarin
Actually the site is really BAD coded, I've never seen such bad coding my entire webdev career. 40% of the site is codeigniter, 20% is procedural coding in notepad and rest is Dreamweaver mess...