Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 533
  • Last Modified:

php mysql_query with select

Hey,

I have the following PHP trying to access a mysql database on a Linux server (RHEL5, 64). I am able to write to the database via PHP (thanks to you guys) but when I try to read I simply get nothing back.

Thanks!


<?php
    //  Make a MySQL Connection
         mysql_connect("localhost", "root","") or die(mysql_error());
         mysql_select_db("tapestorage") or die(mysql_error());
        $query="SELECT * FROM tapes";
         mysql_query("$query");
?>

Open in new window

0
Mike R.
Asked:
Mike R.
1 Solution
 
bartvdCommented:
Use:
$result=mysql_query($query);

the result is in $result;
0
 
Greg AlexanderLead DeveloperCommented:
Here are a few examples

<?php
    //  Make a MySQL Connection
        mysql_connect("localhost", "root","") or die(mysql_error());
        mysql_select_db("tapestorage") or die(mysql_error());
        $query = "SELECT * FROM tapes";
        $result = mysql_query("$query");
		
		//here is a loop throough the data
		while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
			echo $row['field_name_here'],"<br>";
		}	
		
		//put the data into an array
		$data = array();
		while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
			$data[] = $row;
		}	
		
		echo "<pre>";
		print_r($data);
		echo "<pre>";
?>

Open in new window

0
 
Cornelia YoderArtistCommented:
<?php
    //  Make a MySQL Connection
         mysql_connect("localhost", "root","") or die(mysql_error());
         mysql_select_db("tapestorage") or die(mysql_error());
        $query="SELECT * FROM tapes";
         $result = mysql_query("$query");

         $row = mysql_fetch_array($result);
         $field1value = $row["Field1Name"];
         $field2value = $row["Field2Name"];

?>
0
 
Cornelia YoderArtistCommented:
If you want to process all rows, use

<?php
    //  Make a MySQL Connection
         mysql_connect("localhost", "root","") or die(mysql_error());
         mysql_select_db("tapestorage") or die(mysql_error());
        $query="SELECT * FROM tapes";
         $result = mysql_query("$query");

         while ($row = mysql_fetch_array($result))
    {
         $field1value = $row["Field1Name"];
         $field2value = $row["Field2Name"];
          //do whatever
     }

?>
0
 
Mike R.Author Commented:
That got it!! Thanks!
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now