Forum Moderators: coopster

Message Too Old, No Replies

two pieces of exasperation

PHP INI and get last insert id()

         

Baruch Menachem

4:52 pm on May 17, 2009 (gmt 0)

10+ Year Member



I finally got PHP to run on my mac. (not serving to the outside world, not with my code!) Which is cool.

Right now ini has errors off. I want to go in and change the logging to error logging on. PHP info shows that php ini is in the /etc path, but when I look at the etc path, I can't find the PHP ini file

The other thing is, I want to do is get the number of the last insert i did. I have a little library thing I am doing. right now the logic is
get author id from a select list,
get new book name from user input (html entities and mysql string cleaning first)
Insert new book
get last insert id
insert last insert id and author id into relationship table

When I try to print out the get last insert id from my php script it gives me resource id =3. That is not what I am looking for. I am looking for last insertion was 155 or somehting

How do I get last insert id() to give me the number, rather than the resourse.

Thanks

bkeep

5:06 pm on May 17, 2009 (gmt 0)

10+ Year Member



To get the last inserted id [us3.php.net...]


mysql_query(INSERT....blah blah);

$lastid = mysql_insert_id();
print "$lastid";

Note: Because mysql_insert_id() acts on the last performed query, be sure to call mysql_insert_id() immediately after the query that generates the value.

You can turn on error reporting in your PHP scriipt


ini_set('display_errors', 1);
error_reporting(E_ALL¦E_STRICT);

Regards,
Brandon

Baruch Menachem

6:08 pm on May 17, 2009 (gmt 0)

10+ Year Member



Thanks. It worked great. I was trying all kinds of really not good ideas over and over. The easy way never occurred to me at all.

The next step is to create a loop to install multiple authors into the relationship table. That should entertain me for a while.

As for the in stuff, I put it in my first config file. I can comment it out there pretty easily. Thanks for the help

Baruch Menachem

7:25 am on May 18, 2009 (gmt 0)

10+ Year Member



Another puzzle for me: Use of the distinct keyword

I have a data base of books. many books have multiple authors. I want to list my books only once, but show both authors. If an author has multiple books, I want to show all the books for that author, and all authors, once for each book.
My query looks like this
"select books.title as title, concat(authors.efname,' ',authors.elname) as scribe, Topics.topic as subject from books inner join bookswritten using(book_id) inner join authors using(writer_id)inner join Topics using(Topic_id) where title like '%$profound%' order by authors.elname, books.title";

Is there any place I could put a distinct in the search that would make it so I get only one listing for all the authors?

Also, since php goes through the list of books one by one, would it be better to effectivly create a bunch of cards, one card per book?

dreamcatcher

8:14 am on May 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use GROUP BY.

group by authors.elname order by authors.elname, books.title

dc

Baruch Menachem

2:40 pm on May 18, 2009 (gmt 0)

10+ Year Member



Could I group by scribe? I have one set of books written by a married couple.

dreamcatcher

3:11 pm on May 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can group by anything. If you group a field it will only return one instance of any data in that field.

dc

Baruch Menachem

7:07 pm on May 18, 2009 (gmt 0)

10+ Year Member



Not what I want.

Of course, it looks like an issue of bad data design. I only want one book to show up, so I should do group by title then. That means I will get only one of the authors, I guess.

Baruch Menachem

7:12 pm on May 18, 2009 (gmt 0)

10+ Year Member



Tried it out, it does mostly what I want, I get only one instance of the book, and only one of the authors. Given the limits of the design, that is the best I can hope for right now.