Hello guys,

I am trying to get from the database the titles of nodes of a certain type and print them. My function looks like this:

function mymodule_get_titles() {
$mytype = 'mtype';
$sql = 'SELECT title FROM node WHERE type = %s';
$result = db_query($sql, $mytype);
while ($keep_coming = db_fetch_array($result)) {
$titles[] = $keep_coming;
$out .= sprintf ($titles);
}
return $out;
}

What I get is an error:

user warning: Unknown column 'mtype' in 'where clause' query: SELECT title FROM node WHERE type = mtype

Can you please help?

Comments

sunnydeveloper’s picture

You need quotes around your string:

$sql ="SELECT title FROM node WHERE type = '%s' ";

or

$sql ='SELECT title FROM node WHERE type = \'%s\' ';

steven9’s picture

Thank you so much for the reply tiptoes. I tried both of your suggestions and I don't get the error anymore but instead of getting the titles my function prints:
"ArrayArrayArray"

Any other suggestions?

sunnydeveloper’s picture

No problem at all,

You are using db_fetch_array() to retrieve your records, so each record is being returned as such.

So (off the top of myhead, you'll need to test:

echo $keep_coming['title'] gives you the actual value.

Or you can use db_fetch_object, in which case it would be

echo $keep_coming->title;

You can always use var_dump($keep_coming) to see the contents of the array, and then play around until you can echo the value.

steven9’s picture

Thank you so much, tiptoes. Now it works. What I did was:
$out .= $keep_coming['title'];

You saved my day!

Vc Developer’s picture

I basically doing the same but I keep getting a "Resource ID #" in the return $result. I run the same query in phpMyAdmin with success. What am I doing wrong?

function mymodule_get_titles() {
$out = "Error!...";
$mytype = 'wordpage';
$sql = "SELECT title FROM main_node WHERE type = '%s'";
$result = db_query($sql, $mytype);
echo $result .'
';
while ($keep_coming = db_fetch_object($result)) {
$out .= $keep_coming->title;
}
return $out;
}

jaypan’s picture

$result is a mysql result - you need to run db_fetch_array() or db_fetch_object() on it before the data is available. You have done this further down in your code, so I'm not sure exactly what the problem is.

Contact me to contract me for D7 -> D10/11 migrations.

Vc Developer’s picture

I'm reading the API and it says,

Return value

A database query result resource, or FALSE if the query was not executed correctly.

So why is my while loop failing? I did a search and all the examples are no different. Does this resource comes back as a "Resouce Id #352"? and the db_fetch_object uses this to read?

.............. I was able to do this with success:
$result = db_query('SELECT name FROM {users} WHERE uid = %d', 1);
print $result->name .'
';

So this means I'm not doing something right to obtain node's?

Vc Developer’s picture

I think my nodes I imported didn't import correctly because the data I see from phpmyAdmin are different from what I'm getting when I run the sql function. My types are showing up with the original names I changed during import. I exported a "Story" and attempt to change the variables before import, and another I imported from another site, but I did want the same variables and changed them also.

I'll just start over again and make sure I changed all the variables!