Codeigniter doesn't let me execute query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Codeigniter doesn't let me execute query

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Codeigniter doesn't let me execute query

Post 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();
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Codeigniter doesn't let me execute query

Post 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...
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Codeigniter doesn't let me execute query

Post by VladSun »

Code: Select all

$vrs_query="SELECT * FROM cars";
$vrs_result=mysql_query($vrs_query);
die(mysql_error());
?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Codeigniter doesn't let me execute query

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Codeigniter doesn't let me execute query

Post 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;
}
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Codeigniter doesn't let me execute query

Post 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:
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Codeigniter doesn't let me execute query

Post 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 :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Codeigniter doesn't let me execute query

Post 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...
Post Reply