0

I am trying to create a PHP variable from a database table with only one row and one column. So Basically, I'm trying to pass information within several pages using a database. Is this possible? I have a table called name and a column on it also called name. I only have one row on it, and I would like to select the value of that only field and assign a variable to it. How do I do this? Thanks!

Edit: I have tried:

$name = mysql_query("SELECT * FROM name ORDER BY RAND() LIMIT 0,1");

var_dump($name);  

But it returns null.

1

1 Answer 1

1
$result = mysql_query("SELECT * FROM lv1 ORDER BY RAND() LIMIT 0,1"); //You get only 1 row

$name = ($result && mysqli_num_rows($result) == 1) ? mysql_fetch_assoc($result) : null ;

var_dump($name) ;
Sign up to request clarification or add additional context in comments.

6 Comments

thanks, but I forgot to mention, I need multiple variables that come from different tables
also, I get Parse error: syntax error, unexpected '[' in /homepages/12/d441172468/htdocs/Maze/process.php on line 12. Line 12 is where I put the $name = ($result) ? mysql_fetch_assoc($result)['name'] : null ;
it works, but the output is array(1) { ["name"]=> string(5) "Jerk." }. When I open Firebug, it tells me that the value of the $name is ""
when I do echo $name, it doesnt show Jerk.. Instead, it shows Array. How do I fix this? Sorry, Im kinda new to PHP
that fixed it, but how do I create 10 more variables, all from different tables?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.