MySQL - Find Row Value Based on Another Value in Same Row
Posted October 5th 2013, 12:30pm
Hello Dion Designs,

For a MOD that I am attempting to write for phpBB, I need to know how to find a value in a row of a MySQL database table based off of another row in the table. For example, say that this is my table.

Row_Id | Name | Birthday
-------------------------
1 | Bob | 1/19/1965
2 | Ann | 9/15/1968


How could I find the person's name based off of the row ID?
φ
Posts: 280
Joined: October 2nd 2011, 11:00pm
Likes Given: 27
Likes Received: 4
1
#2
MySQL - Find Row Value Based on Another Value in Same Row
Posted October 5th 2013, 2:46pm
Well you'd use a select query like any other. :)
SELECT Name FROM table WHERE Row_id = 1 LIMIT 1

With the actual php, using the phpbb dbal;
$result = $db->sql_query(...SQL query similar to the one above);
$name = $db->sql_fetchfield('name');

(I think it's sql_fetchfield, doesn't look right now that I've written it down)
φ
Posts: 509
Joined: July 9th 2011, 11:00pm
Location: England
Likes Given: 15
Likes Received: 26
1
#3
MySQL - Find Row Value Based on Another Value in Same Row
Posted October 5th 2013, 6:49pm
David B said:
How could I find the person's name based off of the row ID?

Jordan gave you the proper query, but I wonder if you're asking for the right thing.

Perhaps you might want to be more specific -- what exactly are you trying to access in the phpBB database?
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
MySQL - Find Row Value Based on Another Value in Same Row
Posted October 6th 2013, 11:20am
I am trying to access a table that I made for the MOD, and LGForum's query works, but I can't get the result to echo, which I want to happen. I am writing a series of PHP files that make changes to the database, and the Java program that I am eventually going to write will pass its information through the PHP files. I understand that you can connect directly to a database using Java, but I think that it is rather insecure (if the program gets decompiled, all the database information will be in it) to do so, so I am letting the PHP file handle it for me.
φ
Posts: 280
Joined: October 2nd 2011, 11:00pm
Likes Given: 27
Likes Received: 4
MySQL - Find Row Value Based on Another Value in Same Row
Posted October 6th 2013, 2:37pm
If you want to 'echo' it out, then you only need to add echo $name; to the second code box in my last post. If it's possible the value doesn't exist then you may need to add an if() block.
φ
Posts: 509
Joined: July 9th 2011, 11:00pm
Location: England
Likes Given: 15
Likes Received: 26
MySQL - Find Row Value Based on Another Value in Same Row
Posted October 6th 2013, 3:32pm
Okay. Does your code use sqli? I just want to make sure because apparently you can access a database without sqli, but the method to do so will soon be removed from PHP. I do not know if that is true or not, but that's what I've heard.
φ
Posts: 280
Joined: October 2nd 2011, 11:00pm
Likes Given: 27
Likes Received: 4
MySQL - Find Row Value Based on Another Value in Same Row
Posted October 6th 2013, 4:46pm
phpBB uses a template system to generate output to the client. Using echo() inside a phpBB modification will almost certainly generate PHP errors, usually a warning that output has begun before headers have been sent. There are numerous modifications in the phpBB3 Hacks forum here that should provide examples of how to generate output using the $template class. If you have any specific questions, feel free to ask.

The mysql extension has been deprecated in PHP5.5, so it is best to use the mysqli extension. This should not affect your modification because you should be using the phpBB DBAL (database abstraction layer) to access the database. Jordan's example code used the phpBB DBAL.
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
MySQL - Find Row Value Based on Another Value in Same Row
Posted October 7th 2013, 2:16am
Woops, yeah David, you want to assign the value to the template like you were shown a while back; remember when you were doing things with your chatbox?
φ
Posts: 509
Joined: July 9th 2011, 11:00pm
Location: England
Likes Given: 15
Likes Received: 26
MySQL - Find Row Value Based on Another Value in Same Row
Posted October 7th 2013, 7:20pm
Oooh! Yes, I remember now. I went back to look at it. Now that it was brought up, I remember, but as far as I am aware, that only works for phpBB. How would I echo a query's result if I were doing something independent of phpBB? I am hoping to soon write something that is independent of phpBB that interacts with a MySQL database.
φ
Posts: 280
Joined: October 2nd 2011, 11:00pm
Likes Given: 27
Likes Received: 4
MySQL - Find Row Value Based on Another Value in Same Row
Posted October 8th 2013, 3:52am
Well it depends really. You can directly echo it out using 'echo', you can kill the script and output just the value with die. You can output it with print. Or you may even decide to use a templating system on your own project, in which case you'd assign it to the template however it is written to do so.

Your functions would be different if your planning on using MySQLi.
$db = new mysqli(...database details, look at documentation...); // this only needs to do be done once in the script not every query
$result = $db->query(...the sql query...);
$row = $result->fetch_assoc();
$name = $row['name'];
//echo or user the name as you please
φ
Posts: 509
Joined: July 9th 2011, 11:00pm
Location: England
Likes Given: 15
Likes Received: 26
MySQL - Find Row Value Based on Another Value in Same Row
Posted October 10th 2013, 4:43am
Hmmm...none of the methods that I have tried to print the result of the query have worked. I am beginning to wonder if the problem lies not with the way that I am trying to print the result, but rather lies with the query itself.
φ
Posts: 280
Joined: October 2nd 2011, 11:00pm
Likes Given: 27
Likes Received: 4
MySQL - Find Row Value Based on Another Value in Same Row
Posted October 10th 2013, 8:45am
You can check your query in phpMyAdmin. If it generates an error or returns no results, then you know the query is the issue. If it returns results, then check the code in your program.
φ
Posts: 1599
Joined: March 12th 2009, 11:00pm
Location: Uncertain due to momentum
Likes Given: 26
Likes Received: 357
Post a reply
12 posts

Who is online

Users browsing this forum: No registered users and 0 guests