Question:
How can I extract the data in my SQL table using PHP? The code I am using only works for some values...?
2007-11-26 19:44:50 UTC
I need to extract the value contained in the column "reqtable1".
Here is the code I am using for other columns such as "name":

$rdata = mysql_query("SELECT * FROM $infotable")
or die(mysql_error());
$info = mysql_fetch_array( $rdata );
$name1 = $info['name'];
-- If I echo $name1, it returns as 'John Doe'...

Heres my code for extracting the value in column reqtable1:

$rdata = mysql_query("SELECT reqtable1 FROM $infotable")
or die(mysql_error());
$info = mysql_fetch_array( $rdata );
--- "print_r($info);" here returns "Array ( [0] => [reqtable1] => )"

If I use SELECT * FROM instead of SELECT reqtable1 FROM, I still cannot retrieve the column value...
Here is my SQL table as shown in PHPMyAdmin:

name____John Doe___NULL
email____J@d.com___NULL
phone____555-555___NULL
pref______2________ NULL
scanner___Pro-96____NULL
city______Testville____NULL
state_____Hawaii____ NULL
reqtable1__NULL____ request211961249


Any advice helps, thank you!!!!
Four answers:
WordToTheWise
2007-11-26 23:58:32 UTC
"$rdata = mysql_query("SELECT * FROM $infotable")

or die(mysql_error());

$info = mysql_fetch_array( $rdata );

$name1 = $info['name'];

-- If I echo $name1, it returns as 'John Doe'..."



* * * * *

Does that above work (your first example)?

Technically, I don't think that works. The function mysql_fetch_array returns a result that contains array elements. In your example, you have mysql_fetch_array being set to $info. So all the results would be inside $info in the form of an array of arrays. Let me explain by example:



Let's say my table had columns like this:

First Name (first_name)

Last Name (last_name)

Age (age)



Then my records look like this:

John, Doe, 25

Jane, Doe, 28

Jim, Doe, 19



If I performed a SELECT * on that table, it would return like this:

Row 0 = Array with values of John, Doe, 25

Row 1 = Array with values of Jane, Doe, 28

Row 2 = Array with values of Jim, Doe 19



So technically, to access Jane Doe's information, I would need:

$info[1]['first_name'] = 'Jane'

$info[1]['last_name'] = 'Doe'

$info[1]['age'] = '28'



So in your example, $info['name'] wouldn't work.



So on to your second and last examples, to access a specific column, you would need to find the row you want to access and the column name. So to get "John Doe"'s name from your table, you would need to do:

$info[0]['name']



Now if you want to get the info on all the rows on your table, you need to put it in a loop to access the info. And for that, you should refer to the example on PHP.net (http://www.php.net/mysql_fetch_array)
kaitlyn
2016-05-26 05:58:19 UTC
$db_hostname = 'localhost'; $db_database = database; $db_username = 'root'; $db_password = ''; $db_server = mysql_connect($db_database); mysql_select_db($db_database) or die("Unable to select database: " . mysql_error()); $query = "SELECT SUM( `product_quantity` ) FROM `confirmed_orders` WHERE `product_id` = '" . $custom[3]. "' AND `order_status` = 'S' "; $result = mysql_query($query) or die (mysql_error()); $row = mysql_fetch_array($result); $sum = $row[0];
2007-11-26 20:31:42 UTC
Are you sure it's not hitting a separate server when PHP runs the script? It's possible you have two databases? Your query looks fine to me.
2007-11-29 17:18:02 UTC
Try this website: http://www.mysqlphpcode.com/ . This is a pretty good code.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...