Beginner PHP/MySQL question: SELECTing multiple variables.

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
jameshutchings
Posts: 8
Joined: Sun Mar 08, 2015 4:18 am

Beginner PHP/MySQL question: SELECTing multiple variables.

Post by jameshutchings »

Say I SELECT several things from a single database, and then put one into a variable:

Code: Select all

$result=mysql_query ("SELECT id, firstname, lastname FROM characters",$link);
$x=mysql_result($result,1);
In this example, will $x be equal to the result stored in firstname?

If not, what would I enter to get $x equal to firstname?
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

Re: Beginner PHP/MySQL question: SELECTing multiple variable

Post by a_bertrand »

Why not make a var_dump and see what's inside your variable? BTW All that info can easily be found with a single google search.
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Beginner PHP/MySQL question: SELECTing multiple variable

Post by Chris »

Code: Select all

$result=mysql_query ("SELECT id, firstname, lastname FROM characters",$link);
while($x=mysql_fetch_assoc($result)) {
    echo $x['firstname'] . '<br />';
} 
Fighting for peace is declaring war on war. If you want peace be peaceful.
gedq
Posts: 18
Joined: Sun Apr 05, 2015 9:53 pm

Re: Beginner PHP/MySQL question: SELECTing multiple variable

Post by gedq »

no, what you'll get back is a mysql result set, which can't be read directly from PHP, you have to use a function to read it, it can then be converted to native php data types. You're using mysql_ functions, which are deprecated and likely to be removed in a year or two, destroying your code; you're best moving to mysqli or pdo *now*. In the meantime Chris is right, but you'll need to be aware of the result pointer - PHP maintains a pointer to the current record inside the mysql result set. mysql_fetch_assoc returns the row at the current pointer position and bumps the pointer up one. This can cause problems if you need to read the same result set multiple times.

Also remember (this is right? It's certainly my memory although I haven't used mysql functions for a while), that to convert a mysql_ result set into an array you need to loop through it one record at a time, which can take time, especially with large data sets. With PDO all you need is

$myArray = $dbResult->fetchAll(PDO::FETCH_ASSOC);

one call. so much faster :)
jameshutchings
Posts: 8
Joined: Sun Mar 08, 2015 4:18 am

Re: Beginner PHP/MySQL question: SELECTing multiple variable

Post by jameshutchings »

i) Where can I learn about mysqli?

ii) For now, what do I do to get id, firstname etc?

I forgot to add that there should only be one result for this SELECT.
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Beginner PHP/MySQL question: SELECTing multiple variable

Post by Winawer »

jameshutchings wrote:i) Where can I learn about mysqli?
http://php.net/manual/en/book.mysqli.php
jameshutchings wrote:ii) For now, what do I do to get id, firstname etc?
Do what Chris showed you.
jameshutchings wrote:I forgot to add that there should only be one result for this SELECT.
Your game will only have one character?
Sim
Posts: 412
Joined: Sat Dec 26, 2009 5:37 pm

Re: Beginner PHP/MySQL question: SELECTing multiple variable

Post by Sim »

I thought I should point out, they are not called variables, but selecting the values from multiple fields(your so called variables in a database table). This is not to insult you, but to give you a better understanding of the terminology.
oRPG Creator - Make Your Own Browser Game
oRPG Creator on Facebook
Post Reply

Return to “Beginner Help and Support”