crmamx Posted January 30, 2011 Share Posted January 30, 2011 I am brand new to mysql and php but I have created a database and loaded two tables using cPanel and phpMyAdmin. Now I need some programs to access my data. I have a couple of simple ones that work, but I can't figure out what I really need, I am trying to Select a table Where the Value is a $variable, not a fixed value. Of course the end result will be to pass the value from a Form, but I have to get this to work first. <?php // Connect to database============================= include("connect_db.php"); $table='airplanes'; $amano='123456' $iden='1' // Send query =========================================================== // $result = mysql_query("SELECT * FROM {$table} where ama='123456'"); == this works // $result = mysql_query("SELECT * FROM {$table} where ama='940276'"); == this works // $result = mysql_query("SELECT * FROM {$table} where id='1'"); // this works // $result = mysql_query("SELECT * FROM {$table} where id = '{$iden}'"); == doesnt work // $result = mysql_query("SELECT * FROM {$table} where id = $iden"); == doesnt work // $result = mysql_query("SELECT * FROM {$table} where id = ($iden)"); // == doesnt work // $result = mysql_query("SELECT * FROM {$table} where id = $iden"); // == doesnt work // $result = mysql_query("SELECT * FROM {$table} where ama='$amano'"); // == doesnt work $result = mysql_query("SELECT * FROM {$table} where ama=($amano)"); // == doesnt work Thanks Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/ Share on other sites More sharing options...
Pikachu2000 Posted January 30, 2011 Share Posted January 30, 2011 Start by removing the query string from the query execution and storing it in a variable, then use the variable in the query execution. Check to make sure the query executes successfully, and if not, echo the error along with the query string. $query = "SELECT `field` FROM `table` WHERE `some_field` = 'some_value'"; if( $result = mysql_query($query) ) { if( mysql_num_rows($result) > 0 ) { echo 'Query ran successfully and returned ' . mysql_num_rows($result) . 'results.'; } else { echo 'Query ran successfully, but returned an empty result set'; } } else { // query failed to execute echo "<br>Query: $query<br>Produced error: " . mysql_error() . '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/#findComment-1167305 Share on other sites More sharing options...
crmamx Posted January 30, 2011 Author Share Posted January 30, 2011 Thanks for the reply. $table='airplanes'; $amano='123456' $result = mysql_query("SELECT * FROM {$table} where ama={$amano}"); // == doesnt work Getting ready to try it but might I ask why doesn't $iden or $amano work with the Where clause? $table works fine with the From clause. Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/#findComment-1167350 Share on other sites More sharing options...
Pikachu2000 Posted January 30, 2011 Share Posted January 30, 2011 It *should* work just fine, but until the logic is such that you can return the error and the actual query string that caused the error, all we can do is guess why it failed. Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/#findComment-1167351 Share on other sites More sharing options...
crmamx Posted January 30, 2011 Author Share Posted January 30, 2011 I guess I just do not understand. Tried this: <?php include("connect_db.php"); $amano='123456' $query = "SELECT `ama` FROM `airplanes` WHERE `ama` = '$amano'"; // doesn't work // $query = "SELECT `ama` FROM `airplanes` WHERE `ama` = '123456'"; // works, returns 1 result // $query = "SELECT `field` FROM `table` WHERE `some_field` = 'some_value'"; Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/#findComment-1167373 Share on other sites More sharing options...
Pikachu2000 Posted January 30, 2011 Share Posted January 30, 2011 Post the full code you tried that didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/#findComment-1167376 Share on other sites More sharing options...
crmamx Posted January 30, 2011 Author Share Posted January 30, 2011 <html> <head> <title>MySQL Table Viewer </title> </head> <body> <?php // Connect to database============================= include("connect_db.php"); $amano='123456' $query = "SELECT `ama` FROM `airplanes` WHERE `ama` = '$amano'"; // does't work // $query = "SELECT `ama` FROM `airplanes` WHERE `ama` = '123456'"; // works, returns 1 result // $query = "SELECT `field` FROM `table` WHERE `some_field` = 'some_value'"; if( $result = mysql_query($query) ) { if( mysql_num_rows($result) > 0 ) { echo 'Query ran successfully and returned ' . mysql_num_rows($result) . 'results.'; } else { echo 'Query ran successfully, but returned an empty result set'; } } else { // query failed to execute echo "<br>Query: $query<br>Produced error: " . mysql_error() . '<br>'; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/#findComment-1167439 Share on other sites More sharing options...
jcbones Posted January 30, 2011 Share Posted January 30, 2011 Parse error: You forgot your semi-colon after your $amano variable declaration. Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/#findComment-1167447 Share on other sites More sharing options...
Pikachu2000 Posted January 30, 2011 Share Posted January 30, 2011 Of the three possibilities, what output did that code return? Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/#findComment-1167472 Share on other sites More sharing options...
crmamx Posted January 30, 2011 Author Share Posted January 30, 2011 With this query $query = "SELECT `ama` FROM `airplanes` WHERE `ama` = '123456'"; // works, returns 1 result it returned: Query ran successfully and returned 1results. which is the right answer Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/#findComment-1167485 Share on other sites More sharing options...
crmamx Posted January 30, 2011 Author Share Posted January 30, 2011 I can't believe it. I have pulled my hair out. I am an old time cobol/rpg/fortran programmer. Just a novice with HTML and javascript but I usually can get it to work. I know the kind of things to look for but this one has stumped me......till you posted this: Parse error: You forgot your semi-colon after your $amano variable declaration. It works fine. Now I will try to pass the variable to php from a Form. Thank you so much. I have tried several forums but this is the first one where the participants are serious rather than flaming a novice. Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/#findComment-1167490 Share on other sites More sharing options...
Pikachu2000 Posted January 30, 2011 Share Posted January 30, 2011 As long as people ask a reasonable question, and at least try to help themselves, we try to help them learn Which brings up the point that you will save yourself a ton of headaches if you develop with error reporting at max, and display errors on. If you need to know how to do that, just say so. Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/#findComment-1167494 Share on other sites More sharing options...
crmamx Posted January 30, 2011 Author Share Posted January 30, 2011 Saying so. Any and all help would be appreciated. Now I can't get the variable to pass from the Form, but I have an idea what it is if I can just find the tutorial where I read it. Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/#findComment-1167506 Share on other sites More sharing options...
Pikachu2000 Posted January 30, 2011 Share Posted January 30, 2011 Make a backup of your php.ini file, then find the following lines (usually about 1/4 of the way into the file) and edit the live copy so they read as below, then restart apache. error_reporting = -1 display_errors = On Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/#findComment-1167509 Share on other sites More sharing options...
crmamx Posted January 31, 2011 Author Share Posted January 31, 2011 Pikachu: Finally after hours and hours I got the basics to working like I want it. Never would have got it without your help. Now I need to do some error checking and other stuff. Would you be interested in reviewing it and offering your expert comments on what I have done? Thanks a bunch! Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/#findComment-1167608 Share on other sites More sharing options...
Pikachu2000 Posted January 31, 2011 Share Posted January 31, 2011 Sure, just post the code here. There are a lot of highly skilled people here that can look it over. Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/#findComment-1167642 Share on other sites More sharing options...
crmamx Posted January 31, 2011 Author Share Posted January 31, 2011 This is what I ended up with and it seems to work well. <html> <head> </head> <body> <?php // Connect to database===================================================== include("connect_db.php"); $table='airplanes'; // retrieve form data ====================================================== $amano = $_POST['amano']; // sending query =========================================================== $result = mysql_query("SELECT * FROM $table WHERE ama='$amano'") or die(mysql_error()); if (!$result) { die("Query to show fields from table failed"); } echo "<table border='10' cellpadding='3' cellspacing='2'>"; echo "<p>Airplanes for Joe Blow</p><br>"; echo "<tr> <th>ID</th> <th>AMA #</th> <th>Model Name</th> <th>Model MFG</th><th>Wingspan</th><th>Engine</th><th>Decibels</th></tr>"; // keeps getting the next row until there are no more to get ================ while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table ========================== echo "<tr><td>"; echo $row['id']; echo "</td><td>"; echo $row['ama']; echo "</td><td>"; echo $row['model_name']; echo "</td><td>"; echo $row['model_mfg']; echo "</td><td>"; echo $row['wingspan']; echo "</td><td>"; echo $row['engine']; echo "</td><td>"; echo $row['decibels']; echo "</td></tr>"; } echo "</table>"; ?> <br> Put something here. <body> </html> This program will display all the entries for the AMA Number entered. Now I need 3 more programs: 1. Delete an ID number. 2. Add a new entry. 3. Update an ID number. Suspect this will be the most difficult. This stuff is addictive. I need to go work on my airplanes. Quote Link to comment https://forums.phpfreaks.com/topic/226123-help-with-selectwhere-clause-using/#findComment-1167666 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.