Page 1 of 1

Beginner PHP/MySQL question: SELECTing multiple variables.

Posted: Wed Apr 22, 2015 3:39 am
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?

Re: Beginner PHP/MySQL question: SELECTing multiple variable

Posted: Wed Apr 22, 2015 4:57 am
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.

Re: Beginner PHP/MySQL question: SELECTing multiple variable

Posted: Thu Apr 23, 2015 9:38 pm
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 />';
} 

Re: Beginner PHP/MySQL question: SELECTing multiple variable

Posted: Mon Apr 27, 2015 12:24 am
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 :)

Re: Beginner PHP/MySQL question: SELECTing multiple variable

Posted: Wed Apr 29, 2015 6:13 am
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.

Re: Beginner PHP/MySQL question: SELECTing multiple variable

Posted: Wed Apr 29, 2015 10:47 am
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?

Re: Beginner PHP/MySQL question: SELECTing multiple variable

Posted: Wed May 20, 2015 2:31 am
by Kesstryl

Re: Beginner PHP/MySQL question: SELECTing multiple variable

Posted: Wed May 20, 2015 9:21 pm
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.