FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


SELECTing from MySQL with PHP





ddukki
I'm in need of some assistance from you PHP/MySQL gurus out there. I'm trying to select one piece of data from a MySQL table but I can't find an efficient way, other than doing like so:
Code:
$query = "SELECT info FROM table WHERE id=".$id;
$result = mysql_query($query);
$array = mysql_fetch_array($result);
extract(array);
and using $info from there. But when dealing with one piece of data, the array function doesn't work, since there's only one piece, and it doesn't count it as an array. How do I fetch $info as a variable?
DX-Blog
ddukki wrote:
I'm in need of some assistance from you PHP/MySQL gurus out there. I'm trying to select one piece of data from a MySQL table but I can't find an efficient way, other than doing like so:
Code:
$query = "SELECT info FROM table WHERE id=".$id;
$result = mysql_query($query);
$array = mysql_fetch_array($result);
extract(array);
and using $info from there. But when dealing with one piece of data, the array function doesn't work, since there's only one piece, and it doesn't count it as an array. How do I fetch $info as a variable?

$info = $array["info"]; should do
thnn
Code:
$query = "SELECT info FROM table WHERE id=".$id;
$result = mysql_query($query);
$array = mysql_fetch_array($result);
extract(array);


You could also change the line that says $array = mysql_fetch_array($result); to $array = mysql_fetch_object($result); and have

$info = $array->info;
krazycapital
Code:
$query = "SELECT info FROM table WHERE id='$id'";
$result = mysql_query($query);
$array = mysql_fetch_array($result);
extract($array);

Try that.
ddukki
I was actually looking for the mysql_result function. A friend pointed it out to me after he referenced the PHP manual to me. I guess I should post the way it works, for people having a similar annoyance:

Code:
$query = "SELECT info FROM table WHERE id=".$id;
$info = mysql_result(mysql_query($query),0);
    //picks out the first item in the resource result and sets it to variable $info


That's four lines of code reduced to two. But that's when you are looking for only one piece of information.
comp615
You could also use the mysql_fetch_array or mysql_fetch_assoc...you can do those multiple times so like:

while($row = mysql_fetch_array($result) {
do stuff
}
frozenhead
You posted:

ddukki wrote:

Code:
$query = "SELECT info FROM table WHERE id=".$id;
$result = mysql_query($query);
$array = mysql_fetch_array($result);
extract(array);



I'm not trying to be silly here (basing on what you've posted) but the last line will expect a CONSTANT variable in other words you missed the "$" sign in your variable "array". should be "$array" extracting values from your fetch function. Wink . So it should be:

Code:
$query = "SELECT info FROM table WHERE id=".$id;
$result = mysql_query($query);
$array = mysql_fetch_array($result);
extract($array); // <!-- you missed the $ sign here.


At least it will work now, somehow. You know, it pays a lot when you have that keen eye.

_
SgtGarcia
The mysql_result function is really important when using php/mysql. It makes it really easy to for example create a html-table from a database.

Code:

$query="SELECT A,B,C from table"; //the query
$result=mysql_query($query); // get the result from the database
$num=mysql_num_rows($result); //count the number of rows in the table
$i=0;
echo "<table>"; // start the table-output
while($i<$num){
$A=mysql_result($result,$i,"A");
$B=mysql_result($result,$i,"B"); //get the results in variables
$C=mysql_result($result,$i,"C");
echo"<tr><td>$A</td><td>$B</td><td>$C</td></tr>";
$i++;
}
echo "</table>"


This script gets the columns A, B and C from the database and creates a html-table from it. I use it often when i need to get info from a database.
Related topics
Where can I find tutorials for MySQL?
Seeking MySQL-php-Apache installer for WinXP
PHP DB Error: syntax error
How to install PHP and Apache in 5 min on Win98
mysql/php help please
server with ready PHP
MySQL, PHP and APACHE
[ Newbie; Makin pages, mySQL, PHP, etc ]
[ Newbie; Makin pages, mySQL, PHP, etc ]
Is mySQL/php in safe mode on Frihosting?
PHP&MySql - Is there any server side caching of queries?
apache php and mysql on mac OS X
MySQL [php Mail()] and FriHost Mass mail rules
Setting up a local PHP and MySQL Workspace
What to learn first?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.