3 Replies - 295 Views - Last Post: 11 June 2012 - 09:26 AM Rate Topic: -----

#1 meany  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 07-June 12

mysql query question

Posted 11 June 2012 - 08:45 AM

I am very new to php, and I am trying to follow a tutorial into creating a blog using php and mysql.

I have been following this tutorial relatively easily until I came across this block of code:
$query = mysql_query("SELECT first_name, last_name FROM people WHERE id = " . $inAuthorId);
$row = mysql_fetch_assoc($query);
$this->author = $row["first_name"] . " " . $row["last_name"];


I remember that I should stay away from mysql_ functions, as recommended by the php.net site.
However, I just would like to understand the first line of the code.
From my understanding, I am taking the first_name and last_name data from the table "people". However, I do not understand the
"WHERE id = " . $inAuthorId part.

Also, I would appreciate if I could see the block of codes in PDO form.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: mysql query question

#2 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: mysql query question

Posted 11 June 2012 - 09:23 AM

That query will, as you said, query the person table and retrieve the first_name and last_name fields. Everything after WHERE in an SQL statement is considered the WHERE Clause. In this specific WHERE Clause, you're using the id field to narrow down your search in the db table.

In this example, $inAuthorId was probably already set to something, most likely a number, but doesn't have to be. Now in the mysql_query function, you're concatenating the first part of the query and this variable. So if the variable's value was 2 your query would look like:

SELECT first_name, last_name FROM people WHERE id = 2

So this query will return the first_name and last_name of the row in the people table which has id equal to 2.

If I were doing this in PDO it'd look something like:


$sql = "SELECT first_name, last_name FROM people WHERE id = :theId";
		$result = self::$pdo->prepare($sql);
		$params = array(
			'theId' => $inAuthorId
		);
		$result->execute($params); 
		$result->bindColumn('first_name', $firstName);
$result->bindColumn('last_name', $lastName);
		$result->fetch(\PDO::FETCH_BOUND);



For more info on PDO: http://php.net/manual/en/book.pdo.php

This post has been edited by eZACKe: 11 June 2012 - 09:24 AM

Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2881
  • View blog
  • Posts: 9,958
  • Joined: 08-August 08

Re: mysql query question

Posted 11 June 2012 - 09:23 AM

Think of WHERE as a conditional. The query will only return values that match the conditions in it. In this case, the id must contain the same value as $inAuthorId.

PDO:
http://www.dreaminco...duction-to-pdo/
Was This Post Helpful? 1
  • +
  • -

#4 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 169
  • View blog
  • Posts: 606
  • Joined: 12-October 09

Re: mysql query question

Posted 11 June 2012 - 09:26 AM

The WHERE clause filters the results.

Say you just selected first_name & last_name from people, this might return a load of rows like:

first_name | last_name
Bob|Smith
Jim|Smith
Frank|Lampard
John|Terry
etc..

By using a WHERE clause you can filter it down to exclude all rows that don't meet the condition in your clause.

E.g. if you put "WHERE last_name = 'Smith'"

It would get filter out all the rows that don't have 'Smith' as the last_name in your results.

So WHERE id = $inAuthorId is doing the same thing. It's using whatever value has been put into the $inAuthorId variable beforehand in your script, and filternig out all the rows that don't have that value in their "id" field/column.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1